R and RStudio are both free,open-source software, available for all commonly usedoperating systems. R is developed cooperatively andnoncommercially under the auspices of the Free SoftwareFoundation; RStudio is a commercial product.
R and RStudio install in the standardmanner 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 beforeinstalling RStudio.
Please read and follow theseinstructions carefully. Installation assistance will also beavailabile from the instructor (John Fox) and teachingassistant (Allison Leanage) prior to the start of the lectureseries and during office hours.
Installing R on Windows
Visit the Comprehensive R ArchiveNetwork (CRAN) and select a mirror site; a list of CRAN mirrorsappears at the upper left of the CRAN home page. I suggestthat you use the 0-Cloudmirror, which is the first on the list. Click on thelink Download R for Windows, which appears nearthe top of the page; then click on install R for thefirst time, and subsequently on Download R x.y.zfor Windows (where x.y.z is the current version of R,which is R 4.1.0 at the start of the lectures series). Onceit is downloaded, double-click on the R installer. Youmay take all of the defaults, but I suggest that youmake the following modifications:
Instead of installing R in thestandard location, C:\Program Files\R\R-x.y.z, I suggest thatyou use C:\R\R-x.y.z. Again, x.y.z is the current version ofR. This will allow you to install packages in the main Rlibrary without running R with administrator privileges andmay avoid problems that sometimes occur when there are spacesin paths.
You may take all of theremaining defaults in the R for Windows installer.
Building Packages Under Windows, etc.(Optional)
If you wish to build packages, oruse compiled C, C++, or Fortran code in R, or use the rstanpackage for Bayesian inference, you will have to installsome additional software and properly configure your Windowssystem. You do not have to be able to build R packagesin order to install pre-built Windows binary packages fromCRAN, so these steps are generally unnecessary unlessyou plan to write your own packages, use compiled code, oruse rstan. None of these topics are covered in thelecture series.
Click on the Rtools link onthe R for Windows CRAN page. Download the currentversion of the Rtools installer and run it. You may take allof the other defaults. An additional necessary step is to addthe Rtools usr\bin subdirectory to your system path; forexample, if Rtools is installed in c:\rtoolsxy (which is thestandard location for version xy of Rtools), then you wouldadd c:\rtoolsxy\usr\bin; to your system path. Type thislocation carefully, including the terminating semicolon -- youdon't want to mess up your path.
An alternative, and possibly safer,procedure for specifying the path to Rtools is described onthe Rtoolswebpage.
If you want to be able to build Rpackages outside of RStudio, also add c:\R\R-x.y.z\bin; to thepath (assuming that you installed R in the location that Isuggested).
If you want to be able to build PDFhelp files for packages, download and install the MiKTeX LaTeX system; there isalso a link to MiKTeX on the Building R for Windowspage. Installing MiKTeX will also allow you to create Sweaveand knitr LaTeX documents in RStudio, and to compile RMarkdown documents directly to PDF files.
Installing R on macOS
Visit the Comprehensive R ArchiveNetwork (CRAN) and select a mirror site; a list of CRAN mirrorsappears at the upper left of the CRAN home page. I suggestthat you use the 0-Cloudmirror, which is the first on the list. Click on thelink Download R for MacOS X, which appears nearthe top of the page; then click on R-x.y.z.pkg(where x.y.z is the current version of R -- R 4.1.0 at thestart of the lectures series), which assumes that you areusing macOS 10.11 (El Capitan) or higher. You'll also findolder versions of R if you have an older version of macOS.Note: As a general matter, you're probably better offupdating your macOS to the current version.
Two macOS installers are provided for thecurrent version of R: one for Macs that use Intel chips andone for newer Macs that use the Apple M1 chip. The formerinstaller is named R-x.y.z.pkgand the latter is named R-x.y.z-arm64.pkg. Atpresent, I recommend that you use the R-x.y.z.pkg installer whetheror not you have a Mac with an Intel chip. The Intelversion will work with both kinds of Macs, and there arestill some compatibility issues for certain packagesassociated with the M1-specific version of R.
Once it is downloaded,double-click on the R installer. You may take all of thedefaults.
Building Packages Under macOS, etc.(Optional)
If you wish to build packages, oruse compiled C, C++, or Fortran code in R, or use the rstanpackage for Bayesian inference, you must install the AppleXcode developer tools. None of these topics is covered inthe lecture series. For macOS 10.7 (Lion) or higher, you caninstall Xcode for free from the App Store. For earlierversions of macOS, Xcode can be installed from your systemDVD or downloaded from the Apple developer website. Youdo not need Xcode to install pre-built macOS binarypackages from CRAN, so this step is unnecessaryunless you plan to write your own packages, use compiledcode, or use the rstan Bayesianestimation package.
Some R packages include Fortran, C,or C++ code; to build such packages, you will have to installcompilers for these languages.The C and C++ compilers areincluded in the Apple Xcode tools, but you will have toseparately downloadand install a Fortran compiler.
If you want to be able to build PDFhelp files, download and install the MacTeX LaTeX system.Installing MacTeX will also allow you to create Sweave andknitr LaTeX documents in RStudio, and to compile R Markdowndocuments directly to PDF files.
Installing X-Windows on macOS(Optional)
Some R software (e.g., my Rcmdrpackage) makes use of the Tcl/Tk graphical-user-interface(GUI) builder via the tcltk package tocreate point-and-click interfaces and to display GUIelements such as progress bars. To use the tcltkpackage, which is a standard part of the R distribution, youmust have the X11 windowing system installed on your Mac.Some other packages that don't use Tcl/Tk, such as the rglpackage for dynamic 3D graphics, also require X11.
Check to see whether the X11windowing system (X Windows) has already been installed onyour computer. If you wish, it should do no harm to skipthis step and simply go to the next step to install XQuartz.
ForOS X 10.6 and 10.7,the file X11.app should appearin the Utilities folderunder Applications in the finder. This application shouldalways be installed under OS X 10.7.
ForOS X 10.8orhigher, the file is named XQuartz.app and is no longerincluded with the operating system. XQuartz.app may also beinstalled in OS X 10.6 or 10.7.
Note that if you upgrade macOS, youwill have to reinstall XQuartz even if you installed itpreviously.
You may also issue the command capabilities("X11")at the R command prompt. If the response is TRUE thenX11 is installed.
If neither X11.app nor XQuartz.app isinstalled, install XQuartz from https://www.xquartz.org/.As mentioned, it should do no harm to install XQuartzeven if you have X11 currently installed.
1. Download the disk image(dmg) file for XQuartz.
2. When you open this file bydouble-clicking on it, you'll find XQuartz.pkg;double-click on it to run the installer, clickingthrough all the defaults.
3. Important:After the installer runs, you'll have to logout and back on to your macOS account, orjust reboot your Mac. Also, onfirst use, XQuartz builds a cache of fonts and soinitial performance may be slow; this problem should goaway after a short period of time.
Installing R on Linux Systems
Visit the Comprehensive R ArchiveNetwork (CRAN) and select a mirror site near you; alist of CRANmirrors appears at the upper left of the CRAN homepage. I suggest that you use the 0-Cloud mirror,which is the first on the list. Click on the link DownloadR 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, andproceed as directed.
If you have a Linux or Unix systemthat's not compatible with one of these distributions, youwill have to compile R from source code; the procedurefor doing so is described in the R FAQ (frequently askedquestions) list.
Installing RStudio
Go to the RStudiodownload page, select the free version of RStudioDesktop, click the Download button, and click on thelink to the appropriate installer for your operating system(Windows, macOS, or Linux distro). Visit the RStudio IDEhome page for more information about RStudio.
Once it is downloaded, run theRStudio installer and take all of the defaults: In Windows,double-click on the RStudio installer to start theinstallation; in macOS, double-click on the downloaded RStudiodisk-image file, and drag the RStudio icon to the Applicationsfolder.
When you first run RStudio, it shoulddetect your R installation and start the R console. Toconfigure RStudio to your taste, select Tools > GlobalOptions (Windows) or RStudio > Preferences(macOS) from the RStudio menus. Inparticular, I suggest that on the General optionsscreen you deselect Restore .RDatainto workspace at startup, and set Saveworkspace to .RData on exit to Never.
If you encounter difficulties,consult the RStudiotroubleshooting guide. or seek help from John orAllison.
Installing R Packages for the LectureSeries
Once you have installed R andRStudio, you can install additional packages required forthe lecture series by typing the following command at the> command prompt in the R Console (and pressing the Enteror return key):
install.packages(c("car","data.table", "effects", "knitr", "lme4", "rgl", "rmarkdown","sfsmisc", "tidyverse"))
You can simply copy and paste thiscommand from these installation instructions. Alternatively,you can install packages from the RStudio Packagestab. Be aware that, depending on the speed of your internetconnection, it may take some time to download and installthese packages and their dependencies.
If you want to tryusing 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 inthe sections above on building packages under Windows andmacOS.
Similarly, if you want to use theStan Bayesian statistical software via the rstanpackage (not discussed in the lecture series), you'll have toinstall the package by the command install.packages("rstan"), andalso install a C++ compiler.