installation

R

R is the computing language we will use to run our analyses and produce high quality plots. If you already have R installed (you will need at least version 4.5), you can go straight to installing RStudio. If not, follow these steps to install R:

  1. Go to https://cran.r-project.org/

  2. Click on “Download R for <your operating system>” (see footnote), depending on your operating system you will select “Download R for Linux”, “Download R for (Mac) OS X”, or “Download R for Windows”.

We will use <this notation> quite a bit. It indicates a place where you should insert information, data, or something similar that corresponds to your particular situation. In this example it means insert “your operating system”, i.e. Linux, (Mac) OS X, or Windows.

  1. For Mac: download the .pkg file for the latest release. For PC: click “install R for the first time”, then click “Download R <version> for Windows”.

  2. After the executable finishes downloading (in Windows, it is a file with .exe extension; for Mac, it is a .dmg file or a .dmg inside a .pkg file), open the file as an administrator, and follow the installation instructions. R should install without any problems. You can click OK for all of the windows that pop-up during installation, and choose a “regular” installation (if given the choice).

If you have trouble installing R please google “Install R Mac” or “Install R PC” and follow one the many video tutorials out there. If you have tried this and are still having trouble, please contact me.

checking your R version

If R is already on your computer, check its version before going any further. The course package is built for R 4.5 and newer, and on an older R the installation will try to build it from scratch and usually fail. Open R or RStudio and type this into the console:

R.version.string

R answers with a line such as "R version 4.5.1 (2025-06-13)". If the number after “R version” is 4.5 or higher, you are ready for RStudio. If it is lower, update R by installing the newest version over the top of the old one: close RStudio, then follow steps 1 to 4 above. On a Mac the new version replaces the old one. On Windows it is installed alongside, and RStudio uses the newest version automatically. Windows users can instead update from inside R: open the plain R program (not RStudio) and run install.packages("installr") and then installr::updateR().

Each version of R keeps its own set of packages, so after updating R you need to install the course packages again (see the phylochemistry section below). Run R.version.string once more to confirm the update worked.

RStudio

Once we install R, we can install RStudio, which is essentially a convenient way of interacting with R. Some people do not like RStudio and prefer to interact with R directly. This is fine, but many beginning R users find RStudio helpful, so I recommend it. Follow these steps to install RStudio:

  1. Go to https://rstudio.com/

  2. Click “DOWNLOAD” at the top of the page.

  3. Click the “DOWNLOAD” button that corresponds to RStudio Desktop with the free Open Source License.

  4. The page may automatically detect which operating system you are using and recommend a version for you. If it does, download that file (.exe for PC or .dmg for Mac). If not, scroll down to the “All Installers” section and download the file that is right for you. Open the file as an administrator, and follow the installation instructions. RStudio should install without any problems. You can click OK for all of the windows that pop-up during installation, and choose a “regular” installation (if given the choice).

If you have trouble installing RStudio please google “Install RStudio Mac” or “Install RStudio PC” and following one the many video tutorials out there. If you have tried this and are still having trouble, please contact me.

verification

Open RStudio by clicking on the appropriate file in your applications folder, or wherever it is saved on your computer. You will see several windows. One is the Code Editor, one is the R Console, one is the Workspace and History, and one is the Plots and Files window.

The R Console window should have a > in it. Type head(Indometh). This should display the first six lines of a data set describing the pharmacokinets of indomethacin. This is one of the built in datasets in R - you do not need any additional files to run this test.

head(Indometh)
## Grouped Data: conc ~ time | Subject
##   Subject time conc
## 1       1 0.25 1.50
## 2       1 0.50 0.94
## 3       1 0.75 0.78
## 4       1 1.00 0.48
## 5       1 1.25 0.37
## 6       1 2.00 0.19

Next, type plot(Indometh) into the R Console. This will plot the indomethacin dataset in a basic way.

plot(Indometh)

If both the above commands (head(Indometh) and plot(Indometh)) worked and there were no error messages during installation, then you should be ready to proceed.

TeX

In this class we will generate high quality reports suitable for submission to supervisors, academic journals, etc. For this, we need the typesetting engine TeX. There are a few ways to do this. The easiest way is using the following commands:

