Rstudio Latex

  

R and RStudio are both free, open-source software, available for all commonly used operating systems. R is developed cooperatively and noncommercially under the auspices of the Free Software Foundation; RStudio is a commercial product.

R and RStudio install in the standard manner on each of Windows, macOS, and Linux systems. System-specific instructions for installing R are given below. Regardless of your operating system, you should install R before installing RStudio.

How to monitor cluster resources with RStudio Server Pro, Launcher, and Kubernetes. Customizing LaTeX Options. Installing LaTex in RStudio. Luckily there is a very nice package that was created for the easy installation of LaTeX in RStudio! Type install.packages('tinytex') into the Console and press return to run the code. After that is complete, type tinytex::installtinytex into the Console and press return. This should take a bit of time depending.

If you wish to install the R Commander graphical user interface for R (used only in lecture 1), you may want also to consult the R Commander installation instructions (especially if you run into difficulties).

Please read and follow these instructions carefully. Installation assistance will also be availabile from the instructor (John Fox) and teaching assistant (Allison Leanage) prior to the start of thelecture series and during office hours.

Installing R on Windows

Visit the Comprehensive R Archive Network (CRAN) and select a mirror site; a list of CRAN mirrors appears at the upper left of the CRAN home page. I suggest that you use the 0-Cloud mirror, which is the first on the list. Click on the link Download R for Windows, which appears near the top of the page; then click on install R for the first time, and subsequently on Download R x.y.z for Windows (where x.y.z is the current version of R, which is R 4.0.2 at the start of the lectures series). Once it is downloaded, double-click on the R installer. You may take all of the defaults, but I suggest that you make the following modifications:

Instead of installing R in the standard location, C:Program FilesRR-x.y.z, I suggest that you use C:RR-x.y.z. Again, x.y.z is the current version of R. This will allow you to install packages in the main R library without running R with administrator privileges and may avoid problems that sometimes occur when there are spaces in paths.

In the Startup options screen, I suggest that you select Yes (customized startup). Then select the SDI (single-document interface) in preference to the default MDI (multiple-document interface); feel free to make other changes, but you may take all the remaining defaults.

Building Packages Under Windows, etc. (Optional)

If you wish to build packages, or use compiled C, C++, or Fortran code in R, or use the rstan package for Bayesian inference, you will have to install some additional software and properly configure your Windows system. You do not have to be able to build R packages in order to install pre-built Windows binary packages from CRAN, so these steps are generally unnecessary unless you plan to write your own packages, use compiled code, or use rstan. None of these topics are covered in the lecture series.

Click on the Rtools link on the R for Windows CRAN page. Download the current version of the Rtools installer and run it. You may take all of the other defaults. An additional necessary step is to add the Rtools usrbin subdirectory to your system path; for example, if Rtools is installed in c:rtoolsxy (which is the standard location for version xy of Rtools), then you would add c:rtoolsxyusrbin; to your system path. Type this location carefully, including the terminating semicolon -- you don't want to mess up your path.

An alternative, and possibly safer, procedure for specifying the path to Rtools is described on the Rtools webpage.

If you want to be able to build R packages outside of RStudio, also add c:RR-x.y.zbin; to the path (assuming that you installed R in the location that I suggested).

If you want to be able to build PDF help files for packages, download and install the MiKTeX LaTeX system; there is also a link to MiKTeX on the Building R for Windows page. Installing MiKTeX will also allow you to create Sweave and knitr LaTeX documents in RStudio, and to compile R Markdown documents directly to PDF files.

Installing R on macOS

Visit the Comprehensive R Archive Network (CRAN) and select a mirror site; a list of CRAN mirrors appears at the upper left of the CRAN home page. I suggest that you use the 0-Cloud mirror, which is the first on the list. Click on the link Download R for MacOS X, which appears near the top of the page; then click on R-x.y.z.pkg (where x.y.z is the current version of R -- R 4.0.2 at the start of the lectures series), which assumes that you are using macOS 10.11 (El Capitan) or higher. You'll also find older versions of R if you have an older version of macOS. Note: As a general matter, you're probably better off updating your macOS to the current version.

Once it is downloaded, double-click on the R installer. You may take all of the defaults.

Building Packages Under macOS, etc. (Optional)

