4  Introduction to RStudio

4.1 Panes in RStudio

The first time we open RStudio we will see three panes as in Figure 4.1.

Figure 4.1: RStudio interface.

Pane 1 consists of three tabs:

  1. Console - This pane is where R codes can be typed and executed. The console also is where the output will be displayed. However, any R codes typed into the console can not be saved.

  2. Terminal - This is a command-line interface that allows users to interact with the system shell, much like other terminal applications on your computer. It supports tasks such as navigating the file system, running command-line tools, and managing files, which can be useful for integrating non-R tasks into your workflow. However, for beginners, this tab is relatively unimportant.

  3. Background Jobs - This pane enables users to run R scripts in the background without interrupting ongoing work in the Console. This feature is helps run long or complex tasks, as you can continue coding or working in the RStudio environment while the script executes independently.

Next, Pane 2 consists of four tabs:

  1. Environment - This pane displays all the active objects in the current R session, such as data frames, variables, functions, and vectors.

  2. History - This pane keeps a record of all commands that have been executed in the R console during the session

  3. Connections - This pane facilitates managing database connections within RStudio. It allows users to connect to external databases, view database contents, and run SQL queries directly from the IDE.

  4. Tutorial - The Tutorial pane is part of RStudio’s learnr package, which hosts interactive tutorials directly within the IDE.

Lastly, Pane 3 consists of five tabs:

  1. Files - This pane allows users to navigate, create, delete, and manage files within the current working directory. It helps in organizing project files, accessing scripts, data files, and outputs.

  2. Plots - This pane displays graphical output generated by R code, such as charts, graphs, and plots.

  3. Packages - This pane shows a list of installed R packages and their status (loaded or not). It also allows users to install, remove, or update packages, providing an easy way to manage package dependencies for projects.

  4. Help - This pane provides access to documentation for R functions, packages, and commands.

  5. Viewer - The Viewer pane is used for displaying web content such as interactive visualizations, markdown files, and other HTML outputs directly within RStudio.

  6. Presentation - This pane supports presenting R Markdown or Quarto documents in an interactive format. For example, if you are making a slide or HTML presentation, it will appear in this pane.

The information can be quite overwhelming, especially if you are new to R and RStudio. At this moment, you do not actually need to know every detail functionalities of each pane and tab yet. Once you become more familiar with RStudio, all this information will become second-hand to you.

4.2 Working directory

In Figure 4.1, we have a Files tab in Pane 3. This is usually where your working directory is located. However, we can also check using the R code below:

getwd()

Additionally, the working directory can also be changed to your preferred location.

setwd("C:/Users/tengk/OneDrive/Desktop")

Here, I changed my working directory to my desktop folder. A good practice when running the analysis in R is to set up your working directory before you start any analysis project. So, any outputs and figures generated during the analysis will be saved in your preferred working directory.

4.3 R script

One of the few things to do before running the analysis project (besides setting up the working directory) is to type the R codes in the R script. R scripts is a plain text file with the extension .R. R script can be saved and the R codes in it can be re-run if needed.

There are a few ways to open the R script.

  1. Click on File (upper left side of RStudio) > New File > R Script.

  2. Click on the green plus button (below the File tab) > R Script as shown in Figure 4.2.

Figure 4.2: Opening the R Script.

Once you manage to open the R Script, you will see an additional pane as shown in Figure 4.3.

Figure 4.3: R Script interface.

The R Script can be saved and additionally, at the lower right side of the pane, we can see R Script, confirming that this newly open file is an R Script.

4.4 Updating R and RStudio

installr package can be used to update R. You will what is an R package and how to install it in Chapter 5.

installr::updateR()

Once you run installr::updateR() either in the Console or R Script, this package will check whether there is a newer version of R or not. If the newer version of R is available, this package installs the newer version after asking a series of questions such as do you want to transfer your old packages to the new version of R and whether you want to update the packages or not.

To update the RStudio, you can click the Help tab (at the top of the RStudio) > Check for Updates.

Figure 4.4: Updating RStudio

4.5 Chapter summary

In this chapter, we learn about:

  1. Basic interface of RStudio.
  2. How to set up the working directory.
  3. How to update R and RStudio.

4.6 Revision

  1. Change your working directory to your desktop.

  2. Try creating a new R Script, rename it as Test.R, and save it in your new working directory.