Skip to main content

Tour to R Studio!

The Beginner’s Guide to R Studio:

In last couple of lessons, we installed R and R Studio, in this lesson we will familiarize ourselves with the various components and functionality of R Studio! It provides a cheat sheet of the R Studio environment - warning: this link initiates a download of a PDF from the R Studio GitHub.




The various quadrants

R Studio can be roughly divided into four quadrants, each with specific and varied functions, plus a main menu bar. When you first open R Studio, you should see a window that looks roughly like this:

R Studio’s main interface

You may be missing the upper left quadrant and instead have the left side of the screen with just one region, “Console” - if this is the case, go to File > New File > R Script and now it should more closely resemble the image. You can change the sizes of each of the various quadrants by hovering your mouse over the spaces between quadrants and click-dragging the divider to resize the sections.

We will go through each of the regions and describe some of their main functions. It would be impossible to cover everything that R Studio can do, so we urge you to explore R Studio on your own too!

The four main quadrants of R Studio, plus the main menu bar

The menu bar

The menu bar runs across the top of your screen and should have two rows. The first row should be a fairly standard menu, starting with “File” and “Edit.” Below that, there is a row of icons that are shortcuts for functions that you’ll frequently use.

The commonly used options of the main menu bar

To start, let’s explore the main sections of the menu bar that you will use. The first being the File menu. Here we can open new or saved files, open new or saved projects (we’ll have an entire lesson in the future about R Projects, so stay tuned!), save our current document or close R Studio. If you mouse over “New File”, a new menu will appear that suggests the various file formats available to you. R Script and R Markdown files are the most common file types for use, but you can also generate R notebooks, web apps, websites, or slide presentations. If you click on any one of these, a new tab in the “Source” quadrant will open. We’ll spend more time in a future lesson on R Markdown files and their use.

The File menu

The Session menu has some R specific functions, in which you can restart, interrupt or terminate R - these can be helpful if R isn’t behaving or is stuck and you want to stop what it is doing and start from scratch.

The Session menu

The Tools menu is a treasure trove of functions for you to explore. For now, you should know that this is where you can go to install new packages (see next lecture), set up your version control software (see future lesson: Linking GitHub and R Studio), and set your options and preferences for how R Studio looks and functions. For now, we will leave this alone, but be sure to explore these menus on your own once you have a bit more experience with R Studio and see what you can change to best suit your preferences!

The Tools menu

The console

This region should look familiar to you - when you opened R, you were presented with the console. This is where you type and execute commands, and where the output of said command is displayed.

The console

To execute your first command, try typing 1 + 1 then enter at the > prompt. You should see the output [1] 2 below your command.

Typing into the console and getting an output

Now copy and paste the following into your console and hit enter.

example <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8), nrow = 4, ncol = 2)

This creates a matrix with four rows and two columns, with the numbers 1 through 8.

The environment/history

To view this matrix, first look to the Environment quadrant, where you should see the following:

The environment quadrant

Click anywhere on the “example” line, and a new tab on the Source quadrant should appear, showing the matrix you created. Any data frame or matrix that you create in R can be viewed this way in R Studio.

Your newly made matrix, opened in a new tab of the source panel

R Studio also tells you some information about the object in the environment, like whether it is a list or a data frame or if it contains numbers, integers or characters. This is very helpful information to have as some functions only work with certain classes of data. And knowing what kind of data you have is the first step to that.

The quadrant has two other tabs running across the top of it. We’ll just look at the History tab now. Your history tab should look something like this:

The history tab

Here you will see the commands that we have run in this session of R. If you click on any one of them, you can click “To Console” or “To Source” and this will either rerun the command in the console, or will move the command to the source, respectively. Do so now for your example matrix and send it to Source.

From history tab to Source

The source/The script editor panel

The Source panel is where you will be spending most of your time in R Studio. This is where you store the R commands that you want to save for later, either as a record of what you did or as a way to rerun code. We’ll spend a lot of time in this quadrant when we discuss R Markdown, but for now, click the save icon along the top of this quadrant and save the script as my_first_R_script.R Now you will always have a record of creating this matrix.

Your first R script!

Files/help/plots/packages panel

