Lab 1: Getting Started

Lab instructions for users

These instructions apply to RStudio server users only. You should be using the RStudio server only if you are unable to install R and RStudio on your personal computer (e.g., if you have a Chromebook). If you have arrived here by mistake, please return to the previous page and choose the correct computing device from the list.

The RStudio server

Smith College operates a RStudio server for students, staff, and faculty. This allows you to use RStudio from within your web browser without needing to install any software.

There are a few pros and cons to using the RStudio server, as a opposed to installing R and and RStudio on your personal computer.

Pros

  • It’s easy to get started
    • With the RStudio server, you can start writing code immediately. There’s no need to figure out which programs you need to install, and how to install them.
    • You also don’t need to install any R packages (which are a like add-ons for the R programming language); the Smith IT staff have already installed a large number of R packages.

Cons

  • You need an internet connection
    • When you’re not connected to the internet, you cannot interact with the RStudio server.
  • You have less control
    • It’s unlikely, but if you do need to install an R package, you’ll need to ask the Smith IT staff to do it.
  • You have to share
    • Because everyone using the RStudio server is using the same computer, it’s possible that the server may run slowly (e.g., if everyone does HW at the last minute).

Even though the list of cons is a big longer than the list of pros, the RStudio server should be more than adequate for your SDS classwork. Just keep in mind 1) you need an internet connection, 2) you might need to ask for help, and 3) you do have be aware of shared resources.

Logging on to the Smith RStudio Server

To get started, go to https://posit.smith.edu/. Then, log in with your Smith username and password (see Figure 1).

Figure 1

On the server home page, press the “New Session” button near the top-middle of the page (see Figure 2)

Figure 2

A pop-up window should appear asking you to choose what kind of session you’d like to start. Choose the “RStudio Pro” option, and press “Start Session” in the bottom-left corner (see Figure 3).

Figure 3

After a few seconds, the page should re-load, and the RStudio interface should open (see Figure 4)

Figure 4

The RStudio application window is divided into four “panes”, which are labeled and color-coded in the diagram below

  1. The Console pane
  2. The Editor pane (not shown)
  3. The Environment pane
  4. The “Miscelleaneous” pane

Question

Copy and paste the code below into the console pane, and press the Enter key to run the code. If your installation of R is working correctly, you should see the R version printed out, and a message indicating that your version of R is up do date (like the message seen below).

r_version_info <- R.Version()
if (all(as.numeric(r_version_info$major) >= 4, 
        as.numeric(r_version_info$minor) > 4
        )
    ) {
  msg <- "✔ Your version of R is up to date!"
} else {
  msg <- "✖ Your version of R is out of date; You should update to version 4.4 now."
}
cat(r_version_info$version.string, msg, sep="\n")
R version 4.4.2 (2024-10-31)
✔ Your version of R is up to date!

Configuring RStudio

RStudio has hundreds of configuration options, allowing each user to customize its behavior to their preferences. Unfortunately, several of its default settings make it more difficult to conduct reproducible data analyses. These settings can be changed using the Global Options GUI, but hunting down each setting and changing them one at a time is slow and tedious.

Instead, we’ll use the rstudio.prefs package, which will allow us to change all the settings at once by executing a few R commands in the console.

Copy and paste the code below into the console, and press the Enter key to run the code. This code will change several of RStudio’s default configuration options to make the program more user-friendly.

Important

Before changing your settings, R will print out your pending changes, and ask if you want to continue. You can indicate “Yes” by typing a y into the console, and pressing the Enter key.

library(rstudio.prefs)

use_rstudio_prefs(
  save_workspace = "never",
  load_workspace = FALSE,
  restore_last_project = FALSE,
  restore_source_documents = FALSE,
  check_for_updates = FALSE,
  color_preview = FALSE,
  rmd_viewer_type = "pane",
  rmd_chunk_output_inline   = FALSE
)

Create a project environment for SDS 100

Now that you have R and RStudio installed, we are going to set up your working environment to maximize your productivity.

The work that you do in RStudio should be organized into Projects. Working within projects allows you to switch contexts safely, and keep your work organized. We recommend that you have at least two projects:

  • one project for this class (SDS 100)
  • one project for the companion SDS class you are taking

You can switch between Projects in RStudio at any time using the Projects dropdown menu in the upper-right corner of your screen.

Instructions

Follow these instructions to create a new project in the RStudio Server:

  1. Copy the following code into the R console pane, and press the Enter key to run the code.

    usethis::create_project("~/SDS 100", open = FALSE)
  2. In the Files pane in the bottom right, you should see a new folder named “SDS 100” appear

    Click on this folder to open it.

  3. Inside the SDS 100 folder, you should see a file with a blue cube icon named “SDS 100.Rproj”

    Click on this file, and answer “Yes” when asked if you “Do you want to open the project ~/SDS 100?”

Congratulations, your SDS 100 project is created! In the upcoming weeks, make you select this project whenever you log in to the RStudio Server to work on tasks for SDS 100.

Your First Quarto Document

Quarto is a software program that can be used inside of RStudio which allows you write narrative text and R code together within the same document. A Quarto document is a bit like a Microsoft Word file or Google Doc that has an R console built right into it. This allows you to create a “final product” from all your data analysis work that contains all your R code and its output (like tables and figures) and all your written explanations. You can this final product and share it with other people, so they can see exactly what you did, and understand what your results mean.

Now, it’s time for you to open and render your very first Quarto document! You’ll be using Quarto documents to complete your lab work throughout this course, so we’ll start getting used to the workflow of rendering and turning in your final products.

Instructions

  1. Download this Quarto file.

  2. In the RStudio server, go to the Files pane and click the “Upload” button.

  3. In the pop-up window, click the “Browse” button, and use your computer’s file explorer to navigate to your Downloads folder.

    In your downloads folder, locate the lab_01_setup.qmd file you just downloaded, and select it for upload. Finally, press the OK button to upload the file.

  4. In the Files pane of the RStudio server, you should see a new file named lab_01_setup.qmd. Click on this file to open it in the Editor pane.

  5. Click on the render button button near the top middle of the Editor pane.

  6. Inspect the output. The output may appear in the bottom-right corner, or it might appear in a new tab in your web browser.

    How many checkmarks do you see? You should see 6 check marks.

  7. Download the HTML file produced by your Quarto document. First, go to the Files pane and check the box next to the file called lab_01_setup.html. Second, click the “More” button. Third, click the “Export” option in the dropdown menu.

    Lastly, press the “Download” button in the pop-up window to download the lab_01_setup.html file.

Next Steps

Step 1: Compare solutions

Each week, you will compare your solutions to ours. Does your lab_01_setup.html have the same outputs (checkmarks/messages) as the example solutions? The styling in the solutions file may look a bit different than yours, but the output should be the same.

Step 2: Complete the Moodle Quiz

Complete the Moodle quiz for this lab (Weekly Quiz 1). You can complete the quiz any time before 8:00 am on the day of the next course meeting.

Optional reading

To prepare for next week’s lab on working in R, we recommend reading Chapter 2: R basics and workflows.

References