Chapter 2 Getting Started
2.1 Installing R, R Studio, R Markdown, and Latex
For POL90, 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 some other stats classes). 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 class:
2.1.1 Install software and packages
Install R: http://www.r-project.org
Install RStudio: https://rstudio.com/products/rstudio/download/#download
Install course packages:
- Copy and paste the following command to your R Console:
# opens a script to install R packages, do not restart R
source("http://appliedstats.org/POL90_2022_packages.R")
- If prompted to restart
R
, click ‘No’ - If prompted with ‘Do you want to install from sources the package which needs compilation?’ type
n
and hitreturn
2.1.2 If necessary, install LaTeX
- If you did not install a suite of software called TeX in other stats classes, 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.
2.1.3 TinyTeX
- Both Mac and Windows
- Open
RStudio
- Run the following commands in RStudio:
- Open
install.packages('tinytex')
::install_tinytex() tinytex
- To test if the installation worked, see Test your installation below:
2.1.4 TinyTex Issues
- If, when installing TinyTex, you get an error like the following:
The directory /usr/local/bin is not writable. I recommend that you make it writable. See https://github.com/yihui/tinytex/issues/24 for more info.
Hit Command-Spacebar and type Terminal to OPEN the TERMINAL application
Once the Terminal application is open, copy and paste the following text into your Terminal and hit RETURN:
[ -e /usr/local/bin ] || sudo mkdir -p /usr/local/bin
- Again, copy and paste the following text into the TERMINAL application and hit RETURN (you may be prompted to type your password and it WILL NOT SHOW ANY CHARACTERS AS YOU TYPE THE PASSWORD)
sudo chown -R `whoami`:admin /usr/local/bin
- Finally, copy and paste this into the TERMINAL application
~/Library/TinyTeX/bin/*/tlmgr path add
Open RStudio
Copy and paste this command into the CONSOLE of RStudio (and hit RETURN)
::install_tinytex() tinytex
- See Test your installation below.
- If you run
tinytex::install_tinytex()
and get an error like this:
curl failed to verify the legitimacy of the server
Try the following slight modification:
tinytex::install_tinytex(version = "2021.10")
- For Windows users, if you get an error like this:
Error in install.packages : cannot open file 'C:/Users/.../OneDrive/Documents/R/win-library/4.1/file666459c6cb3/distill/help/figures/logo.png': Permission denied
- Try (1) Run
RStudio
as administrator and (2) Under thePackages
tab, clickInstall
and changeInstall to Library
option toC:/Program...../R/../library
and select the desired package to install. See more of this solution at this StackExchange answer: https://stackoverflow.com/a/50787084/893399
If you have other issues or questions about TinyTex, see: - https://yihui.org/tinytex/ - Also try emailing your error to omar.wasow@pomona.edu
2.1.5 Alternate ways to install LaTeX
- Mac:
- Lite: BasicTeX (~72MB): http://www.tug.org/mactex/morepackages.html
- Or, Complete, MacTeX (~3.2Gb): http://www.tug.org/mactex/index.html
- Windows:
- Lite, MikTeX (~194MB): http://miktex.org/download
- Or, Complete, TeX Live (~2Gb): http://www.tug.org/texlive/acquire-netinstall.html
2.1.6 Test your installation
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!
2.1.7 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 ( omar.wasow@pomona.edu ) any errors you encounter so that this can be refined for future students.
2.1.9 Mac
Hit
command-space
, type Terminal to open the Terminal appCopy 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
- Copy and paste the following text in the Terminal app and hit return.
~/Library/TinyTeX/bin/x86_64-darwin/tlmgr path add
- Try to run the following commands in R:
install.packages('tinytex')
::install_tinytex(force = TRUE) tinytex