The final region we’ll look at occupies the bottom right of the R Studio window. In this quadrant, five tabs run across the top: Files, Plots, Packages, Help, and Viewer.

In Files, you can see all of the files in your current working directory. If this isn’t where you want to save or retrieve files from, you can also change the current working directory in this tab using the ellipsis at the far right, finding the desired folder, and then under the “More” cogwheel, setting this new folder as the working directory.

The files tab

In the Plots tab, if you generate a plot with your code, it will appear here. You can use the arrows to navigate to previously generated plots. The Zoom function will open the plot in a new window, that is much larger than the quadrant. Export is how you save the plot. You can either save it as an image or as a PDF. The broom icon clears all plots from memory.

The plots tab

The Packages tab will be explored more in depth in the next lesson on R packages. Here you can see all the packages you have installed, load and unload these packages, and update them.

The packages tab

The Help tab is where you find the documentation for your R packages and various functions. In the upper right of this panel there is a search function for when you have a specific function or package in question.

The help tab


Summary

In this lesson we took a tour of the R Studio software. We became familiar with the main menu and its various menus. We looked at the Console, where R code is input and run. We then moved on to the Environment panel that lists all of the objects that have been created within an R session and allows you to view these objects in a new tab in Source. In this same quadrant, there is a History tab, that keeps a record of all commands that have been run. It also presents the option to either rerun the command in the Console, or send the command to Source, to be saved. Source is where you save your R commands. And the bottom right quadrant contains a listing of all the files in your working directory, displays generated plots, lists your installed packages, and supplies help files for when you need some assistance! Take some time to explore R Studio on your own!

 

Comments

Popular posts from this blog

What is Data? And What is Data Science Process?

The Beginner’s Guide to Data & Data Science Process About Data: In our First Video today we talked about Data and how the Cambridge English Dictionary and Wikipedia defines Data, then we looked on few forms of Data that are: Sequencing data   Population census data ( Here  is the US census website and  some tools to help you examine it , but if you aren’t from the US, I urge you to check out your home country’s census bureau (if available) and look at some of the data there!) Electronic medical records (EMR), other large databases Geographic information system (GIS) data (mapping) Image analysis and image extrapolation (A fun example you can play with is the  DeepDream software  that was originally designed to detect faces in an image, but has since moved on to more  artistic  pursuits.) Language and translations Website traffic Personal/Ad data (e.g.: Facebook, Netflix predictions, etc.) These data forms need a lot of preprocessin...

Efficient Data Manipulation with Loop Functions in R: A Deep Dive into apply and mapply

The Beginner’s Guide to Loop Functions in R: In addition to lapply and sapply , R also has apply and mapply , which are other loop functions that are commonly used for data manipulation and analysis. In this blog post, we'll explain what these functions are, how they work, and provide some practice material for beginners to intermediate level. apply:  Apply a Function to a Matrix or Array apply is a loop function in R that applies a function to either rows or columns of a matrix or array. Here's the basic syntax: apply(matrix/array, margin, function) The matrix/array argument is the matrix or array you want to apply the function to, and the margin argument specifies whether you want to apply the function to rows or columns. margin = 1 applies the function to rows, while margin = 2 applies the function to columns. The function argument is the function you want to apply. For example, let's say we have a matrix of numbers and we want to apply the sum function to each row:...

Optimization Example of Lexical Scoping in R: Exploring optim, optimize, and nlm Functions

The Beginner’s Guide to Optimization Example of Lexical Scoping in R: When it comes to optimization in R, lexical scoping can be a useful tool for optimizing complex functions that involve multiple variables. In this blog post, we will explore how lexical scoping can be used to optimize a function using the NLL (negative log-likelihood) function , and how the optim, optimize, and nlm functions can be used to perform optimization in R. Optimizing the NLL Function using Lexical Scoping The NLL function is a common function used in optimization problems. It is defined as the negative log of the likelihood function, which is used to estimate the parameters of a statistical model. In R, the NLL function can be defined using lexical scoping, which allows us to pass arguments to the function and access variables from within the function. Here is an example of how to define the NLL function using lexical scoping in R: nll <- function(data, parameters) {   # Define local variables ...