2 Install everything

logologologologologologo

If you are trying R for the first time, it is vital that you attempt to set up your computer with the necessary software in advance or it will be difficult to keep up.

This chapter describes all of the pre-workshop homework:

2.1 Already installed?

If you do not have R and RStudio installed, please skip this section and start with the next section. If you are already an R and RStudio user, this is a great time to check for updates.

Updating RStudio

  • RStudio menu Help > Check for Updates will tell you if you are current or not.
  • To update, close RStudio on your machine, download the new version from the RStudio website, and run the RStudio-some-version-number.exe. (Windows users might have to run the executable as an administrator.)

Update your packages

How to upgrade all out-of-date packages in What They Forgot to Teach You About R by Jennifer Bryan and Jim Hester.

Updating R

The easiest way to update R is to simply download the newest version. RStudio will automatically use the latest you’ve installed.

Alternatively, Windows users can use the installr package:

  • Install the installr package
  • If open, close R and RStudio
  • Navigate to your most recent Rgui.exe file located in your Programs directory, e.g., C:\Program Files\R\R-4.0.0\bin\x64\Rgui.exe
  • Right-click on Rgui.exe and run as administrator
  • In the R GUI window that appears, run the commands
    # Windows users only
    library("installr")
    updateR()

Updating your R library

How to transfer your library when updating R also by Bryan and Hester. Requires the fs package.

Once your updates are complete

Skip the next section and continue the homework with

2.2 Install R and RStudio

The first steps are to install R and RStudio. Windows users may have to login as an Administrator before installing the software.

Once the installation is complete, you can take a 2-minute tour of the RStudio interface.

The same video includes a longer (7 minute) tour of the four quadrants (panes) in RStudio if you are interested.

2.3 Create a project

To begin any project, we create an RStudio Project file and directory. You can recognize an R project file by its .Rproj suffix.

If you prefer your instructions with commentary,

If you prefer basic written instructions,

  • RStudio, File > New Project… > New Directory > New Project
  • Or, click the New Project button in the Console ribbon,

rstudio new project button


In the dialog box that appears,

  • Type the workshop name as the directory name, for example, workshop, or if you like more detail, midfield-workshop-asee-2021
  • Use the browse button to select a location on your computer to create the project folder
  • Click the Create Project button

Whenever you work with the workshop materials, launch the workshop.Rproj file (using the name you actually used) to start the session.

2.4 Add some folders

While file organization is a matter of personal preference, we ask that you use the directory structure shown here for your work in the workshop. Assuming we called our project workshop, the minimal directory structure has three folders in it plus the .Rproj file at the top level.

\workshop
    \data
    \results
    \scripts
    workshop.Rproj

We use the folders as follows:

  • data data files
  • results finished graphs and tabulated data formatted for display
  • scripts R scripts that operate on data to produce results

To create folders:

  • use your usual method of creating new folders on your machine
  • or you can use the New Folder button in the Files pane

rstudio new folder

For a video guide,

2.5 Install CRAN packages

The fundamental unit of shareable code in R is the package. For the R novice, an R package is like an “app” for R—a collection of functions, data, and documentation for doing work in R that is easily shared with others [2].

Most packages are obtained from the CRAN website [3]. To install a CRAN package using RStudio:

  • Launch RStudio

The RStudio interface has several panes. We want the Files/Plots/Packages pane.

  • Select the Packages tab

rstudio packages pane


Next,

  • Click Install on the ribbon
  • In the dialog box, type the name of the package. For our first package, type data.table to install the data.table package [4]
  • Check the Install dependencies box
  • Click the Install button

During the installation, Windows users might get a warning message about Rtools, something like:

WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version....

Rtools is needed for packages with C/C++/FORTRAN code from source—which does not apply to us. You may ignore the warning and carry on.

In the RStudio Console, you should see a message like this one,

    package 'data.table' successfully unpacked and MD5 sums checked

If successful, the package will appear in the Packages pane, e.g.,

rstudio packages pane

Repeat the process for the following packages

wrapr 
Rdpack 
checkmate
ggplot2

Alternatively, you can install them all at once by typing in the Console:

packages_we_use <- c("data.table", "wrapr", "Rdpack", "checkmate", "ggplot2")
install.packages(packages_we_use)

2.6 Install midfieldr

midfieldr is not yet available from CRAN. To install the development version of midfieldr from its drat repository, type in the Console:

# type in the RStudio Console 
install.packages("midfieldr", 
                 repos = "https://MIDFIELDR.github.io/drat/", 
                 type = "source")

You can confirm a successful installation by running the following lines to bring up the package help page in the Help window.

# type in the RStudio Console 
library("midfieldr")
? `midfieldr-package`

# or, equivalently
help("midfieldr-package")

If the installation is successful, the code chunk above should produce a view of the help page as shown here.

midfiedr help page pane

2.7 Install midfielddata

Because of its size, the data package is stored in a drat repository instead of CRAN. Installation takes time; please be patient and wait for the Console prompt “>” to reappear.

Type (or copy and paste) the following lines in the RStudio Console.

# type in the RStudio Console  
install.packages("midfielddata", 
                 repos = "https://MIDFIELDR.github.io/drat/", 
                 type = "source")
# be patient

Once the Console prompt “>” reappears, you can confirm a successful installation by viewing the package help page. In the Console, run:

# type in the RStudio Console  
library("midfielddata")
help("midfielddata-package")

If the installation is successful, the code chunk above should produce a view of the help page as shown here.

midfiedldata help page pane

2.8 Exiting R and RStudio

When you exit R/RStudio, you probably get a prompt about saving your workspace image.

The answer is No. 

You can turn this prompt off by using the pulldown menu,

  • Tools > Global Options…
  • In the dialog box, Save workspace to .RData on exit: Select “Never”

You finished your homework!


▲ top of page