R and RStudio Setup


R and RStudio

R is an open-source programming language for statistical computing. R is widely-used in both academia and industry, due to its capacity for statistical analysis and data science.

In order to use R effectively, you need a suitable editing environment, i.e. RStudio. For more context, RStudio is an integrated development environment (IDE) developed specifically for R programming. Although R can be run without RStudio, RStudio provides a more user-friendly experience with additional functionality.

To download R and RStudio, visit https://posit.co/download/rstudio-desktop.

You will see the following two tasks to be completed:

  • 1: Install R
  • 2: Install RStudio

Step 1: Install R

(Note that the following instructions apply to the latest R version (4.4.0) as of May 24, 2024)

Click on DOWNLOAD AND INSTALL R. This will direct you to the CRAN (Comprehensive R Archive Network) website.

macOS

  • Click on Download R for macOS.

  • Choose the .pkg file suitable for your Mac (Apple silicon (M1-3) Macs or older Intel Macs.)

  • Open the .pkg file after the download is complete.

  • Follow the installation instructions.

Windows

  • Click on Download R for Windows.

  • Click on install R for the first time (on the same line as the base subdirectory.)

  • Choose Download R 4.4.0 for Windows.

  • Open the .exe file after the download is complete.

  • Follow the installation instructions.

Step 2: Install RStudio

Important

R must be installed before RStudio.

Click on DOWNLOAD RSTUDIO DESKTOP… Your operating system is automatically detected. (If your OS is not correctly detected, scroll down and choose the right version for your system.)

macOS

  • Open the .dmg file after the download is complete.

  • Drag and drop it to your Applications folder.

Windows

  • Open the .exe file after the download is complete.

  • Follow the installation instructions.

Step 3: Check R and RStudio installations

Open RStudio and type in the following command in the Console pane.

version

This will print out the current version of R on your machine. The output should look similar to what shown below (the first 4 lines might be different, depending on your operating system.)

               _                           
platform       aarch64-apple-darwin20      
arch           aarch64                     
os             darwin20                    
system         aarch64, darwin20           
status                                     
major          4                           
minor          4.0                         
year           2024                        
month          04                          
day            24                          
svn rev        86474                       
language       R                           
version.string R version 4.4.0 (2024-04-24)
nickname       Puppy Cup                   

Step 4: Install an R package

In R, a package is a collection of functions, data, and compiled code. In addition to a set of built-in base packages, there are numerous external R packages written by the community to add specific functionality.

In general, to install an R package, you can use the install.packages() function and pass in the package name.

The following example shows how to install the tidyverse package in R. The tidyverse is a suite of R packages we will be using throughout this program. It features popular packages such as ggplot2 for data visualization and dplyr for data manipulation.

After installing R and RStudio, open RStudio and enter the following command in the Console pane.

install.packages("tidyverse")

To verify that tidyverse is successfully installed, run the following command:

library(tidyverse)

You should get a message similar to the output below.

── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Ask us for help if you encounter any issues or errors in any of the installation steps above.

R Primers on Posit Cloud

In addition to following the steps above for installing R and RStudio on your computer, we recommend you make a free Posit Cloud (formerly RStudio Cloud) account at https://posit.cloud/. This is a free, browser-based version of R and RStudio that also provides access to a growing number of relevant R tutorials / primers.

After you create a Posit Cloud account, navigate to the menu on the left and click on “Recipes”. This brings up a menu of tutorials, with code primers you can choose to work through. Please complete the tutorials listed under “R Basics” (you can skip the first three). Also, feel free to explore the other tutorials.