One of the technologies that OLI supports is the programming language R, for which it provides commands. The computers in the STEM Center (Room 3906) and some classroom have R installed, so you could just follow the instructions in OLI as written to complete the lab activities in OLI. Nonetheless, many people access R through another piece of helper software to help them manage code files and track variable values. Of the many such helper software available, we will use RStudio, which does even more. It also helps you generate a report of your work that integrates together your written prose with R commands and their results. This lab will introduce you to the basics of RStudio so that you can use it to complete an OLI activity using R and to generate a PDF lab report.

Your own computer probaly doesn’t have both R and RStudio installed already, and neither do the computers at Chabot at this time. Lucky for us, there is a version of RStudio that’s web-based, so that you can access it without going through any installation hassel. Because it is web-based, you can also run it on any computer with a compatible web browser. Even better, R is already part of this web-based RStudio, which is called RStudio Cloud. You should already have an RStudio Cloud account from following the instruction in the link Getting Started with Course Material; if you don’t yet have an account, click on the link, and follow Section III to create an account before continuing.

1 Setting up RStudio Cloud.

  1. Go to https://rstudio.cloud, and log in. You will then get to Your Workspace page showing your projects, of which you have none.

  2. Instead of creating a project from scratch, you can copy mine to cut down on the setup process. To do so, click on this link. After the URL loads, you will see something like this:

  3. A pane in RStudio is a window within it. Note that the screentshot above shows three panes. The left pane has three tabs: Console, Terminal, and Jobs. The right side has two panes stacked on top of each other.

  4. Near the top is the word temporary. Click to its right to save a permanent copy. Now you have an RStudio Cloud project called MTH 43 for this class.

In this lab, we modify an OLI activity to help you run R in RStudio and learn some of its functions. so I have put the following notes, matching the numbered paragraphs in the screenshot, to help you work in RStudio.

2 RStudio Notes on OLI’s R Instructions

You are now ready for your first OLI acivity using R. Open the learning by doing activity at the bottom of p. 18 in OLI. Below are some notes that modifies OLI’s instruction to work with RStudio Cloud. They are numbered to match a screenshot below of the learning by doing activity. As you work through the activity, read the corresponding note here BEFORE you read the OLI instruction.

2.1 Note 1

Make sure OLI gives R instructions. If not, you can find them by clicking on the little triangle that points down and select R.

2.2 Note 2

This paragraph in OLI is about loading the data for this activity. You can ignore this paragraph, and follow instructions here instead.

  • The data file for this lab is already in the RStudio Cloud project MTH 43.
    • The File tab in the lower right-hand pane shows the project files.
    • Single click on the folder name data, and you can see the file actor.RData, which is what the original OLI instruction wants you to download.
    • Later, we will show you how to upload a file into an RStudio Cloud project.
  • To load actor.RData,
    • go to the File menu near the upper left-hand corner in RStudio Cloud, and select Open File.
    • A window will pop up, showing the files in the project.
    • Find actor.RData in the data folder. Select the data file, and then click Open at the bottom of the window.
    • A confirmation dialog box will pop up to make sure you really want to open the data file; click Yes.

2.3 Note 3

An data frame object in R is just what we know as a dataset with some special programming features that we won’t get into now. Fundamentally, it’s a table with rows of observations and columns of data variables.

Whenever OLI asks you to enter a command, you will enter it in the Console tab in the left-hand pane, after the prompt > at the bottom. Names of data frames can be used as a command to see the data in them.

Now read the paragraph corresponding to this note in the OLI activity. After following its instructions, return here to read the rest of Note 3.

RStudio also provides a graphical user interface to see the data frame actor_age. In the Environment tab of the upper right-hand pane, you will see the data frame actor_age.

  • Click on to the left of actor_age to toggle expanding/collapsing information about that data frame. In this case, you can see that the data frame contains one variable age, which is an integer. The first several values of age is shown.

  • To see all the values in the data frame, click on the name actor_age. A new upper left-hand pane appears, displaying actor_age as a table, pushing down the Console tab to the lower left-hand pane.

You will be ready to complete the rest of the OLI learn by doing activity to produce histograms, after reading this paragraph. The histrograms will appear in the Plots tab in the lower right-hand pane. The blue left and right arrows in the Plots’ toolbar helps you retrieve your grahical outputs; you don’t have to worry about the x11() code mentioned in the note of the OLI instructions. After you complete the activity in OLI, return here for the rest of the lab.

3 Files Tab in RStudio

3.2 Renaming a folder

While you can keep all your files for different labs in the same folder, using separate folders for different labs helps you organize your work. We will rename the folder data to LBD_p18_Histogram.

  • Check the box to the left of data folder.
  • Click on Rename in the toolbar, and enter the new name.

3.3 Uploading a file to an RStudio Cloud project

Here is a bit more background information about RStudio Cloud. A project in RStudio Cloud is a copy of RStudio running on an independent virtual computer in the cloud, accessed through a browser. Treat it as if it were a separate computer. Unlike the RStudio softare that is installed on a physical computer, RStudio Cloud cannot access files on the physical computer you are working on. You must upload files into an RStudio Cloud project before it can be used.