If you wish to build packages, or use compiled C, C++, or Fortran code in R, or use the rstan package for Bayesian inference, you must install the Apple Xcode developer tools. None of these topics is covered in the lecture series. For macOS 10.7 (Lion) or higher, you can install Xcode for free from the App Store. For earlier versions of macOS, Xcode can be installed from your system DVD or downloaded from the Apple developer website. You do not need Xcode to install pre-built macOS binary packages from CRAN, so this step is unnecessary unless you plan to write your own packages, use compiled code, or use the rstan Bayesian estimation package.

Some R packages include Fortran, C, or C++ code; to build such packages, you will have to install compilers for these languages.The C and C++ compilers are included in the Apple Xcode tools, but you will have to separately download and install a Fortran compiler.

If you want to be able to build PDF help files, download and install the MacTeX LaTeX system. Installing MacTeX will also allow you to create Sweave and knitr LaTeX documents in RStudio, and to compile R Markdown documents directly to PDF files.

Installing X-Windows on macOS (Optional)

Some R software (e.g., my Rcmdr package) makes use of the Tcl/Tk graphical-user-interface (GUI) builder via the tcltk package to create point-and-click interfaces and to display GUI elements such as progress bars. To use the tcltk package, which is a standard part of the R distribution, you must have the X11 windowing system installed on your Mac. Some other packages that don't use Tcl/Tk, such as the rgl package for dynamic 3D graphics, also require X11.

Check to see whether the X11 windowing system (X Windows) has already been installed on your computer. If you wish, it should do no harm to skip this step and simply go to the next step to install XQuartz.

Chemical properties of argon. For OS X 10.6 and 10.7, the file X11.app should appear in the Utilities folder under Applications in the finder. This application should always be installed under OS X 10.7.

For OS X 10.8 or higher, the file is named XQuartz.app and is no longer included with the operating system. XQuartz.app may also be installed in OS X 10.6 or 10.7.

Note that if you upgrade macOS, you will have to reinstall XQuartz even if you installed it previously.

You may also issue the command capabilities('X11') at the R command prompt. If the response is TRUE then X11 is installed.

If neither X11.app nor XQuartz.app is installed, install XQuartz from http://xquartz.macosforge.org. As mentioned, it should do no harm to install XQuartz even if you have X11 currently installed.

    1. Download the disk image (dmg) file for XQuartz.

    2. When you open this file by double-clicking on it, you'll find XQuartz.pkg; double-click on it to run the installer, clicking through all the defaults.

    3. Important: After the installer runs, you'll have to log out and back on to your macOS account, or just reboot your Mac. Also, on first use, XQuartz builds a cache of fonts and so initial performance may be slow; this problem should go away after a short period of time.

Installing R on Linux Systems

Visit the Comprehensive R Archive Network (CRAN) and select a mirror site near you; a list of CRAN mirrors appears at the upper left of the CRAN home page. I suggest that you use the 0-Cloud mirror, which is the first on the list. Click on the link Download R for Linux, which appears near the top of the page. R is available for several Linux distributions (Debian, RedHat, SUSE, and Ubuntu); select your distribution, and proceed as directed.

If you have a Linux or Unix system that's not compatible with one of these distributions, you will have to compile R from source code; the procedure for doing so is described in the R FAQ (frequently asked questions) list.

Installing RStudio

Go to the RStudio download page, select the free version of RStudio Desktop, scroll down to Installers for Supported Platforms, and click on the link to the appropriate installer for your operating system (Windows, macOS, or Linux distro). Visit the RStudio IDE home page for more information about RStudio.

Once it is downloaded, run the RStudio installer and take all of the defaults: In Windows, double-click on the RStudio installer to start the installation; in macOS, double-click on the downloaded RStudio disk-image file, and drag the RStudio icon to the Applications folder.

When you first run RStudio, it should detect your R installation and start the R console. To configure RStudio to your taste, select Tools > Global Options (Windows) or RStudio > Preferences (macOS) from the RStudio menus. In particular, I suggest that on the General options screen you deselectRestore .RData into workspace at startup, and set Save workspace to .RData on exit to Never.

If you encounter difficulties, consult the RStudio troubleshooting guide. or seek help from John or Allison.

Installing R Packages for the Lecture Series

Once you have installed R and RStudio, you can install additional packages required for the lecture series by typing the following command at the > command prompt in the R Console (and pressing the Enter or return key):

install.packages(c('car', 'data.table', 'effects', 'knitr', 'lme4', 'rgl', 'rmarkdown', 'sfsmisc', 'tidyverse'))

You can simply copy and paste this command from these installation instructions. Alternatively, you can install packages from the RStudio Packages tab. Be aware that, depending on the speed of your internet connection, it may take some time to download and install these packages and their dependencies.

