2  Setting Up Your Computer

Published

February 19, 2025

2.1 Objectives

  • Set up RStudio, R, Quarto, and python
  • Be able to run demo code in R and python

2.2 What are you asking me to install?

It’s a good idea to be skeptical when someone is telling you to install things. 🤨 Here’s a very broad overview of what each of these programs or services does and why I’m asking you to install or sign up for them.

What each program does, in general terms
Program Logo Purpose
R R logo A statistical programming language built around working with data
Python Python logo A general-purpose programming language that is popular for machine learning tasks.
RStudio IDE RStudio logo An integrated desktop environment created to make it easy to work with R, Python, and other data-science programming tools.
Quarto Quarto logo A document creation system based on pandoc. Quarto allows you to include code, results, and pictures generated from data within a document so that they automatically update when the document is recompiled.
Git git logo A version control system used to track changes to files.
GitHub GitHub logo An online collaboration platform based on git that makes it easy to back up, share, and collaborate on programming projects.

2.3 Why do I need to install this stuff?

Presumably, if you’re reading this book, you either want to know how to do some statistical programming task, or you’re taking a class on statistical programming that is using this textbook. Hopefully, if you’re reading this for a class, you also want to learn how to work with data in some fashion. While some parts of this book are fairly language-agnostic (R and python are both good languages for working with data), this setup is opinionated - the book focuses on a set of programs which are useful for doing statistical programming, including writing reports, keeping track of code, visualizing data, cleaning data, and getting set up for modeling data and producing results.

If you’re just trying to learn R, perhaps you don’t need to install python or quarto. If you’re not working with other people, maybe you don’t need to install git. You are welcome to make those decisions for yourself, but if you’re not sure, you might just want to install the whole toolbox - you’ll hopefully learn how to use all of the tools along the way, and it’ll be less confusing later if you already have access to all of the tools and don’t need to go back and get something else when you need it.

2.4 Installation Process

In this section, I will provide you with links to set up various programs on your own machine. If you have trouble with these instructions or encounter an error, post on the class message board or contact me for help.

  1. Install R (and associated tools)
  1. Get the R installer from CRAN: https://cran.rstudio.com/bin/windows/base/

  2. Install the Rtools4 package that matches the R version you installed: https://cran.rstudio.com/bin/windows/Rtools/
    For example, if you installed R 4.4.3, you should download RTools 4.4. If you installed R 4.0.1, you should install RTools 4.0.

  1. Get the R installer from CRAN: https://cran.rstudio.com/bin/macosx/

  2. Install XCode - a set of developer tools - so that you can install R packages more easily. There are two ways to do this:

  1. Get XCode from the App store directly (simple, but you may have to do other things later)
  2. Install Homebrew and then use homebrew to install XCode.1

I personally suggest option b, because Homebrew is used for a lot of different software-development related things, and having it will make life easier later.

Installing Homebrew + XCode

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install mas # Search the apple store
mas search xcode # find xcode
mas install 497799835 # install the program by ID

If you’re using a distribution like Debian, Fedora, RedHat, or Ubuntu, or a distribution derived from one of these distributions, follow the instructions below.

If you don’t know your distribution (or if you’re working on a derivative distribution, like Mint), you can figure out which set of instructions to follow by executing lsb_release -a in your system terminal.

If you’re using Arch or another distribution that isn’t derived from something listed above, google for instructions specific to your distribution - these instructions cover the most common, user-friendly distributions, but if you’ve chosen something more niche, it is assumed you can figure out how to install R. (If that isn’t the case, leave a comment below with your distribution, so I know how best to update this book.)


Installation Instructions

  1. Get the R installer from CRAN: https://cran.rstudio.com/bin/linux/ - pick your distribution, and follow the distribution-specific instructions from there.

