Installing R and libaries

Install R, RStudio, and useful libraries/packages.

Table of Contents


Get source code for this RMarkdown script here.

If you’re new to R, follow the instructions below to install R and RStudio. Otherwise, check out my tutorials and articles.

Click here to download everything on this site as a zip folder.

Alternatively, you can also clone or fork the Github repository for this site. On the Github page, click the green Clone or download button at the top right of the page to download everything you can find on this site. The RMarkdown scripts used to generate all the tutorials and posts have filenames that end with .Rmd.

Download everything from GitHub

Consider being a patron and supporting my work?

Donate and become a patron: If you find value in what I do and have learned something from my site, please consider becoming a patron. It takes me many hours to research, learn, and put together tutorials. Your support really matters.

Installing (or updating) R and RStudio

  1. Download R here. Follow the instructions under Getting Started to download R. Choose a mirror/location that is closest to you and download R for Linux, Mac, or Windows.
  1. Download RStudio here (the FREE version).

  2. After installing both R and RStudio, run RStudio. We usually never use R itself and always run RStudio (RStudio relies on R). R is like your new but empty home (without furniture and quite unlivable unless you’re a real minimalist) and RStudio are like furniture and decorations added to your house (R), making your house much more livable and even pretty. Thus, RStudio is useless without R, just like furniture and decorations are useless if you don’t already have a house.

Updating R and RStudio once every few months

You should check whether there are newer versions of R and RStudio at least once every few months and install them if available. It’s good practice to always use the latest versions. Just go to their websites and download and re-install them again.

Installing R packages

Here’s how to install R packages (also called libraries) that extend R’s basic functionality. Below are a few packages I use regularly. Make sure you have the latest version of R and RStudio. To install packages, run the following code in RStudio. You only need to install any given package once (but remember to update them when newer versions are available).

I also have my own package hausekeep, which has functions I use frequently.

which will also try to install a few of these packages if you don’t already have them). To install all of them, first

You can install one package at a time or multiple at once. I suggest installing one at a time because it can be can be error-prone if you try to install too many packages at once because the warning/error messages can become difficult quite abstruse (if there are any).

Comments are preceded by the # symbol in R, and you can use # or #’ for multi-line comments. R won’t run any code that’s preceded by the # symbol.


# install packages all at once using c(), which allows you to combine/concatenate stuff
install.packages(c("tidyverse", "data.table", 
                   "lme4", "lmerTest", 
                   "ggbeeswarm", "egg"))

# install hausekeep package from my github
# install.packages("devtools")
devtools::install_github("hauselin/hausekeep") # you might have to install devtools first (see above)

#' or you can install one package at a time
#' I have commented out (added a # sign in front of a line)
#' the line of code below so I won't run it accidentally,
#' or it's to tell myself or others who are reading my code,
#' that I usually don't intend to run this line of code
#' unless I really want to.
# install.packages("tidyverse")
# install.packages("data.table")

Every time I start a new R session in RStudio, I usually run the code below at the top of my R script to load all these libraries all at once using library().


library(tidyverse); library(data.table);
library(lme4); library(lmerTest); 
library(ggbeeswarm); library(egg); library(hausekeep)

More about these packages

Support my work

Support my work and become a patron here!

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/hauselin/rtutorialsite, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".