Jonathan C. Johnson

E-mail: jonathan.johnson10@okstate.edu
Office: MSCS 524
Department of Mathematics
Oklahoma State University
Return to MATH 4910/5010 Homepage

MATH 4910/5010 - Practice Lab

This lab is a non-graded lab for you to get practice with completing assignments with R markdown files. You can find the R markdown template and data files for this lab on Canvas in the course files under the "Practice Lab" folder. Follow the instructions below.

Opening R Markdown File

An R Markdown file(.Rmd) is a document format that allows you to blend together narrative text, code, and the output of that code in a single document. R Markdown is particularly popular among data scientists, researchers, and analysts because it enables the creation of dynamic and reproducible reports.

  1. Run the RStudio application.
  2. Under the "Tools" menu, select "Global Options...".
  3. In the R console type the following commands to install the knitr and rmarkdown packages:
    > install.packages("knitr")
    > install.packages("rmarkdown")
  1. In the "Default working directory" text box, input the folder in which you would like to store all of your R files.
  2. Find your working directory on your system and create a folder to store the files for this lab. Give this folder an appropriate name like "practice lab".
  3. From canvas download practice_lab.Rmd and university_rankings.csv into the folder you just created.
  4. Open practice_lab.Rmd in RStudio. You screen should look like this.

Writing Code and Knitting HTML File

Now that you've opened the template in RStudio, we can complete the practice lab tasks. In each lab, most tasks, like the next one, will be in plain black text. These tasks do not require editting the .Rmd file.

  1. Type the following command in the R console. This should be in the bottom-left window.
    > Sys.Date()

Let's turn our attention to the .Rmd file, beginning with completing the header section of the file. Tasks that require editting the .Rmd file, like this next one, will be in green and boxed off.

  1. Fill in your name to the right of author:. Then, fill in today's date to the right of date:.

After the header, R Markdown files contain several section separated by '''{r} and '''. The sections folling ''' with white backgrounds is where plain text which will be displayed in the output file is written. As you may have noticed, many of these section have instructions in them. The sections following '''{r} with grey backgrouds is where you will write code. Each of these sctions is labeled with "Code block" and a number to help navigate the file. Comments can be added to the code sections by using the # symbol.

Lets complete the following tasks in the .Rmd file. Since we're just starting, I've provided code for each task. Don't worry if you don't understand the code. You will learn more about these commands in later labs.

  1. In code block 1, write code to print "Howdy Cowboy!!" to the screen.
    print("Howdy Cowboy!!")

If you want to run a chunk of code from your .Rmd file in the console, you can click the play button in the upper right corner of the chunk.

  1. In code block 2, write code to plot points on the graph y=x^2.
    xvals <- -5:5
    yvals <- xvals^2
    plot(xvals,yvals)

You can next task involves loading data into R. To do this, we will first need to install a package.

  1. In the R console, type the following command to install the tidyverse package:
    > install.packages("tidyverse")

  1. In code block 3, write code to load the US News university ranking data. Then, list the top 15 entries along with their rankings from 2018 to 2023.
    library(tidyverse)
    uni_rank <- read.csv("university_rankings.csv")
    uni18_23<-select(uni_rank,University.Name,X2023:X2018)
    slice_head(uni18_23,n=15)

Some tasks involve answering questions inside the .Rmd file. These will involve filling a [Answer Here] section of the file.

  1. Aren't R markdown files neat? Fill in response in [Answer Here].

Once you've completed all the task and are satisfied with the results you can knit an .html file from the .Rmd file.

  1. Save your .Rmd file. Then, select "Knit" from the file editor window. An .html file which looks like this should appear.

Uploading Completed Files

Now that we have completed all the task. We can upload the results to Canvas.

  1. Navigate to the "Pratice Lab" assignment on the course Canvas page.
  2. Click the "Start Assignment" button.
  3. Click the "Choose File" button. Then, find and select practice_lab.Rmd.
  4. Select "Add Another File". Then, upload practice_lab.html in similar fassion.
  5. Finally, click the "Submit Assignment" button.

Congradulations! You've successfully submitted a lab assignment.