First download the file you need from Canvas:

  • Log into Canvas. If for some reason, you don’t have access to the Canvas site to this course, you can click on this link to get the file, and skip the rest of the instructions.
  • In the left sidebar, click on Assignments.
  • Click on the assignment Lab: Histogram, R, RStudio, and R Markdown.
  • In the assignment description is a link to the file LBD_p18_Histogram.Rmd. Click on it to download it to the physcal computer or your jump drive. If you don’t have a jump drive and are not working on your own computer, the best practice is to put all your files in a folder, and delete that folder before you leave the computer. Of course, before deleting, back up your files by uploading them to a cloud service like Google Drive (which you have with your Chabot Zonemail account and is the preferred option) or e-mailing them to yourself.

Then upload the file:

  • In RStudio Cloud, navigate to the folder \project\LBD_p18_Histogram in the Files tab in the lower-right pane.
  • click on Upload in the toolbar. A dialog box will appear.
  • Click on Choose File button,and find the file your just downloaded, and complete the upload process by following directions on the screen.

4 R Markdown in RStudio

The file LBD_p18_Histogram.Rmd is an R Markdown file, marked by the extension .Rmd. R Markdown is a text formatting language that integrates the execution of R code. RStudio makes it easy to turn an R Markdown file into an output format like HTML, PDF, or Word. By integrating R into your written work, you can show others your computing process as well as well as display outputs directly into your document.

4.1 Opening .Rmd file

Click on the file LBD_p18_Histogram.Rmd in the Files tab in the lower right-hand pane in RStudio. It will pop up in the upper left-hand pane.

4.2 Rendering .Rmd file

To render a file means to convert it to a desired output format. In the toolbar for the pane showing LBD_p18_Histogram.Rmd, click on the down triangle to the right of Knit in the toolbar to reveal the Knit dropdown menu, and then select Knit to HTML. A new window will show the content of the .Rmd file as a webpage. If necessary, disable the browser’s pop-up blocker.

4.3 Deciphering R Markdown

Reading and comparing side by side the file LBD_p18_Histogram.Rmd and the rendered webpage, stopping before the section R Documentation. Then make some observations about how the .Rmd file affects the appearance of the rendered webpage. The bullets below list observation you may make, as well as other information about .Rmd files. Line reference refers to the .Rmd file.

Take some time to make your own observations before reading further.

4.3.2 Formatting

  • Written text, computer code, and the code’s output are integrated into the same document.
  • Adding a URL link is easy (Line 11).
  • To make text in webpage appear in gray like code, enclose the text in backward single quotes (line 20).
  • Note the ability to bold (Line 21) or italicize (Line 22), and even to both bold and italicize together (Line 18).

4.3.3 Code chunk

There are two chunks of code, lines 14-16 and lines 24-27.

  • A code chunk appears gray in both the text editor in RStudio and in the rendered webpage.
  • In .Rmd, code chunks begin and end with three backward single quotes, with options for the chunk listed inside braces {}.
    • The r indicates that the code is written in the programming language R.
    • The name that follows r is the label identifying the chunk. The first chunk is appropriately labeled setup, as it loads the data to set up our work. The second chunk is labeled histograms. Labels for code chunks are optional.
  • A code chunk in .Rmd may contain multiple commands, as in lines 25-26. In the rendered webpage, the output of a command, if any, appears right after the command.

4.4 Running code chunk.

When you Knit to HTML, the code chunks are executed in sequence in a separate fresh environment, as if you have a new RStudio project running with no data loaded.

At the upper right-hand corner of a code chunk, you can see some icons. To see what they do, just move your mouse cursor on top of each, and a description will appear. Identify which icon is Run Current Chunk and which is Run All Chunks Above. They allow you to run code chunks interactively. Do the following to learn about running code chunks.

  1. Let’s start over and wipe away all the data that you loaded. Go to Environment tab in the upper-right pane of RStudio. Click on the broom icon in the toolbar, and confirm removal of objects.
  2. Return to LBD_p18_Histogram.Rmd in the text editor to the left. In the code chunk labeled histograms, execute the chunk either (a) by clicking Run Current Chunk or (b) by placing your cursor inside the chunk and pressing Ctrl+Shift+Enter. You will see an error.
    • When you run an individual chunk, RStudio executes the code line by line in the console. As we deleted the data frame from which the histogram is based, the code did not work.
    • The code execution stops as soon as an error occurs. The line number with the error is marked red.
    • Read the error message carefully. It can help you figure out what’s wrong.
  3. Go back to the code chunk labeled histograms. Click on Run All Chunks Above, which loads the data frame actor_age. Then Click on Run Current Chunk. An output chunk appears below the code chunk.
    • Thumbnail images of the output of the two commands in the chunk are across the top of the output chunk.
    • The output of the last command in the chunk is the largest output shown.
    • The small icons on the upper right-hand corner of the output chunk allow you to manage the output. Try clicking on them to learn what they do.

5 Editing R Markdown

You will add your responses and results to some tasks listed in the LBD_p18_Histogram.Rmd file. Follow the instruction in that file starting with the section R Documentation, where you stopped reading earlier.