R, RStudio, and R Packages

Download and Install R and RStudio

R vs RStudio

  • R is a programming language and open source software environment for predictive analytics and data visualization.

  • RStudio is the premier IDE for R.

  • R may be used without RStudio, but Rstudio may not be used with R.

  • Launch RStudio program, just as you would open any program, by clicking on its icon or by typing ``RStudio’’ at the Windows Run prompt.

Four primary panes of RStudio User interface - from https://docs.posit.co/ide/user/ide/guide/ui/ui-panes.html

R Packages

  • Extensive collection of packages designed for statistical analysis.

  • Dynamic and reproducible documents with R Markdown.

  • Tidyverse \(\leftarrow\) A collection of R packages designed for data science

    • Data visualization with ggplot2
    • Data transformation with dplyr
    • Data tidying with tidyr
    • Data import with readr, readxl
    • Manipulate lists with purr
    • String manipulation with stringr
  • Interactive web apps with Shiny

  • \(\cdots\)

Install and Preload Packages in R – Example

Install Packages

  1. Run Install.packages("the-package-name")

Example Install package tidyverse.

install.packages("tidyverse")

Note that, R Markdown is automatically installed and loaded in RStudio IDE. So you don’t need to explicitly install rmarkdown package if using R Studio.

  1. Click Tools \(\rightarrow\) Install Packages \(\rightarrow\) Type in package name and install.
  2. Refer to the pane that has the tabs Files, Plots, Packages. Click on the tab Packages, and then click on the button Install to install the package needed.

Load Packages

Example After installation, load package tidyverse.

library(tidyverse)

You only need to install a package once (like buying a tool from the hardware store and adding it to your toolbox), but you need to reload it every time you start a new session (like grabbing the specific tool you need for the task at hand).

Example

Try the following codes in the R console.

cheddar # The data set cannot be found, as the package "faraway" is not installed and/or loaded.
# install.packages("faraway") # Install the package
library(faraway) # Load the package
cheddar # Now you can use the dataset

### You may specify a dataset from a package by using
faraway::cheddar

Creat .R and .Rmd file

  • Create an R script file (with extension .R) by clicking File \(\rightarrow\) New File \(\rightarrow\) R Script. (Shortcut is Ctrl + Shift + C (windows), command + shift + N (Mac))

  • Create an R Markdown file (with extension .Rmd) by clicking File \(\rightarrow\) New File \(\rightarrow\) R Markdown.

R Markdown File

The following workflow is from R Markdown Cheat Sheet

Install TinyTex for PDF output

If you want to generate PDF output, you will need to install TinyTex. Please run the following codes in the R Console.

install.packages('tinytex')
tinytex::install_tinytex()  # install TinyTeX

Some Useful Tips

  • Use Ctrl + L to clear Console (windows), Cmd + L (Mac)
  • Default comment/uncomment codes selection is Ctrl + Shift + C (windows), Command + Shift + C (Mac). You may change the shortcut to Ctrl + /.

(You can do so by clicking Tools \(\rightarrow\) Modify Keyboard Shortcuts \(\rightarrow\) Search “comment” \(\rightarrow\) Change the shortcut \(\rightarrow\) Apply.)