Getting Started with R


1 New to R?

If you are completely new to R, we recommend watching at least the first couple of videos from OSU’s Mike Sovic’s Youtube playlist of short videos on R, to get started.

Here are some additional resources for learning the basics of R:

Also, don’t hesitate to reach out to the Code Club organizers if you have any questions!


2 Miscellaneous R tips

2.1 Useful settings

By default, R will try to save your “environment” (e.g., your loaded data, variables, etc) when you exit, and then reload everything the way it was upon restarting R. However, this is bad! You should always be able to reproduce your environment given a set of commands saved in an R script or R Markdown document, whereas saving and reloading your environment encourages you to be sloppy about this.

To disable this in RStudio, go to Tools > Global Options > General and set the options as follows:


2.2 Installing R packages

CRAN packages

To install an R package that is available at CRAN, the default R package repository, from within R (e.g. in the R console in RStudio), use the install.packages() function.

The install.packages() function will handle dependencies within R — i.e., it will install other R packages that your package depends on. Occasionally, when the install function needs to compile a package from source, errors arise that relate to missing system dependencies (i.e. software outside of R).

On Mac and Linux, these system dependencies are best installed outside of R, such as with homebrew on Mac or apt on Ubuntu. The errror message you got when trying to install an R package should tell you which system dependencies are needed.

On Windows, you can use the installr package to install such dependencies or other software from within R — for example:

install.packages("installr")    # Install the installr package first
installlr::install.RStudio()    # Install RStudio
installr::install.python()      # Install Python

Installing packages from Github

To install a package from Github, use either the devtools or the remotes package – for example:

install.packages("remotes")                # Install the remotes package
remotes::install_github("kbroman/broman")  # Install from a repository using "<username>/<repo-name>"

This will install the package from source, so you will need to make sure you are able to do so by following the instructions in the section right above this one.

Installing packages from Bioconductor

If you’re doing bioinformatic analyses in R, you will probably run into packages that are not on CRAN but on Bioconductor. To install a package from Bioconductor, use the BiocManager package – for example:

install.packages("BiocManager")  # Install the BiocManager package
BiocManager::install("edgeR")    # Install the edgeR package from Bioconductor


2.3 Updating R

Consider updating R if you have an older version of R installed. As of August 2023, we would recommend to update R if your version is below 4.1.

You can check which version of R you have by looking at the first lines of output when running the following command inside R:

sessionInfo()

To update:

  • Windows: You can update R from within R. The updateR() function will also take care of updating your packages:

    install.packages("installr")
    installr::updateR()
  • Mac: Download and install the latest .pkg file as if you were installing it for the first time.


Re-installing your packages after updating (Mac and Linux)

While the installr::updateR() function for Windows users takes care of reinstalling your packages along with updating R, Mac and Linux users will have to manually re-install their packages. Some people prefer to re-install these packages on the fly, which can end up being a way to get rid of packages you no longer use.

But if you want immediately reinstall all your packages, run this before you upgrade:

my_packages <- installed.packages()
saveRDS(my_packages, "my_packages.rds")

Then, after you’ve installed the latest R version:

my_packages <- readRDS("CurrentPackages.rds")
install.packages(my_packages[1, ])

This will only work for packages available on CRAN. Of course, you can check your list for Github-only and Bioconductor packages and then install those with their respective commands (see below). Yes, this can be a bit of a hassle!

Back to top