Troubleshooting R and R Markdown Errors


Chester Ismay (cismay@reed.edu)

Chem Tutors Basic Training

Slides available at http://rpubs.com/cismay/debug_chem_r

What’s going on with Chem 101/102 labs?

Four pieces

  1. YAML header
  2. Markdown text
  3. R chunks
  4. Resulting PDF (built from \(\LaTeX\))

YAML header

YAML

What is Markdown?

  • A “plaintext formatting syntax”
  • Type in plain text, render to more complex formats
  • One step beyond writing a txt file
  • Render to HTML, PDF, etc.

What does it look like?

  # Header 1
  
  ## Header 2
  
  Normal paragraphs of text go here.
  
  
  **I'm bold**
  
  [links!](http://rstudio.com)
  
   * Unordered
   * Lists   
   
  And  Tables
  ---- -------
  Like This
  
markdown

What is R Markdown?

  • “Literate programming”
  • Embed R code in a Markdown document
  • Renders textual output along with graphics

```{r}
x <- rnorm(100)
length(x)
hist(x)
```
## [1] 100

R chunks

  • Variable creation / Loading data
  • Calculations / modeling
  • Plots
  • Tables

Variable creation / Loading data

A <- c(25.75, 38.93, 27.61, 7.30)

Calculations / modeling

ave_Cu <- mean(CuClass)
fit1 <- lm(Abs ~ Cu + 0)

Plots

plot(Ratei2 ~ Ci,
     xlab = "X Axis Label",
     ylab = "Y Axis Label",
     pch = 20) 

Tables

bold_all_col_names <- function(x) {paste('\\textbf{', x, '}', sep ='')}
title <- 'Results for the Mass \\% for a Copper Sulfate Complex of form $CuSO_4(NH_3)_x(H_2O)_y$'
print(xtable(Class, 
             caption = title, 
             align = "|r|r|r|r|",  
             digits = c(0, 0, 0, 0),
             display = c("d", "f", "f", "f")
             ), 
      caption.placement = 'top',  
      hline.after = seq(-1, nrow(Class)),  
      include.rownames = FALSE,
      comment = FALSE,
      caption.width = '4in',
      sanitize.colnames.function = bold_all_col_names 
      )

What formats can R Markdown output to?

rmd pipeline

But…how do I troubleshoot errors!?!?

Let’s break our code and see!

When am I available?

  • Email me at cismay@reed.edu or chester.ismay@reed.edu to schedule a time to meet if office hours don’t work
  • Spring 2016 office (ETC 223) hours
    • Mondays (10 AM to 11 AM)
    • Wednesdays (1:30 PM to 2:30 PM)
  • Sometimes available for virtual office hours via Google Hangouts (email me for details)

Resources

Markdown Cheatsheet

R Markdown Reference Guide

Thanks!


cismay@reed.edu


  • Slides available here