Dive Deeper into RStudio

RStudio Interface

When you first launch RStudio, you may find it overwhelming, or even intimidating. This is how it looks like (appearance may have minor changes between versions):

There are 4 Panes:

  • Top-left: Where you write your script
  • Top-right: Environment, History, Connections
  • Bottom-left: Console, Terminal, Jobs
  • Bottom-right: Files, Plots, Packages, Help, Viewer

You can click on the close icon (×) next to Untitled1 to close off the top-left pane. We will go through each of the remaining panes in more details. Your view should look like this:

  1. Console: This is where you can write your R code interactively
  2. Terminal: A built-in terminal to write your code in. Most versions of Linux / earlier Mac OS defaults to the bash shell. Windows user will get the command prompt
  3. Jobs: Where you can schedule or queue your R scripts and monitor these (“jobs”)

In the console ( 4 ), go ahead and type the following command:

getRversion()

You may notice RStudio’s code completion feature getting into work even as you punch your code in. You can use the UP and DOWN keyboard key to navigate to the desired code and then hit TAB or ENTER to effect the completion:

For the remainder of this tutorial series, whenever you read (or hear me say) “in your R console”, or “write the following code”, I am referring to exactly this Console pane.

If you’re a Linux user or a Mac user that has some experience working with the terminal that ships with your OS, the Terminal tab (2) is just a built-in terminal for convenience’s sake. This is likely going to be your bash shell or the zsh shell, depending on the OS you’re on. You can create a directory, move files, navigate to a different directory, delete files, and do other command-based operation just like you would in your OS terminal.

If you’ve never understood programmers’ obsession with command lines but would like to give it a try, click on the Terminal tab, and then enter the following command, one line at a time. Windows users should look at the notes below:

pwd
ls
mkdir programming-folder
  1. The above line first print your current working directory (hence, pwd). On a Windows OS, you should use chdir instead.
  2. It then lists all the files and directories (i.e folders) in your current working directory (hence, ls). On Windows, you should use dir instead.
  3. mkdir makes a directory (i.e. folder) named programming-folder. If your current directory is your Desktop, return to your desktop and you should see a new directory being created with that name.

You do not necessarily have to worry about Jobs right now. In the future, this is where you schedule or queue your long-running R scripts and monitor, start or stop these (“jobs”) as they execute.

Creating your first variable in R

Back in your R console, type the following code (one line at a time):

website <- "mywebsite.com"
print(website)
Notice that as you do so, a new variable named website, pointing to the value of “mywebsite.com” is created in the Environment.

If you’ve done programming in the past, this is not too unfamiliar to you. However, you may find R’s syntax at creating a variable a little peculiar. A Python programmer for example may have expected to create a variable, named person storing the value “James” to be like the following:

# don't do this!
person = "James"

In fact, if you try it out, R also accepts the above syntax as valid. The person variable is duly created, as reflected in your Environment. At a later course as we dive into R programming, we will discuss the difference, but for now, anytime you read (or hear me say) “create a variable named ___”, stick to using <- .

Environments and History in RStudio

All the variables that you have created in your environment will appear under the Environment ( 5 ) tab.

The History ( 6 ) tab shows you a list of your executed commands, ordered chronologically. Selecting a command and clicking on the “To Console” will send that command into your Console panel, and you can hit enter again to execute the command.

At some point in the future, when you start working with large datasets that don’t fit on your computer’s RAM anymore (or even if they do), you may consider connecting RStudio directly to a data source capable of storing those terabytes of data. The Connection tab allows you to connect with any database of your choice via ODBC or a big data technology like Apache Spark. If none of these make sense, don’t worry about it. I will cover them in a future series when we’re much more ready for that.

Files

Windows and Mac users are used to a graphical interface when it comes to managing files. You can change to a different directory (whenever you hear “directory”, think “folder”), look at a list of files in your current directory, rename files, delete files, create new files etc. You can do all of that in this tab ( 7 ) as well.

Plots

Go back to your Console, and let’s create our first plot! Type the following:

x <- 1:10
y <- x^2
plot(x, y, type="l")

On line 1, you created a variable x and assign it a vector containing integer value of 1 to 10 (1, 2, … , 10). If you issue the command print(x) you will see the 10 values printed out.

On line 2, you created a variable y and assign it a value of x to the power of 2. Feel free to print y out using print(y) if you’re unsure about the values.

On line 3, you plotted x against y. By specifying type='l' you got a line-type instead of the default point-type. The plot is shown in the plots tab. You can save this image as a PNG, JPG, BMP, PDF or other formats, or copy to clipboard and pasting it into a Powerpoint presentation, Word document, or even email by hitting CTRL + V (keyboard shortcut for paste).

Packages

Remember in the earlier lesson I told you that people and companies, small and large contribute to the open source ecosystem that is R. A primary way to do that is by packaging the code you’re written as what is known as a package. In a future lesson, I would show you how to install packages. All packages you install are found under this Packages tab, and a helpful search bar is provided to help you quickly locate a package by name.

Help

Go ahead and type the following code in your console:

hist(y)

Supposed you’re wondering, what does this hist() function do? Well, click on the Help tab and in the search bar within the tab, type “hist” and hit ENTER. This opens up R’s built-in documentation, and sure enough, you’ll see that the function plots a histogram (hence, hist). Alternatively, you can also type help(hist) into the console and it will also pull up this documentation. This works for any functions in R and is easy to perform — wrap the name of the function in help() — and doesn’t require an internet connection because it comes with R. It’s a good thing to try before heading off to Google if you just needed a quick reminder of what a function do and its usage examples.

Viewer

RStudio is powerful – it allows you to create automated reports, interactive web dashboards, export your research to PowerPoint presentations, and export your work to HTML documents. The Viewer panel is a built-in browser to quickly view the HTML or PDF output as you work and is something we’ll look at in more details in a future lesson.

Bonus: Customise your RStudio apperance

If you hit CTRL + , (comma-sign), or CMD +, on a Mac OS, you will bring up the Global options window. Alternatively, you can bring your mouse to the top menu and click on Tools > Global options.

You can pick a theme you like for your RStudio interface, a font, and an editor theme. When you’re happy with the changes, click Apply, and then OK.

See you in the next Lesson!

Scroll to Top