You could install R using your package manager, but often this version is out of date. The instructions for each version will get you a more up-to-date version that will be easier to work with.

  1. Download and install the latest version of python 3

    • Windows: check the box that asks if you want to add Python to the system path. This will save you a lot of time and frustration. If you didn’t do this, you can follow these instructions to fix the issue (you’ll need to restart your machine).

    • If you’re interested in python, you should install Jupyter using the instructions here (I would just do pip3 install jupyterlab)
      We will not use jupyter much in this book - I prefer quarto - but the python community has decided to distribute code primarily in jupyter notebooks, so having it on your machine may be useful so that you can run other people’s code.

    • Additional instructions for installing Python 3 from Python for Everybody if you have trouble.

  2. Download and install the latest version of RStudio for your operating system. RStudio is a integrated development environment (IDE) for R, created by Posit. It contains a set of tools designed to make writing R, python, javascript, and other data-related code easier.

  3. Download and install the latest version of Quarto for your operating system. Quarto is a command-line tool released by Posit that allows you to create documents using R or python, combining code, results, and written text.

The following steps may be necessary depending on which class you’re in. If you want to be safe, go ahead and complete these steps as well.

  1. Install git using the instructions here. Consult the troubleshooting guide if you have issues. If that fails, then seek help in office hours.

    We will configure git later, in Chapter 5. For now, let’s just get it installed.

  2. Install LaTeX, rmarkdown, and quarto:

    • Launch R, and type the following commands into the console:
    install.packages(c("tinytex", "knitr", "rmarkdown", "quarto"))
    library(tinytex)
    install_tinytex()

    If you have an existing installation of LaTeX, using tinytex or another installer, I highly recommend that you uninstall LaTeX before re-installing the most recent version.

    • Configure RStudio to use the tinytex installation of LaTeX:
      • Go to Tools -> Global Options -> Sweave
        The RStudio configuration pane for Sweave. The PDF generation options are as follows: Weave Rnw files using `knitr`, Typeset LaTeX into PDF using `XeLaTeX` (for font compatibility and modern rendering). The LaTeX editing and compilation options are a set of checkboxes; the two checked options are use tinytex when compiling .tex files, and Clean auxiliary output after compile. The PDF preview options can be set as you desire and are highly operating-system dependent.
Your turn

Open RStudio on your computer and explore a bit.

  • Can you find the R console? Type in 2+2 to make sure the result is 4.
  • Run the following code in the R console to set up some basic packages:
install.packages(c("tidyverse", "rmarkdown", "knitr", "quarto"))
  • Run the following code in the R console to set up Python:
install.packages("reticulate")
reticulate::virtualenv_create("stat-python") # Create a python environment
reticulate::virtualenv_install("stat-python", c("numpy", "matplotlib", "pandas"))
  • Configure RStudio to use the python virtual environment you created:
    • Go to Tools -> Global Options -> Python
    • Click the Python interpreter Select button
    • Click Virtual Environments
    • Choose stat-python and hit “Select”
    • Restart RStudio when prompted to ensure the setting takes effect.
  • Test out Python:
    • Go to the Console tab, and click on the R logo. A dropdown should be present with an option to select Python.
    • Wait for the Python prompt to appear (it will look like this: >>>)
    • Type 2 + 2, hit enter, and make sure the result is 4.
  • Can you find the text editor?
    • Create a new quarto document (File -> New File -> Quarto Document).
    • Select a blank quarto document (bottom of the pop-up window)
    • Change the text editor to Source mode (it will default to visual mode). This makes it possible to paste a quarto document without Visual mode trying to “fix” the markup characters.
    • Paste in the contents of this document.
    • Compile the document (Ctrl/Cmd + Shift + K) and use the Viewer pane to see the result.
  • If this all worked, you have RStudio, Quarto, R, and Python set up correctly on your machine.

  1. If you prefer a different package manager, that’s fine - Homebrew is widely used, but there are other options. Ultimately, you just need to have XCode so that you can compile R packages.↩︎