install.packages("tidyverse")
R, RStudio, and R Packages
Download and Install R and RStudio
Visit the website, download and install R and RStudio (selecting the appropriate version for your OS) https://posit.co/download/rstudio-desktop/
Alternatively, you may use Posit Cloud: https://posit.cloud/ to try all the functions introduced below. [FYI, the Cloud Free plan of Posit Cloud allows up to 25 compute hours per month. For more details, please visit https://posit.cloud/plans/free.]
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
Popular R packages for data science
- R is a powerful tools because tons of R packages are used to extend the capabilities of base R.
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
- Run
Install.packages("the-package-name")
Example Install package 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.
- Click Tools \(\rightarrow\) Install Packages \(\rightarrow\) Type in package name and install.
- Refer to the pane that has the tabs
Files, Plots, Packages
. Click on the tabPackages
, 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.
# The data set cannot be found, as the package "faraway" is not installed and/or loaded.
cheddar # install.packages("faraway") # Install the package
library(faraway) # Load the package
# Now you can use the dataset
cheddar
### You may specify a dataset from a package by using
::cheddar faraway
Creat .R and .Rmd file
Create an R script file (with extension .R) by clicking
File
\(\rightarrow\)New File
\(\rightarrow\)R Script
. (Shortcut isCtrl + 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')
::install_tinytex() # install TinyTeX 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 toCtrl + /
.
(You can do so by clicking Tools
\(\rightarrow\) Modify Keyboard Shortcuts
\(\rightarrow\) Search “comment” \(\rightarrow\) Change the shortcut \(\rightarrow\) Apply.)