Chapter 4 A Tour of RStudio

In this chapter we will talk about a very useful R-related tool called RStudio. RStudio is an environment for using R that can be extremely helpful for writing code and making your analyses reproducible.

A Tour of RStudio, Learning Objectives are to be able to, Recognize why RStudio is useful, Install RStudio and get started with it, Navigate RStudio

4.1 Why use RStudio?

RStudio is what is called an integrated development environment (IDE) for writing code in R (although it also has compatibility for other languages).

It is designed to make working in R easier in a variety of ways by helping you:

  • write code by using suggestions to complete what you have written - currently this is mostly for suggesting package names or functions (which are specific pieces of code that accomplish a particular task, often packages have several functions)
  • view the output of your code, this is especially true for creating reports or viewing plots
  • find errors in your code
  • keep track of any objects that you have assigned in R
  • orient yourself in terms of the files on your computer
  • track changes in your code and other files over time
  • IDE - Integrated Development Environment - a computing environment for writing code, debugging code, and looking at the output of your code
  • RStudio - an IDE designed especially for writing R code
  • function - a specific piece of code that performs a task - packages in R often have several functions
  • objects - objects in R could be anything that you can refer to with some name to recall again such as a data tables, vectors, functions, plots and more.

We will dive deeper into these benefits later once we get started with RStudio, but first we will discuss how to make sure you have it downloaded and installed on your computer.

4.2 Installing RStudio

In case you don’t yet have RStudio on your computer, we will walk you through the process of getting started.

4.2.1 Installing and Updating R

You first need to make sure that you have R. R is not the same as RStudio. R is instead the libraries needed to use R code on your computer and it is needed so that you can use RStudio. It is also a good idea to update the version of R that you are using periodically.

Click here for directions if you have never installed R before on your computer.

You can install the latest version of R from the R project site located here: https://www.r-project.org/

From here you can click on the menu option that says CRAN on the far left to start. Recall that CRAN stands for the Comprehensive R Archive Network.

Once you go to the r project website, you can click on the CRAN button to download the latest version of R

This will take you to a website with a list of what are called mirrors, which are locations that have the same exact copy of R but are dispersed geographically mostly to improve download speeds for users. Nothing bad will happen if you click on a mirror that isn’t closest to you, but it can improve download speeds for everyone overall if people use appropriate mirrors.

CRAN mirror list

Once you click on one of the mirror links you will be taken to a new page to download R. For example, you could click on the Iowa state University mirror if you are located in the US somewhere.

Page to download R for computers with different operating systems, such as Mac or Windows

You would want to click on the appropriate link for your computer. For example, if you have a Windows machine, click the link for Windows.

This will take you to a new page to select the appropriate link to download R. For Mac users this might be the most recent version of R which will look like R and several numbers afterwards.

Click here for instructions on how to update R.

To update R, if you are using a Mac or Linux computer, you can follow the directions of installing R the first time.

If you have a Windows computer, you can use the following code to update your version of R within an R session. You can start an R session by typing R into Command Prompt window. If you have not used the Command Prompt window, read instructions here about how to find it. After opening your Command Prompt window, copy and paste this code and press enter.

# Check for the install r package and install if needed
if(!require(installr)) {
  install.packages("installr");
  require(installr) #load installr pakage
}

updateR()#update your version of R

4.2.2 Installing RStudio

Next we want to download and install RStudio. You can do so by going to the Posit website at this link: https://posit.co/. Note that you can likely accomplish all you need with the completely free option.

Posit is a software company that used to be called RStudio that develops open-source data science tools and packages. It is a Public Benefit Corporation (PBC) and a Certified B Corporation®, so it is committed to creating software that benefits the public. See here for more information.

Click here for instructions on how to download and install RStudio.

The Posit website for downloading RStudio Note that the website may look slightly different when you visit it.

There should be a download button on the upper right corner. This will take you to another page to choose if you want the free or paid version of RStudio. The free version should be enough for most users.

The free version of RStudio is likely all you need.

Then you need to scroll down to select the appropriate download for your computer based on what kind of computer you have.

Select the appropriate link to download RStudio according to the type of computer you have.

Note that by the time you read this the versions will likely have changed and there may be slight variations in how the website appears.

You should then be directed by your computer on how to install RStudio once the download is complete. You may need to go to your downloads first and click on the RStudio file that was downloaded to start this process.

For Mac users, note that you will need to move the RStudio icon into the icon that looks like the Applications folder.

Drag and drop RStudio into the Applications folder to install on a Mac

Drag and drop RStudio into the Applications folder to install on a Mac.

Seehere for more information on the process of installing RStudio.

If you run into trouble, check the following:

  • Did you install the correct version of software for your operating system?
    • Check that you installed the version right for your type of system, (macOS vs Windows for example)
    • Check if maybe you need a different version for the age of your system. First check that your version of R was right - there are multiple versions for different macOS systems for example. You can check the apple icon (top left corner) and “About This Mac” to learn more about the age of your operating system.

If your operating system is older (and you can’t update it), try installing progressively older versions found here until it works. You will know if it worked if you try to open RStudio and you see an interface without a message about things going poorly. Here you can see an example of this.

4.2.3 Updating RStudio

It is also a good idea to keep RStudio up-to-date. New features become available as the Posit team works on developing RStudio. So if you already have RStudio, you might want to check to see if your version is up-to-date.

To check for updates you can go to the Help menu at the top of RStudio and then click on Check for Updates.

Check for updates for RStudio in the Help menu of RStudio.

If you don’t need to update RStudio, when you check with this method RStudio will let you know that you are using the newest version.

An example of the popup that shows you that you are using the newest version of RStudio.

With recent versions RStudio will also give you a popup to let you know that you could update.

Popup indicating that you could update RStudio.

4.4 Find Errors

Another nice thing about RStudio, is that it can help you troubleshoot your code.

It helps to identify common coding mistakes. It will indicate a potential problem by showing a red circle with an “x” in it on the far left of the Editor near the line of code that it thinks is problematic. Note that sometimes errors may occur earlier in your code than where RStudio starts to notice an issue.

Here is an example of such a case. Here we have an extra parentheses in our code.

An example of RStudio showing a potential coding error. Hovering over the red circle with the x near the line of code that it sees an issue with, will give a message about what might be wrong.

Note that just because RStudio thinks your code is free of errors, it does not necessarily mean that your code is correct. RStudio can detect certain syntax issues, but it does not detect all types of errors. However, you can probably see how it could be very helpful!

4.5 Keyboard Shortcuts

There are lots of useful keyboard shortcuts for RStudio that can save you time.

Check out this link if you are interested!

The most helpful shortcut, is for testing a selection of code in an R Markdown file using a keyboard shortcut of Ctrl+Enter on Windows & Linux computers or Cmd+Return on Mac computers.

4.6 Conclusion

In summary…

  • RStudio can help you write code in R and work with files on your computer.
  • There are 3 main panes when you first open RStudio, to see a fourth you need to create a new file like an R Markdown file.
  • When we open a file like an R Markdown file, the top left pane called the Editor is for writing code we wish to save.
  • After opening a file, the lower left pane contains the Console which is where we test code.
  • R Markdown files are files that create reports of an analysis that can demonstrate more about what you did than a simple script and test to make sure that your code works.