UNR Logo

Installing RStudio and TeXstudio

Below are instructions to install RStudio and TeXstudio so that (1) you can use R and create professional looking documents using LaTeX, and (2) so that both RStudio and TeXstudio can integrate R code into a LaTeX document and have that code and/or it's output appear in the document without any additional formatting by you! This will also allow you to use R Markdown to make high quality R documents without the learning curve associated with LaTeX.

Please install this software in the given order: Rstudio and TeXstudio need R and your TeX distribution (e.g. MikTeX or TeX Live) to be installed first if you'd like to compile R Markdown documents as well as knitr documents.

Note: For now, this only has instructions for Windows. It should be similar on OS X and Linux, so let me know if you try! Mac Users: If you prefer to stick with TeXShop instead of TeXstudio, you can configure TeXShop to work with knitr as well. See instructions at https://yihui.name/knitr/demo/editors/.

Install R, a LaTeX distribution, then RStudio and TeXstudio

  1. Install either MikTeX (Windows) or TeX Live (Mac OS X) via the MacTeX distribution, or (recommended!) just install BasicTeX (the light version of MacTeX that can automatically download missing packages later; only 110 megabytes instead of 2 gigabytes) and Ghostscript (look for the Ghostscript.pkg link here).
  2. Install R (www.r-project.org).
  3. Once installed, run the command install.packages("knitr"). Better yet, install the all of these packages (recommended):

    install.packages(c("bbmle", "bipartite", "bitops", "car", "caret", "coda", "colorspace", "cowplot", "data.table", "deBInfer", "deSolve", "devtools", "dichromat", "digest", "emdbook", "forecast", "GenSA", "glmnet", "googlesheets", "googleVis", "gtable", "igraph", "KernSmooth", "knitr", "labeling", "lubridate", "manipulate", "maps", "maptools", "markovchain", "mime", "multcomp", "munsell", "mvtnorm", "optimx", "plyr", "poweRlaw", "proto", "qcc", "randomForest", "RColorBrewer", "Rcpp", "RCurl", "reshape2", "rgl", "rmarkdown", "RMongo", "RMySQL", "RODBC", "rootSolve", "roxygen2", "RPostgreSQL", "RSQLite", "scales", "scatterplot3d", "seas", "shiny", "sp", "sqldf", "stargazer", "swirl", "tidyverse", "vegan", "vcd", "XML", "xtable", "xts", "zoo"), repos='http://cran.us.r-project.org')
  4. Install RStudio (www.rstudio.org) and then TeXstudio (www.texstudio.org; Mac users may prefer to also install TeXShop (an alternative to TeXstudio), and maybe LaTeXiT.)

Configure TeXstudio to use R + knitr

  1. Go to Options > Configure TeXstudio > Build and edit the "User Commands" to read
    "C:\YOUR-R-DIRECTORY\bin\x64\Rscript.exe" -e "knitr::knit2pdf('%.Rnw')"
    Mac Users: use this instead:
    R -e "knitr::knit2pdf('%.Rnw')" | txs:///pdflatex | txs:///view-pdf-internal
    Note: the long filename for Rscript.exe needs to reflect where R files are located on your computer!
    See also http://yihui.name/knitr/demo/editors/



  2. Configure a Keyboard Shortcut to compile knitr (*.Rnw) documents:

    Once the User Command above is defined, you can assign a keyboard shortcut to quickly compile knitr (*.Rnw) files. I prefer F4 to the default of Alt-Shift-F1 (TeXstudio recently changed the default shortcut to "Build & View" a LaTeX (*.tex) document from F1 to F5, hence my preference for F4 to build knitr (*.Rnw) documents). Another alternative might be Shift+F5. (Note that F7 will display the PDF, once compiled.) To do this...

    Go to Options > Configure TeXstudio > Shortcuts > Tools > User.

    Click the second column ("Current Shortcut") entry for knitr then scroll up to F4 or hit the F4 key (typing "F" then "4" will not work!). Optional: Click the 3rd column (Additional Shortcut) and set it to Shift+F5.

    Now save, and you should be all set!
  3. To test it out, download this knitr example and try to compile it by hitting F4: homework-knitr-template.Rnw

Troubleshooting:

  1. Make sure MikTex (or whatever LaTeX software you installed) works properly. Once you can compile *.tex documents in TexStudio, THEN debug problems with *.Rnw documents.
  2. You may need to manually add the MikTeX path to your user environment.
  3. You may be prompted to install a missing LaTeX package when compiling Rnw documents. Unfortunately, it will do this one package at a time until they're all installed, so keep recompiling over and over again until all the required packages install. You'll only need to go through this process once!
  4. Make sure you have installed the knitr package in R before you try to compile *.Rnw documents in TeXstudio. Better yet, make sure you can compile an R Markdown document to PDF in R Studio.

Dark Color Theme for TeXstudio Editor

Instructions for installing a pre-configured dark theme for the TeXstudio editor:
  1. Download a dark color theme:
    1. My dark theme: Texstudio-dark-Hurtado.txsprofile
    2. Various examples at http://tex.stackexchange.com/questions/108315/how-can-i-set-a-dark-theme-in-texstudio.
    3. My dark theme plus some editor modifications (e.g. supressed underline of grammar mistakes, etc.): Texstudio-darkplus-Hurtado.txsprofile
  2. Open up the color theme in a text editor (I prefer notepad++).
  3. Next come the "not-so-user-friendly" steps, so let me walk you through them!
    1. Go to Options > Save profile... and save a backup of the current profile to something like user-default.txsprofile.
    2. Do this again, but instead save to a file named something like custom-dark1.txsprofile. Note the directory this file is saved it!
    3. Open custom-dark1.txsprofile (or whatever you named it) with a text editor (again, I like notepad++) and scroll to the very bottom which should look like
      ...
      [formats]
      version=1.0
      
      Note these are the first two lines of the dark theme file you downloaded! Copy and paste the contents of the dark them to this section of custom-dark1.txsprofile file and save. The [formats] section of custom-dark1.txsprofile should now read
      ...
      [formats]
      version=1.0
      data\normal\priority=-1
      data\normal\bold=false
      ...
      
  4. Go to Options > Load profile... and load the newly edited custom-dark1.txsprofile.
  5. Restart TexStudio and open a file, and you should see the dark themed editor!
  6. Finally, you can further modify your color scheme by going to Options > Configure TeXstudio > Syntax Highlighting