⇦ Back

RStudio is an IDE (integrated development environment) for the R programming language:

So RStudio is a programme that can be used to create and edit R scripts, and also to run them. It’s an IDE and so has an editor as one of its features.

Download and Install

macOS

  • First, install R from CRAN (the Comprehensive R Archive Network)
  • Then, install RStudio by going to the RStudio website > Download > download RStudio Desktop
  • Drag the RStudio icon into the Applications folder so it can be accessed easily

Check that it’s worked by opening the terminal and running:

R --version

This should show a start-up message including the version number so you can double check that you’ve installed the latest stable release. Open R in the terminal by running the following:

R

This will open an interactive command line where you can enter and run R code manually. Close it by running q() and then n to not save the workspace image.

Ubuntu

  • Install R from the terminal with:

    sudo apt install r-base-core
    • If you have Ubuntu 20.04 you will first need to add the ubuntu focal-cran40 repo to your APT package manager. The name of this repo reflects the fact that this is CRAN’s (the Comprehensive R Archive Network’s) R version 4.0 for Ubuntu 20.04 (Focal Fossa).

      sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
      sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
      sudo apt update
      sudo apt install r-base
  • Check that it’s worked by running:

    R --version

    This should show a start-up message including the version number so you can double check that you’ve installed the latest stable release.

  • Open R in the terminal by running:

    R

    This will open an interactive command line where you can enter and run R code manually. Close it by running q() and then entering n to not save the workspace image.

  • You can download and install RStudio by going to its website > Download RStudio > under “RStudio Desktop” click “Download RStudio” > scroll down to “Ubuntu 22” and download its associated deb file (at the time of writing this is rstudio-2023.09.1-494-amd64.deb). If you keep scrolling down the page you will see that you also have the option of downloading a zip/tarball (a .tar.gx file). Only do this if you have a specific reason for wanting this type of file instead of a deb file.

  • Install the deb file from the terminal like so (you will be prompted to enter your computer password):

    cd Downloads
    sudo dpkg -i rstudio-2023.09.1-494-amd64.deb
  • Troubleshooting: if you get the following error (either in the terminal or when trying to open RStudio):

    /usr/lib/rstudio/bin/rsession: error while loading shared libraries: libpq.so.5: cannot open shared object file: No
    such file or directory

    It means you need to install PostgreSQL; follow the instructions here although running the following is a good start:

    sudo apt-get install postgresql-12

Updating

  • To update R, see here
  • To update RStudio, download the latest version
    • On macOS, install it as per normal (ie replace the version you currently have in the Applications folder; any configurations you have will be carried over from the old version to the new version)
    • On Ubuntu, point your terminal at your Downloads folder and install the deb file:
    cd Downloads
    sudo apt install ./rstudio-1.4.1106-amd64.deb

Configure RStudio

  • Add a ruler as a guideline to help keep your scripts the standard width: Tools > Global Options… > Code > Display > check “Show margin” > use a Margin column of 80 characters. This causes a vertical grey line to appear in your scripts at a distance of 80 characters from the left-hand side. This is the standard limit for how wide your scripts should be.
  • In RStudio v1.4+, add another pane so you can view scripts side-by-side: Tools > Global Options… > Pane Layout > Add Column

Keyboard Shortcuts

All the keyboard shortcuts for RStudio can be found by going to Tools > Keyboard Shortcuts Help (Shift+Alt+K). Here is a selection of the most useful ones:

Action macOS Windows & Linux Same?
New R Script Shift+Cmd+N Ctrl+Shift+N Cmd/Ctrl swap
Save Cmd+S Ctrl+S Cmd/Ctrl swap
Save all Alt+Cmd+S Ctrl+Alt+S Cmd/Ctrl swap
Find Cmd+F Ctrl+F Cmd/Ctrl swap
Next tab Ctrl+Tab Ctrl+Tab Yes
Previous tab Ctrl+Shift+Tab Ctrl+Shift+Tab Yes
Close tab Cmd+W Ctrl+W Cmd/Ctrl swap
Comment/uncomment Shift+Cmd+C Ctrl+Shift+C Cmd/Ctrl swap
Re-indent lines Cmd+I Ctrl+I Cmd/Ctrl swap
Indent Ctrl+] Ctrl+] Yes
Outdent Ctrl+[ Ctrl+[ Yes
Run current line or selection Cmd+Enter Ctrl+Enter Cmd/Ctrl swap
Run line(s) without moving cursor Alt+Enter Alt+Enter Yes
Run all lines Alt+Cmd+R Ctrl+Alt+R Cmd/Ctrl swap
Run from beginning to line Alt+Cmd+B Ctrl+Alt+B Cmd/Ctrl swap
Run from line to end Alt+Cmd+E Ctrl+Alt+E Cmd/Ctrl swap
Source the current file Shift+Cmd+S Ctrl+Shift+S Cmd/Ctrl swap
Knit document Shift+Cmd+K Ctrl+Shift+W Cmd/Ctrl swap

⇦ Back