Welcome

This tutorial will help you set up your computer to use R. It is for you if you need to:

  • Install R on your computer
  • Install the RStudio IDE
  • Install the tidyverse R package

You can skip this tutorial if you've already done these things.

Is this tutorial for you?

Do you need to work through the tutorial? Take the quiz below to find out.

Install R

How to install R


  1. Go to cran.r-project.org

  2. Click on one of the 3 options presented to you on cran-r website that matches your operating system:
  3. What you do next will depend on your operating system.
    • (Mac) OS X users:
      • Click on "Download R for (Mac) OS X".
      • Choose the adapted version for your Mac OS X.
      • After downloading, double click on the .pkg file.
      • A window will open and click on "Continue" every time until the complete installation.
    • Windows users:
      • Click on "Download R for Windows".
      • Then click on "base".
      • Choose the current version available and follow the instructions.
    • Linux users:
      • Click on "Download R for Linux".
      • Then select the right distribution
      • Follow the distribution specific instructions to install R.

Test your knowledge

Quiz - Install R

Install RStudio

How to install RStudio

RStudio is an Integrated Development Environment for R. What does that mean? Well, if you think of R as a language, which it is, you can think of RStudio as a program that helps you write and work in the language. RStudio makes programming in R much easier and I suggest that you use it!


  1. Go to https://www.rstudio.com/products/rstudio/download/.

  2. Go to the "Installers for Supported Platforms" section at the end of the page and click on the Rstudio right version for your computer.

  3. The download will start. After the downloading is complete, install the application by following the instructions.

  4. Once the installation is complete, open the application as any others.

Test your knowledge

Quiz - Install RStudio

Install Packages

How to install R packages


When you install by yourself R and Rstudio on your computer, you will need some packages to do your analysis.

Many useful R function come in packages, free libraries of code written by R's active user community. To install an R package, open an R session and type in your console the line of code.

install.packages("<the package's name>")

R will download the package from CRAN, so you'll need to be connected to the internet. Once you have a package installed, you can make its contents available to use in your current R session by running

library("<the package's name>")

Test your knowledge

Quiz - Working with Packages

R Packages

There are thousands of helpful R packages for you to use, but navigating them all can be a challenge. To help you out, the Rstudio team compiled this guide to some of the best. These packages are some of the top most downloaded R packages.

To load data

install.packages("DBI")

DBI - The standard for for communication between R and relational database management systems. Packages that connect R to databases depend on the DBI package.

install.packages("odbc")

odbc - Use any ODBC driver with the odbc package to connect R to your database. Note: RStudio professional products come with professional drivers for some of the most popular databases.

install.packages("RMySQL")
install.packages("RPostgresSQL")
install.packages("RSQLite")

RMySQL, RPostgresSQL, RSQLite - If you'd like to read in data from a database, these packages are a good place to start. Choose the package that fits your type of database.

install.packages("XLConnect")
install.packages("xlsx")

XLConnect, xlsx - These packages help you read and write Micorsoft Excel files from R. You can also just export your spreadsheets from Excel as .csv's.

install.packages("foreign")

foreign - Want to read a SAS data set into R? Or an SPSS data set? Foreign provides functions that help you load data files from other programs into R.

install.packages("haven")

haven - Enables R to read and write data from SAS, SPSS, and Stata.

R can handle plain text files – no package required. Just use the functions read.csv, read.table, and read.fwf. If you have even more exotic data, consult the CRAN guide to data import and export.

For more information about using R with databases see db.rstudio.com.

To manipulate data

install.packages("dplyr")

dplyr - Essential shortcuts for subsetting, summarizing, rearranging, and joining together data sets. dplyr is our go to package for fast data manipulation.

install.packages("tidyr")

tidyr - Tools for changing the layout of your data sets. Use the gather and spread functions to convert your data into the tidy format, the layout R likes best.

install.packages("stringr")

stringr - Easy to learn tools for regular expressions and character strings.

install.packages("lubridate")

lubridate - Tools that make working with dates and times easier.

To visualize data

install.packages("ggplot2")

ggplot2 - R's famous package for making beautiful graphics. ggplot2 lets you use the grammar of graphics to build layered, customizable plots.

install.packages("ggvis")

ggvis - Interactive, web based graphics built with the grammar of graphics.

install.packages("rgl")

rgl - Interactive 3D visualizations with R

htmlwidgets - A fast way to build interactive (javascript based) visualizations with R. Packages that implement htmlwidgets include:

install.packages("leaflet")
install.packages("dygraphs")
install.packages("DT")
install.packages("diagrammeR")
install.packages("network3D")
install.packages("threeJS")
  • leaflet (maps)
  • dygraphs (time series)
  • DT (tables)
  • diagrammeR (diagrams)
  • network3D (network graphs)
  • threeJS (3D scatterplots and globes).
install.packages("googleVis")

googleVis - Let's you use Google Chart tools to visualize data in R. Google Chart tools used to be called Gapminder, the graphing software Hans Rosling made famous in hie TED talk.

To model data

install.packages("car")

car - car's Anova function is popular for making type II and type III Anova tables.

install.packages("mgcv")

mgcv - Generalized Additive Models

install.packages("lme4")
install.packages("nlme")

lme4/nlme - Linear and Non-linear mixed effects models

install.packages("randomForest")

randomForest - Random forest methods from machine learning

install.packages("multcomp")

multcomp - Tools for multiple comparison testing

install.packages("vcd")

vcd - Visualization tools and tests for categorical data

install.packages("glmnet")

glmnet - Lasso and elastic-net regression methods with cross validation

install.packages("survival")

survival - Tools for survival analysis

install.packages("caret")

caret - Tools for training regression and classification models

To report results

install.packages("shiny")

shiny - Easily make interactive, web apps with R. A perfect way to explore data and share findings with non-programmers.

install.packages("rmarkdown")

R Markdown - The perfect workflow for reproducible reporting. Write R code in your markdown reports. When you run render, R Markdown will replace the code with its results and then export your report as an HTML, pdf, or MS Word document, or a HTML or pdf slideshow. The result? Automated reporting. R Markdown is integrated straight into RStudio.

install.packages("xtable")

xtable - The xtable function takes an R object (like a data frame) and returns the latex or HTML code you need to paste a pretty version of the object into your documents. Copy and paste, or pair up with R Markdown.

For Spatial data

install.packages("maps")

maps - Easy to use map polygons for plots.

install.packages("ggmap")

ggmap - Download street maps straight from Google maps and use them as a background in your ggplots.

For Time Series and Financial data

install.packages("zoo")

zoo - Provides the most popular format for saving time series objects in R.

install.packages("xts")

xts - Very flexible tools for manipulating time series data sets.

install.packages("quantmod")

quantmod - Tools for downloading financial data, plotting common charts, and doing technical analysis.

To write high performance R code

install.packages("data.table")

data.table - An alternative way to organize data sets for very, very fast operations. Useful for big data.

References

This course uses the Tuto called How to: Install R and Rstudio, the Quick list of useful R packages by Rstudio.

Acknowledgments

To cite this course:

Warin, Thierry. 2020. “Nüance-R: R Nanocourses.” doi:10.6084/m9.figshare.11842416.v2.