# Installing R, RStudio, R Markdown, Latex

Supplement to pol346

Team 346 pol346.com (Department of Politics, Princeton University)http://princeton.edu/politics
2020-05-19

For POL346, we require students shift away from word processors like Microsoft Word and use a workflow that involves R and a formatting language called R Markdown (as is done in POL345). The two languages are glued together with another language called knitr. Certain kinds of advanced typography, like formulas, rely on yet another language called Latex. Installing and configuring all of this will impose a modest learning curve at the start but will make life much easier down the road.

The basic advantage of R + R Markdown + knitr + Latex is that you can easily braid together your prose, R code, formulas and R output (e.g., plots and tables) in a single document. This not only saves time and makes life easier by reducing the number of files that need to be managed, it also helps with creating better research such as when you write JPs or Senior Theses (we’ll discuss this more in class).

To get set up with R + R Markdown + knitr + Latex, please do the following before precept:

## If necessary, install LaTeX

• If you did not install a suite of software called TeX in POL345, you’ll need to do that, too. If you did install either TinyTeX, MacTeX or MikTeX (or something similar) you can skip this step. Latex is the engine that helps with advanced typography, in cases like formulas or tables, or when converting documents into PDFs. You will only need to learn a little of the TeX formatting language to get started. The TeX downloads are large but, if your computer has sufficient storage, we recommend the complete versions.

## TinyTeX

• Both Mac and Windows
1. Open RStudio
2. Run the following commands in RStudio:
• install.packages('tinytex')
• tinytex::install_tinytex()
3. To test if the installation worked, see Test your installation below
4. If you have issues or questions about TinyTex, see:

## Alternate ways to install LaTeX

• Once you have installed TeX, if you already had RStudio open, quit and then re-open it.

• Once you have installed R, RStudio, knitr, and TeX and restarted RStudio, then, in RStudio do the following to compile a test R Markdown document:

• Create new document

• File $$\rightarrow$$ New File $$\rightarrow$$ R Markdown
• A pop-up window will appear. Give the document a title.
• Click on the Knit icon (it’s a ball of blue wool with a knitting needle in it) and select Knit to HTML
• Save the document when prompted
• After saving a formatted HTML document should appear
• Also try clicking on the Knit icon again and select Knit to PDF
• If all goes well a PDF document will be created and you’re all set!

## Troubleshooting

• This section is a work-in-progress The instructions below have worked for some students but not others. They will continue to be revised and improved as we identify additional solutions.

• Please email me ( ) any errors you encounter so that this can be refined for future students.

## Mac

1. Hit command-space, type Terminal to open the Terminal app

2. Copy and paste the following text in to the command line prompt in the Terminal app and hit return


sudo chown -R whoami:admin /usr/local/bin
1. Copy and paste the following text in the Terminal app and hit return.

~/Library/TinyTeX/bin/x86_64-darwin/tlmgr path add
1. Try to run the following commands in R:

install.packages('tinytex')
tinytex::install_tinytex(force = TRUE)

## Windows

1. Attempt to uninstall MikTek
1. Try to run the following commands in R

install.packages('tinytex')
tinytex::install_tinytex(force = TRUE)