Lab 1 Jan 09 2019

Learning Objectives

For all homeworks in this class I’ll expect you to submit a PDF that has been “Knit” from an Rmarkdown document. This ensures your work has a minimal level of reproducibility and that the TA and I can see exactly how you reached your answers. It’s also a great skill to learn and used extensively in industry.

By the end of today’s lab, you should be able to:

rstudio.cloud

rstudio.cloud is a web-based version of R and RStudio. Today you’ll start from a project I’ve created, but by the end you will have created your own project ready for Homework 1.

Getting started

The TA will walk you through these steps to get started.

  1. Get a free rstudio.cloud account, by signing up at rstudio.cloud.

  2. Join the class workspace by following this link: ST552 Winter 2019.

  3. Click on the project “Lab 1” to open it.

  4. You’ll see a flashing “Temporary Copy” sign, save a copy to get your own version of this project. You only need to do this once, then you’ll be able to re-access your version from the class workspace.

You are now in your own copy of the project, and it should look much like RStudio where you have used it before. I’ve included some files in this project, you should see them in the Files pane.

(Why are we using rstudio.cloud?)

Rmarkdown

We’ll use Rmarkdown to generate our reports. Rmarkdown files combine markdown (a kind of plain text markup language) with chunks of R code. When compiled, the R code in the file is evaluated, then the results are woven into the markdown. Markdown is flexible enough that it can then be turned into a pdf (via LaTeX), a Word document, or an html file (for hosting on the web for instance, like this lab!).

Let’s learn by example. The TA will walk you through this part:

  1. Open 01-getting-started-rmarkdown.Rmd

  2. Skim through the file. This is just a plain text file with some special syntax. TA will point out:

    • YAML header

      ---
      # Some options that control the output
      ---
    • code chunks

      ```{r}
      # R CODE HERE
      
      ```
    • Markdown syntax in the plain text parts, e.g. ## Section Headings and `code`.

  3. Hit the Knit button to “Knit” this Rmarkdown to PDF. All going well a PDF file will appear (you’ll also find it in the Files pane as 01-getting-started-rmarkdown.pdf). Give it a quick skim. TA will point out:

    • code chunks are “echoed”,
    • the results from code chunks are included shown after the code
    • … same for figures
    • the special Markdown syntax has generated formatting like sections and text that looks like code.
  4. You don’t have to “Knit” a document to see the results of running code, if your cursor is inside a code chunk, you can hit Crtl/Cmd + Enter to run the current line, or Crtl/Cmd + Shift + Enter to run the whole chunk. In fact, this is usually how you work as you build an Rmarkdown document.

Your turn Make some changes in the Rmarkdown re-Knit and check the changes propagated. You might like to:

Debugging problems with Knitting

Errors when you Knit can be a little hard to track down because the error could be a code error, or an error from converting to the output you’ve chosen. If you run into an error, a good first step is to rule out code errors.

Rule out code errors, by going to Run -> “Restart R and Run All Chunks”. Any code errors should raise their heads and you can fix them before trying to Knit again.

The code in an Rmarkdown document is run in a clean R session, so the most common mistakes are forgetting to load a package, or relying on objects you have in your workspace but that aren’t defined in your document.

Your Turn Open 02-errors.Rmd and Knit it. You’ll get an error, try the strategy outlined above to track down the error, fix it, and re-Knit. (There’s more than one problem you you might need to iterate through this process).

Starting from scratch

Let’s get you ready to embark on a new Rmarkdown file:

  1. Create a new Rmarkdown file: File -> New File -> R markdown. Provide a title (how about “Homework 1”?), your name as the author, and select PDF output. A new file will open with some contents designed to remind you how to use Rmarkdown. Save it with an informative name.

  2. You probably don’t want this template contents, so delete everything below the header, and start a new section called Q1.

  3. You can easily insert a code chunk with the shortcut Ctrl/Cmd + Alt/Option + I. Then you can write R code inside it. How about loading the faraway package?

  4. Take a look at Homework 1. This first task is to complete an initial analysis of the pima dataset. Perhaps you might start with looking at a few values. Create a new code chunk, and include:

    head(pima)

    Practice running the code with a the keyboard shortcut. What do you notice about the data? You’ll probably find looking at ?pima useful. You might make a few notes to yourself in the Rmarkdown.

  5. It’s a good time to check our file still Knits. Knit it to check.

And that’s basically how you proceed to complete your homework…

Looking ahead

Sometimes I’ll provide you an entire project, like I did today, because I can make sure you have the right packages and provide some files. But, you’ll also want to maintain your own projects for submitting homeworks.

The easiest approach to begin with, is to create a new project in the class workspace (this ensures you’ll have all the needed packages pre-installed), and do all your homeworks inside as individual Rmarkdown documents. You can use folders inside a project to help organise things.

If you create a project in your private workspace, you will have to install the packages you need.


Why are we using rstudio.cloud?

Because it makes my life easier:

  • I can easily pre-install packages for you
  • I can deliver entire collections of files to you
  • You’ll all be working in the same environment

But hopefully it also makes your life easier:

  • You don’t have to manage an install on your computer of R, RStudio and latex.
  • You can access your work and pick up where you left off from any computer with internet access.

It does have some downsides:

  • If you don’t have internet, or for some reason the cloud goes down, you can’t access your work
  • You have to manage getting files on and off the cloud

You are welcome to use your own local install of R and RStudio but getting everything you need installed will be up to you.