install.packages(c('tinytex', 'rmarkdown'))

If you are on Mac, you may get an error about “not being able to write to a path” or something like that. In that case you probably need to open your terminal and run the following two commands:

sudo chown -R \`whoami\`:admin /usr/local/bin

and then

~/Library/TinyTeX/bin/\*/tlmgr path add

Then, on both Mac and PC, you then need to do:

tinytex::install_tinytex()

Other options are: if you have Windows, download and install MikTeX. If you have OSX, you can download and install MacTeX.

phylochemistry

In addition to the tidyverse, there are a variety of other packages we will need, as well as some datasets and custom functions. The datasets and custom functions come in the course’s R package, phylochemistry, which also installs the other packages the chapters use. Install everything by running these two commands once in the R console:

install.packages("BiocManager")
install.packages(c("tidyverse", "phylochemistry"), repos = c("https://thebustalab.r-universe.dev", BiocManager::repositories()))

The first command installs BiocManager, a small helper that points R at the version of Bioconductor that matches your version of R. Bioconductor hosts a few of the packages phylochemistry relies on, such as the tree-drawing package ggtree. The second command installs the tidyverse and phylochemistry along with everything they need. The installation can take several minutes. If R asks “Do you want to install from sources the packages which need compilation?”, type no and press enter. You only need to do this once on each computer, and again after you upgrade R.

Then, each time you open R or RStudio, load the tidyverse and phylochemistry:

The datasets used in this book, such as algae_data, are available as soon as phylochemistry is loaded. A few large datasets are downloaded the first time you ask for them, for example ny_trees <- getDataset("ny_trees"). Some functions in the later chapters need extra packages, and the first time you use one of them it will tell you what to install.

If you are in the Busta Lab, also install the lab package once, then load it in each session alongside phylochemistry:

install.packages("bustalab", repos = c("https://thebustalab.r-universe.dev", BiocManager::repositories()))
library(bustalab)

keeping phylochemistry up to date

The course package gets new functions and fixes during the semester. When a newer version is available, loading it prints a short message such as:

A newer version is available: phylochemistry 1.0.4 (you have 1.0.3). To update, run: updatePhylochemistry()

When you see it, type the command it names:

This installs the newest phylochemistry, along with bustalab if you have it. Then restart R (in RStudio: Session > Restart R) and run library(phylochemistry) again, since a package that is already loaded cannot be swapped for the new one. R checks for updates at most once a day and stays silent when you are offline. If the update fails on Windows, restart R and run phylochemistry::updatePhylochemistry() before loading the package.

If you installed phylochemistry before this message existed (any version before 1.0.1), or if you used the older source("https://thebustalab.github.io/phylochemistry/phylochemistry.R") line, your copy will never show the message. Update it once by running the install.packages(c("tidyverse", "phylochemistry"), ...) line above again: that line always installs the newest version. You can check which version you have with packageVersion("phylochemistry").

data import

Once R, RStudio, and phylochemistry are installed, the last piece is getting your own data into R. The sandboxes in this book load their datasets for you, but data sitting on your own hard drive has to be imported, and there are two ways to do it.

The easiest way to do this is to use the interactive command readCSV(), a function that comes with phylochemistry. You run readCSV() in your console, then navigate to the data on your hard drive.

Another option is to read the data in from a path. For this, you will need to know the “path” to your data file. This is essentially the street address of your data on your computer’s hard drive. Paths look different on Mac and PC.

  • On Mac: /Users/lucasbusta/Documents/sample_data_set.csv (note the forward slashes!)
  • On PC: C:\\My Computer\\Documents\\sample_data_set.csv (note double backward slashes!)

You can quickly find paths to files via the following:

  • On Mac: Locate the file in Finder. Right-click on the file, hold the Option key, then click “Copy as Pathname”
  • On PC: Locate the file in Windows Explorer. Hold down the Shift key then right-click on the file. Click “Copy As Path”

With these paths, we can read in data using the read_csv command. We’ll run read_csv("<path_to_your_data>"). Note the use of QUOTES ""! Those are necessary. Also make sure your path uses the appropriate direction of slashes for your operating system.