Rmarkdown, A Simple Example
This post is an example showing how to write a post in R markdown. In particular, there is an Rmd block that generates a figure and a second Rmd block that generates a nicely formatted table.
YAML
Start with the usual YAML header:
---
layout: post
title: "R Markdown Example"
date: 2015-10-01 19:00:00
tags: knitr authoring markdown R CRAN example
---
Plots
Next add the Rmd to generate a plot:
```{r qplot, fig.width=6, fig.height=4.5, dev='svg', echo=TRUE}
library(ggplot2)
summary(cars)
qplot(speed, dist, data=cars) + geom_smooth()
```
output
library(ggplot2)
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
qplot(speed, dist, data=cars) + geom_smooth()
Tables
And the Rmd for a table:
```{r xtable, results="asis", echo=TRUE}
library(xtable)
options(xtable.html.table.attributes = "class='foobar'")
n <- 100
x <- rnorm(n)
y <- 2*x + rnorm(n)
out <- lm(y ~ x)
tab <- xtable(summary(out)$coef, digits=c(0, 2, 2, 1, 2))
print(tab, type="html")
```
output
library(xtable)
options(xtable.html.table.attributes = "class='foobar'")
n <- 100
x <- rnorm(n)
y <- 2*x + rnorm(n)
out <- lm(y ~ x)
tab <- xtable(summary(out)$coef, digits=c(0, 2, 2, 1, 2))
print(tab, type="html")
Estimate | Std. Error | t value | Pr(>|t|) | |
---|---|---|---|---|
(Intercept) | 0.08 | 0.10 | 0.8 | 0.40 |
x | 1.91 | 0.09 | 20.2 | 0.00 |