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.
Go to https://rstudio.cloud, and log in. You will then get to Your Workspace page showing your projects, of which you have none.
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:
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.
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.
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.
Make sure OLI gives R instructions. If not, you can find them by clicking on the little triangle that points down and select R.
This paragraph in OLI is about loading the data for this activity. You can ignore this paragraph, and follow instructions here instead.
actor.RData in the data folder. Select the data file, and then click Open at the bottom of the window.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.
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.
Rename in the toolbar, and enter the new name.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:
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:
\project\LBD_p18_Histogram in the Files tab in the lower-right pane.Upload in the toolbar. A dialog box will appear.Choose File button,and find the file your just downloaded, and complete the upload process by following directions on the screen.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.
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.
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.
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.
Lines 1-9: This contains header information about the file, only some of which appear when you render the file.
output section shows the options for the output format. You can see that this R Markdown is rendered into HTML, R Notebook, and PDF formats using the default options for each format..Rmd is rendered into a webpage without running any of the R code in it. If code output is shown in the preview, it is the result from the last time the file was rendered.There are two chunks of code, lines 14-16 and lines 24-27.
.Rmd, code chunks begin and end with three backward single quotes, with options for the chunk listed inside braces {}.
r indicates that the code is written in the programming language R.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..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.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.
Environment tab in the upper-right pane of RStudio. Click on the broom icon in the toolbar, and confirm removal of objects.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.
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.
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.