Introduction to RStudio

How to install RStudio

  1. Go to www.rstudio.com and click on the Download button on the top bar.
  2. Scroll down and click on the Download button for the free RStudio Desktop version.
  3. Download the latest version for your operating system:
    • Windows users: Click on the button Download RStudio for Windows. The file should have a .exe extension.
    • Mac users: Click on Download RStudio for (Mac) OS X. You should see a .dmg extension.
  4. Install it!

The RStudio graphical user interface (GUI)

The RStudio interface has four panes:

  • Editor: This is were you usually code. You can either use .Rmd (R-Markdown) or ``.R(plainR` code) files.
  • Console: This is where the results appear once you execute your R-code. You can also directly type R-code into the console and execute it. However, we cannot save this code which is why we usually work in the Editor.
  • Environment: Here you have an overview over all the objects currently loaded in your environment. You will learn more about objects later in the course.
  • Files, Plots, Packages, Help, Viewer: Plots and other things will appear here, don’t worry too much about it for the moment.

RStudio Projects

RStudio projects, .Rproj, make it straightforward to divide your work into multiple contexts (Environments), each with its own working directory, workspace, history, and source documents. A project is basically a folder on your computer that holds all the files relevant to a particular piece of work. Working in RStudio Projects has multiple advantages:

  • Once an RStudio Project is set up, you do not have to worry about your working directory anymore. By default, the location of the R-project (.Rproj) or script (.R/.Rmd) is also the working directory (wd).
  • When opening an RStudio Project, a new R session (process) is started. This makes sure that things you do in different projects do not mess up or get intermixed. Note: By default, R projects share the same default library of packages (software addons for R providing additional or revised functionality). Each version of R (not RStudio or RStudio Project) shares the same local library of installed packages (on the device you are using).Refer to the section on Replication illustrating alternatives for library management.
  • RStudio projects can easily be exported to and imported from GitHub.

Shortcuts and Keybindings

  • To run a line of code using Windows or Linux, put the cursor on the same line then press ctrl + enter. The same shortcut works to run a highlighted part of code. On macOS, replace cmd for ctrl.
  • To run the entire code chunk press ctrl + shift + enter. On macOS, either replace cmd for ctrl or use the functionally equivalent shortcut cmd + option + c.
  • To add a new code chunk (in R-Markdown files, or .rmd), click the Insert Chunk button on the toolbar above, alternatively press Ctrl+Alt+I on Windows or Cmd+Option+I on macOS.

An overview of all shortcuts (for your current operating system) is available under Tools > Keyboard Shortcuts Help.

Cheatsheets

For a concise overview of RStudio, you can find a cheatsheet for RStudio here.

A collection of cheatsheets can be accessed via the website of RStudio developer ‘Posit’. Some of the cheatsheets are also available in German here.

Next