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

Mastering Debugging in R: Essential Tools and Techniques

The Beginner’s Guide to Debugging Tools in R: Debugging is an essential part of programming in any language, including R. When your code doesn't work as expected, it can be frustrating and time-consuming to find and fix the issue. Fortunately, R provides a variety of debugging tools that can help you identify and fix issues in your code more efficiently. In this blog post, we'll explore some of the most useful debugging tools in R, along with examples of how to use them. The browser() function:  The browser() function is a built-in debugging tool in R that allows you to pause the execution of your code and inspect the values of variables at that point. To use the browser() function, simply insert it into your code where you want to pause the execution. For example: my_function <- function(x) {                                              y <- x * 2  ...

Mastering Loop Functions in R: Exploring tapply and split for Data Manipulation and Analysis

The Beginner’s Guide to Loop Functions in R: Loop functions are powerful tools in R for data manipulation and analysis . They provide efficient and concise ways to apply a function to multiple elements of a data structure. Two commonly used loop functions in R are tapply and split . In this blogpost, we will explore these functions in detail and learn how they can be used to effectively analyze and manipulate data. We will cover the basics of these functions and provide practical examples to illustrate their usage. tapply()  tapply is a loop function in R that applies a function to subsets of a vector or array based on a grouping factor. The syntax of tapply is as follows: tapply(X, INDEX, FUN) where X is the input vector or array, INDEX is the grouping factor, and FUN is the function to be applied. Now suppose we have a data frame containing information about various cities, including their population and average temperature. We could use tapply() to calculate the mean popula...

Mastering R Data Types: Matrices, Factors, Missing Values, Data Frames, and Names Attribute

The Beginner’s Guide to R Data Types: R is a programming language that is widely used for data analysis and statistical computing. It has a powerful set of data structures, including vectors, lists, and data frames, that allow users to work with data in a flexible and efficient way. Matrices A matrix is a two-dimensional array in R that can contain elements of any data type. You can create a matrix using the matrix() function. For example: # Create a matrix with 3 rows and 2 columns  my_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2) Factors A factor is a type of variable in R that represents categorical data. Factors are stored as integers, where each integer corresponds to a level of the factor. You can create a factor using the factor() function. For example: # Create a factor with three levels: "low", "medium", "high"  my_factor <- factor(c("low", "high", "medium", "high", "low")) Missin...