Rstudio

If you wish to use the R Commander, also issue the command install.packages('Rcmdr'). On first use, via the library('Rcmdr') command in R, the R Commander will offer to install additional packages that it needs.

If you want to try using C++ code within R (not discussed in the lecture series), also install the Rcpp package, install.packages('Rcpp'). You'll also have to install a C++ compiler, as described in the sections above on building packages under Windows and macOS.

Similarly, if you want to use the Stan Bayesian statistical software via the rstan package (not discussed in the lecture series), you'll have to install the package by the command install.packages('rstan'), and also install a C++ compiler.

Installing R and LaTeX


Contents

1 Introduction
2 Windows
3 MAC OS X
4 Linux
5 Official Presentation Format
6 Further Information
7 Frequently Asked Questions
8 Acknowledgment

1 Introduction

This article explains how to set up an R deveploment enviroment (including LATEX)on various operating systems. We provide a set of instructions for the three majordesktop operating systems Windows, Mac OS X and Linux (Debian-basedsystems in particular) in Sections 2, 3, and 4, respectively. Section 6 includesreferences to related articles and further resources. For more information about Rplease visit the website of the R Project at http://www.r-project.org/.Additional informations about installation and administration of R is available inthe manual at http://cran.r-project.org/doc/manuals/R-admin.html.Further questions not covered by this article and feedback may be sent to[email protected].

2 Windows

