knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  error = TRUE,
  message = FALSE,
  eval = TRUE,
  echo = TRUE
)

Setting Up R and RStudio


Author: Jimmy Briggs
Date: June 2, 2020


The purpose of this guide is to provide an in-depth walkthrough of the various tasks, installations, and processes involved with setting up your machine for using R.

Installing R & RStudio

The first step is to install R and RStudio from their respective websites listed below.

Download R

Install R from the main CRAN (Comprehensive R Archive Network) website: https://cran.r-project.org/.

Additional Notes on R

For more details see the section discussing how to efficiently migrate R packages between versions in this guide.

Download RStudio

Install the free Version of RStudio Desktop for Windows from the RStudio Website here: https://rstudio.com/products/rstudio/download/.

Additional Notes on RStudio


Configure RStudio Settings

A range of Project Options and Global Options are available in RStudio from the Tools menu (accessible from the keyboard via Alt+T).

Most of these are self-explanatory but it is worth mentioning a few that can boost your programming efficiency:

Unticking this default prevents loading previously created R objects. This will make starting R quicker and reduce the chance of getting bugs due to previously created objects. For this reason, I recommend you untick this box.

Alternatively you can simply run this code:

require(usethis)
usethis::use_blank_slate(scope = "user")

See code{?usethis::use_blank_slate for more information.

Upgrading R

Sometimes people run into difficulties whenever R updates its latest version. One should be cautious whenever the Major patch version of R is upgraded (i.e. from R3.6.1 to R4.0.0).

For minor patch updates (i.e. R3.6.3 to R3.7.0 or R3.6.4), it is usually a good idea to update your local version as these updates incorporate bugfixes and speed enhancements.

On major updates, it is best practice to keep your old version of R on your machine until you are comfortable with the updated R version.

A very useful feature of RStudio is that you can have multiple versions of R installed on your computer at the same time and by holding down the Control key during the launch of RStudio you can cause the R version selection dialog to display at startup.

When migrating R versions between major patch updates, you will need to re-install you library of R packages. This process can be vastly expedited utilizing installr's helpful installr::copy.packages.between.libraries() function.

Installing and Setting Up Additional Software and Utilities

To get the most out of R and RStudio it is helpful to install these additional software resources:

Here's the code to install these additional resources:

if (!require(pacman)) install.packages("pacman")

pacman::p_load(devtools, 
               installr, 
               tinytex, 
               rstudioapi, 
               magrittr,
               dplyr,
               pkgbuild)

# configure RStudio settings ----------------------------------------------

# disable reloading of workspace between sessions
usethis::use_blank_slate(scope = "user")

# review system environment variables:
Sys.getenv()

# configure your R library path for R packages
.libPaths()

# copy packages to new R-version's windows library
libdir_prior <- file.path("<enter prior win-library path here>")
libdir_current <- file.path("<enter current win-library path here>")
installr::copy.packages.between.libraries(
  from = libdir_prior, to = libdir_current
)

# check
.libPaths()[1] == libdir 

# configure dotfiles .Rprofile & .Renvrion --------------------------------

# review dotfiles
usethis::edit_r_environ(scope = "user") # (RTools Path, github PAT, keys, etc.)
usethis::edit_r_profile(scope = "user") # (various options for packages)

# additional software ---------------------------------------------
pkgbuild::setup_rtools()

# Rtools
installr::install.rtools()
rstudioapi::restartSession()

# git
installr::install.git()
rstudioapi::restartSession()

# tinytex
tinytex::install_tinytex()
rstudioapi::restartSession()
tinytex::use_tinytex()

# java
installr::install.java()

# pandoc
installr::install.pandoc()

# node.js (only if desired)
installr::install.nodejs()

# github Git Client (only if desired)
installr::install.github()

# inno (only if desired)
installr::install.inno()

Note that not all of these are required, however, I recommend at a minimum to install RTools, Git, Pandoc, and a Latex service.

I have provided an R script named R-Setup-Script.R with this guide that uses the installr package to help ease the process of installing all these extra resources.

Advanced Configuration

This section discusses more advanced R related configurations such as:

For more advanced R developers you may want to further configure your development environement by customizing you R related dotfiles; specifically, your .Rprofile and .Renviron.

Here is what my minimal setup includes:

Additionally, you can configure keybinding for RStudio addins from RStudio and store them within the .R folder located in your R_USER path. To view this path run Sys.getenv("R_USER").

On a windows computer, you may need to adjust where the system chooses to look for various R related items on your system. For example, the path to your RTools bin executable, your HOME path, your library path for packages, and many other windows specific paths. Note the difference between SYSTEM PATHS and USER PATHS.

To add your system RTOOLS PATH to your .Renviron (easier than manually configuring within windows system settings) run the code:

cat('PATH = ${RTOOLS40_HOME}\\usr\\bin;${PATH}',
    file = fs::path(Sys.getenv("R_USER"), "/.Renviron"), 
    append = TRUE)

Memory in R

R breaks down memory usage into Vcells (memory used by vectors) and Ncells (memory used by everything else).

However, neither this distinction nor the "gc trigger" and "max used" columns are typically important. What we're usually most interested in is the the first column: the total memory used.

A helpful function one can use for this is pryr::mem_used which wraps around gc() to return the total amount of memory (in megabytes) currently used by R. pryr::object_size() is another helpful function which works similarly to utils::object.size() but counts more accurately and includes the size of environments. pryr::compare_size makes it easy to compare the output of object_size and object.size.



jimbrig/rtraining documentation built on March 12, 2023, 12:57 a.m.