R and RStudio Setup at Kellogg


 

The combo of the open-source statistical analysis software R and the companion commercial software RStudio Desktop is increasingly being used in industry and in the Kellogg curriculum. To get up and running with this powerful system, you can follow our guidance below.

Contents

Installing R

Installing RStudio Desktop

To download, go to the product page for RStudio Desktop, click Download RStudio Desktop. On the Choose Your Version page, click Download under the RStudio Desktop Open Source License column. This should autodetect your operating system (Windows or Mac) and give you the right installer for your computer. If it doesn’t, you can choose the correct installer for your respective OS under All Installers.

After the installer is downloaded, run it and accept all defaults.

Installing Packages

In addition to using built-in R functionality, it is likely you will need additional R packages. This can easily be done by typing the following into the RStudio console: install.packages('package'), where ‘package’ is the package name in quotations.

After installing the package, you need to tell R that you will be using the package by typing in:  library(package) in your file, where package is the package name. Note you only need to perform the installation command install.packages('package') once, but you need to call library(package) every time you restart R and want to use that package.

Installing TinyTeX for creating (“knitting”) PDFs

You’ll likely be doing your coursework in RMarkdown, a document composition language that lets you combine your R code, its text and graphical output, and your expository text all in one text-based file. This can be converted into PDF in RStudio using the TinyTeX package, which will then allow you to portably share your document for homework submission and collaboration.

To install TinyTeX:

  1. Launch RStudio and place your cursor in the console window, as in the figure.

In the console, enter the appropriate commands for your operating system:
Windows: Enter these two commands, pressing Enter after each.

install.packages('tinytex')

tinytex::install_tinytex(force = TRUE, repository = 'http://mirror.mwt.me/ctan/systems/texlive/tlnet')


Mac OS: Enter these two commands, pressing Enter after each:

install.packages('tinytex')

tinytex::install_tinytex(force = TRUE, dir='~/.TinyTex', repository = 'http://mirror.mwt.me/ctan/systems/texlive/tlnet') # That's a tilde, not a dash

  1. Installation will take some time, perhaps 10 minutes. Some initial software (10 programs) will be downloaded and installed and then a number of LaTeX packages (78 packages). As the command executes, pay attention. Two error messages might pop up. You can click to dismiss them. When the installation completes, you will see a message like this in the console:

TinyTeX installed to C:\Users\John Smith\AppData\Roaming/TinyTeX

Please restart your R session and IDE (if you are using one, such as RStudio or Emacs) and check if tinytex:::is_tinytex() is TRUE.

  1. Restart RStudio. In the console, type this command and press Enter: tinytex:::is_tinytex()

The output should be TRUE. If so, continue to the next section. If not, please contact us and send a screenshot of your console.

Resources

The following are free online resources for getting started with and mastering R. They are roughly sorted from introductory to expert.

Most Common Issues

There are a few places where installation can run into issues. These are largely platform-specific.

Windows

1. Packages fail to import

The most common issue on Windows is that packages fail to import after installation if your Documents folder is synced with OneDrive. This happens because R stores packages in the Documents folder, and OneDrive interferes.

This is fixed by setting the R_LIBS_USER environment variable to a folder that is not synced by OneDrive.

This can be done by running this script or by following these steps:

  1. Open the Run window (Win + R)
  2. Paste rundll32 sysdm.cpl,EditEnvironmentVariables and hit OK.
  3. Click New… under “User variables” (the top panel)
  4. Paste variable name R_LIBS_USER
  5. You can use Browse Directory… to choose any folder that is not synced with OneDrive (eg. C:\Users\username\R)

Now that you changed your library directory, you’ll need to reinstall your libraries.

Mac

1. TinyTeX installation fails

By and large, the most common issue on the Mac shows up with the following error code at the end of the TinyTeX installation:

`add_link_dir_dir: destination /usr/local/bin not writable, no links from /Users/username/Library/TinyTeX/bin/x86_64-darwin. add of symlinks had 1 error(s), see messages above. add of symlinks had 1 error(s), see messages above. tlmgr: An error has occurred. See above messages. Exiting.`

You can fix this by doing the following:

  1. You should have tab called “Terminal” next to your Console tab in the lower right corner of your RStudio window. Click on this Terminal tab.
  2. Paste in the command following command and press Enter: sudo chown -R `whoami`:admin /usr/local/bin
  3. When it asks for your password, type in the password you use to get into the computer (it may be your Apple ID password). The password prompt will not show any placeholder characters/asterisks.
  4. Press Enter to finish.
  5. When successful, it will say nothing and show the Terminal prompt again.
  6. Re-run the TinyTeX installation in the R Console tab using the command tinytex::install_tinytex(force = TRUE, dir='~/.TinyTex', repository = 'http://mirror.mwt.me/ctan/systems/texlive/tlnet')