The instructions in this manual have been written to run/work under Windows 7.Nevertheless, it is possible to configure the R development enviroment on otherWindows versions. To set up a basic R development environment under Windows,please follow the steps below:

  1. Install the current version of R (available from http://CRAN.R-project.org/bin/windows/base/).
  2. Install the R for Windows toolset (known as Rtools, available from http://cran.at.r-project.org/bin/windows/Rtools/ ).
  3. Set up a suitable LATEX environment, preferably installing all available packages and updates (we recommend TeX Live http://www.tug.org/texlive/ or another distribution like MiKTEXhttp://miktex.org/).
  4. Update the PATHs and further environment variables.

Setting up R and LATEX explained in pictures
Installing the R for Windows Toolset

Rtools includes various software packages needed for successfully building Rand R packages (usually packages containing C or FORTRAN source code).It provides Windows with a set of compilers, libraries and various othertools.

LATEXEnvironment

There are two popular options for installing a LATEX environment under Windows.We are supporting the TeX Live distribution but MiKTEX can also be used. While itis possible to download and use the net installer, we recommend to download theISO-Image from http://mirror.ctan.org/systems/texlive/Images/, mount it toa virtual drive (e.g., using the program DAEMON Tools Lite) and install TeX Liveincluding all packages. In order to update the distribution use the TeX Live Manager(Go to Start All Programs TeX Live <Version>TeX Live Manager fullinstallation), (<Version> has to be replaced with the installed TeX Live version).Note that the installation of the LATEX distribution and the corresponding updateprocess could take several hours.

PATH and further environment variables

Presumably the most important step after the installation procedures is to updatethe PATH environment variable.

To do so go to: Start Control Panel System Advanced system settings Advanced Enviroment Variables.
Search in the System variable list for the PATH variable. Click on edit and enter thecorresponding PATHs into the Variable value field. The PATHs to the ‘bin, x64’ or‘i386’ directories of the following programs have to be included (if they were notincluded automatically):

  • R
  • Rtools
  • TeX Live

Note that the order of the entries is important. Separate them by semicolon. Donot delete other entries, Windows may need them. An example is given below,however there might be different PATHs on the system:

(*)C:Program FilesRR-3.1.1binx64;
(*)C:Rtoolsbin;
(*)C:Rtoolsgcc-4.6.3bin;
(*)C:texlive2014binwin32;
(**)%SystemRoot%system32;
(**)%SystemRoot%;%SystemRoot%System32Wbem;
(**)%SYSTEMROOT%System32WindowsPowerShellv1.0;

(*) are relevant PATH for the R development enviroment, (**) are relevant PATH for Windows (do not change them)

RStudio and alternative Text Editors
We recommend using the RStudio IDE which can be downloaded from http://www.rstudio.com/products/rstudio/download/.
Alternatively, here are some links pointing to URLs where it is possible to download other suitable text editors:

emacshttp://vgoulet.act.ulaval.ca/en/emacs/windows/
Notepad++ http://notepad-plus-plus.org/download
TEXnicCenterhttp://www.texniccenter.org/download/
TeXworks http://www.tug.org/texworks/
Tinn-R http://sourceforge.net/projects/tinn-r/
vim http://www.vim.org/download.php

3 MAC OS X

In order to successfully set up a basic development environment for R-related projectsunder MAC OS X it is necessary to follow these four steps:

  1. Install the current version of R (available from http://CRAN.R-project.org/bin/macosx/ ).
  2. Install a LATEX environment (e.g., MacTeX).
  3. Install XCode.
  4. Install the GNU Fortran compiler (gfortran).

LATEXenvironment

A readily usable environment for typesetting scientific articles with LATEX isprovided with the MacTeX distribution available from http://www.tug.org/mactex/.This distribution also includes a text editor named TeXShop.

XCode

Compilers are needed for installing R packages containing source code typicallywritten in C or FORTRAN. A set of C compilers for Mac OS X is providedin XCode, the basic development tools for Mac OS X. Installable eitherfrom the DVD containing the operating system that came with the Mac or,after downloading the latest version from the Apple Developer website athttp://developer.apple.com/technology/xcode.html.

GNU FORTRAN compiler

Unfortunately, the compiler gfortran is not included in XCode. Luckily, since Rversion 2.5.0 the compiler is installed with the R Mac OS X installer (see Step 1:Installing the current version of R). Nevertheless, it is possible to install it manuallyusing the packages provided at http://r.research.att.com/tools/, in case anyproblems are encountered when setting up the compiler.

Text Editors
Links pointing to URLs where it is possible to download suitable text editors:

emacshttp://vgoulet.act.ulaval.ca/en/emacs/mac/
Rstudio http://www.rstudio.org/ide/download/
LyX http://www.lyx.org/Download/
TeXworkshttp://www.tug.org/texworks/#Getting_TeXworks
vim http://www.vim.org/download.php

4 Linux

In this article we focus on Debian-based distributions, as it is hardly possibleto provide a complete set of instructions for every Linux distribution. Themost prominent of such Debian-based Linux distributions is called Ubuntu(http://www.ubuntu.com). Nevertheless, the instructions mentioned in this sectionshould usually work on any Debian-based system.

In order to successfully set up a basic R development environment under Ubuntufollow these three steps:

  1. Install the current version of R (r-base and r-recommended packages).
  2. Install a LATEX environment (e.g., TeX Live available by installing the texlive-full).
  3. Install compilers and other development packages (r-base-dev packages).

Installing R

R is provided in form of a binary Debian package called r-base. It can be installedeither by using the Synaptic Package Manager or by issuing the following commandon the command prompt:

sudo aptitude install r-base r-recommended

To install a more recent version of R follow the steps provided on the website. ForDebian at http://cran.r-project.org/bin/linux/debian/ or for Ubuntu athttp://cran.r-project.org/bin/linux/ubuntu/.

LATEXenvironment

A readily usable environment for typesetting scientific articles with LATEX isprovided with the TeX Live distribution available from www.tug.org/texlive/.Luckily, one can install the complete distribution with the texlive-full package veryeasily. It can be installed either using the Synaptic Package Manager or by issuingthe following command:

sudo aptitude install texlive-full texlive-xetex
In order to use the wu-beamerstyle one additionally needs to install thettf-mscorefonts-installer.

Rstudio Latex Free

sudo aptitude install ttf-mscorefonts-installer

Compilers and Other Tools

Typically, compilers and other development tools are needed in a reasonabledevelopment environment. Those tools are made available on Debian-based systemssimply by installing the r-base-dev package. It can be installed either using theSynaptic Package Manager or by issuing the following command:

sudo aptitude install r-base-dev

RStudio and Text Editors
We recommend using the RStudio IDE which can be downloaded from http://www.rstudio.com/products/rstudio/download/.
Rstudio latex fontThe package can than be installed via commandline:
sudo dpkg -i /PathToYourDownloadedFile on Debian/Ubuntu

sudo rpm -i /PathToYourDownloadedFile on Fedora/openSUSE
Please feel free to install the editors you need as mentioned above via SynapticPackage Manager or via commandline:
sudo aptitude install i.e. emacs, vim and so on.Rstudio Latex

5 Official Presentation Format

As your lecturer may want you to use the wubeamer style for your presentation withinthe scope of our department’s courses, please feel free to download it via svn from theWU’s svn Repository http://www.wu.ac.at/it/instructions/latex_template.Please note that according to our departments understanding the use of the templateis permitted exclusively for courses at the WU. In order to use the wu-beamerstyle(official presentation format, https://svn.wu-wien.ac.at/wu/ login with the WU SVNRepository Login i.e. h+matriculation number + powernet password) oneadditionally needs to install the ttf-mscorefonts-installer.

6 Further Information

More information about the topic can be retrieved from the following references.

  • R Installation and Administration:
    • Windows
      http://cran.r-project.org/doc/manuals/R-admin.html#Installing-R-under-Windows
    • Mac Os X
      http://cran.r-project.org/doc/manuals/R-admin.html#Installing-R-under-Mac-OS-X
    • Unix/Linux
      http://cran.r-project.org/doc/manuals/R-admin.html#Installing-R-under-Unix_002dalikes
  • Writing R Extensions (R-Project):
    http://cran.r-project.org/doc/manuals/R-exts.html
  • Making R packages for the Mac:
    http://personality-project.org/r/makingpackages.html
  • Making R packages under Windows:
    http://cran.r-project.org/doc/contrib/Graves+DoraiRaj-RPackageDevelopment.pdf and http://www.math.ncu.edu.tw/~chenwc/R_note/reference/package/packages.pdf
  • TEX Formula Quick Reference (Wikipedia):
    http://en.wikipedia.org/wiki/Help:Displaying_a_formula
  • LATEX unter Windows (German):
    http://schlosser.info/latexsystem.html

7 Frequently Asked Questions

  1. TeX Live (Windows):
    1. How to make TeX Live recognize ‘Sweave.sty’?

      Answer:

      Go to the R installation directory, i.e., the directory where R has been installed to. We call this directory ‘RHOME’. Search for the LATEX style sheet called ‘Sweave.sty’. Typically it is located in the folder ‘<RHOME>sharetexmf’. Next, one needs to copy the corresponding files to the “personal“ LATEX libary. We will call the directory where TeX Live has been installed ‘TEXHOME’. The “personal“ LATEX library is located in the directory ‘<TEXHOME>texmftexlatex’ and ‘<TEXHOME>texmf-localtexlatex’. Copy the contents of the ‘<RHOME>sharetexmf’ latex folder to the ‘Sweave’ folder in the “personal“ LATEX library. If the ‘Sweave’ folder is not found in the latex library, create it and copy the contents into this folder.
      Eventually the package database needs to be updated.
      Go to Start and open the command line interface by entering cmd. Once the command line interface starts and update the database by entering texhash (use the command texhash.exe).

    2. How to install further LATEX packages?

      Answer: Go to Start All Programs TeX Live <Version>TeX Live Manager. (<Version> has to be replaced with the installed TeX Live version)

      Search for the packages needed, then select the package and click on Install. (It is also possible to select all packages, but this will include many language packs which are probably not needed).

    3. Note that the installation and the update process of TeX Live can take a very long time. Sometimes this can lead to the sytem becoming unresponsive, in that case simply restart the corresponding process.
  2. MiKTEX(Windows):
    1. How to make MiKTEX recognize ‘Sweave.sty’?

      Answer: In Pictures
      Go to the R Installation directory, i.e., the directory where R has been installed to. We call this directory ‘RHOME’. Search for the LATEX style sheet called ‘Sweave.sty’. Typically, it is located in the folder ‘<RHOME>sharetexmf’. Copy the contents of this folder to the ‘Sweave’ folder in the “personal” LATEX library. The location of the LATEX library can be seen by going to Start All ProgramsMiKTeX<Version>1Maintenance Settings.

      Go to the Roots tab, and mark the “Show MiKTeX-maintained root directories” box. Look at the Description where it says “UserInstall,UserConfig”. The coresponding PATH points to the location. Usually it is in ‘<RHOME>Application DataMiKTeX<Version>texlatex’; (<Version>has to be replaced with the installed MiKTEX version). If there is no ‘latex’ and/or ‘Sweave’ folder, create it and copy the contents into this folder.

    2. How to install further LATEX packages?

      Answer: Go to Start All Programs MiKTeX<Version>1Maintenance(Admin) Package Manager(Admin); (<Version>has to be replaced with the installed MiKTEX version).

      Search for the package needed, then right click on the package name and click on Install.

    3. MikTEX offers downloading missing packages but fails.

      Answer: Note that it is required to be able to write to the system library of MiKTEX. Typically this can only be achieved through logging in with Administrator privileges.

8 Acknowledgment

An earlier version of this manual was written by Ksenia Fraczek.

2011-11-29 cy

Rstudio Latex