Source file ⇒ MarkdownCleanUp.Rmd

Announcements

Thoughts about formatting

R Markdown Formatting Basics

If you hang around RMarkdown long enough, you’ll hear a lot about knitr, yaml, pandoc, and other odd terms related to configuring the resulting document produced. Even if it all feels a bit jumbled together, you can still learn to troubleshoot and accomplish what you’re after in most cases. It’s probably best to tackle most of these things only when the need arrises.

Tables

We’ll use the mtcars data for illustration. We used these data last week when discussing Base R, and had fit a regression model among other things. We’ll pick up there again.

Here’s a look at the first few rows:

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

If we want to make that look a little nicer, you could wrap it in a kable() function from the knitr package:

kable(head(mtcars))
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

There are even arguments to kable() if you want it to round all numbers, change column alignment, include a caption, etc. Other functions exist in the stargazer and xtable packages if you’d like some other options.

Chunk Options

Maybe now, I decide that I want the table, but want to suppress the actual R chunk from appearing in the output file. We can do this by adding echo=FALSE as a chunk option:

mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

Maybe you want to go the other direction, and you’d like to show & evaluate the code without actually printing the result:

kable(head(mtcars))

If you have several commands in sequence, sometimes the code chunk will break in the middle to print interim results. If you want to just print them after the entire chunk you can use hold in the results argument where hide appears now. Other chunk options that you might want to play with are include, eval

Last time we made a residual plot to accompany a simple linear regression model:

require(ggplot2)

model <- lm(mtcars$mpg ~ mtcars$wt)   # this is a model predicting miles per gallon from weight of the car

ggplot() + 
  geom_point(aes(x = model$fitted.values, y = model$residuals))

If we have several graphs in the document and would like to change the size for this one, we can do that as a chunk option:

ggplot() + 
  geom_point(aes(x = model$fitted.values, y = model$residuals))

Global arguments

You can use a code chunk to set global options as follows:

\```{r include=FALSE}
knitr::opts_chunk$set(echo = TRUE) 
\```

You can also set some global options in the “YAML header” at the top of your Rmd document by specifying key-value pairs that are interpreted when the document is rendered. The gear next to the “Knit” button in RStudio opens a window to help you to configure common global options for the document. You can find other key-value pairs for more advanced configuration in the RMarkdown cheat sheet, RMarkdown user manual, Google Searches, etc. As an example, here’s what the yaml header looks like for this document. You can see several key-value pairs dictating the global default for figure dimensions in addition to specifying the title, author, date, etc. By the way, the indenting matters… if you delete the spaces before “html_document” you won’t be able to render the document.

---
title: "Markdown Clean Up"
author: "Matt Beckman"
date: "December 7, 2016"
output:
  html_document:
    fig_height: 4
    fig_width: 6
---

You can even impose a global formatting template by calling a separate file with a .css extension in the yaml header. Then if you want to change the formatting template, all you have to do is switch the .css file!

Knitting to other document types