In some instances, I include a copy of the R Markdown in the displayed HTML, but most of the time I assume you are reading the source and post side by side.
To work with R Markdown, if necessary:
knitr
package: install.packages("knitr")
To run the basic working example that produced this blog post:
ggplot2
and lattice
packages: install.packages("ggplot2"); install.packages("lattice")
.rmd
extensionset.seed(1234)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.0.3
library(lattice)
To insert an R code chunk, you can type it manually or just press Chunks - Insert chunks
or use the shortcut key. This will produce the following code chunk:
Pressing tab when inside the braces will bring up code chunk options.
The following R code chunk labelled basicconsole
is as follows: ## Plots Images generated by knitr
are saved in a figures folder. However, they also appear to be represented in the HTML output using a data URI scheme. This means that you can paste the HTML into a blog post or discussion forum and you don’t have to worry about finding a place to store the images; they’re embedded in the HTML.
Here is a basic plot using base graphics:
plot(x)
## Error: object 'x' not found
plot(x)
## Error: object 'x' not found
Note that unlike traditional Sweave, there is no need to write fig=TRUE
.
Also, unlike traditional Sweave, you can include multiple plots in one code chunk:
```r
boxplot(1:10 ~ rep(1:2, 5))
```

```r
plot(x, y)
```
```
## Error: object 'x' not found
```
boxplot(1:10 ~ rep(1:2, 5))
plot(x, y)
## Error: object 'x' not found
ggplot2
plotGgplot2 plots work well:
qplot(x, y, data = df)
## Error: ggplot2 doesn't know how to deal with data of class function
lattice
plotAs do lattice plots:
xyplot(y ~ x)
## Error: object 'y' not found
Note that unlike traditional Sweave, there is no need to print lattice plots directly.
The following code hides the command input (i.e., echo=FALSE
), and outputs the content directly as code (i.e., results=asis
, which is similar to results=tex
in Sweave).
Here are some dot points
```
## Error: object 'y' not found
```
Here are some dot points
## Error: object 'y' not found
x | y — | —
## Error: dim(X) must have a positive length
x | y — | —
## Error: dim(X) must have a positive length
The folllowing code supresses display of R input commands (i.e., echo=FALSE
) and removes any preceding text from console output (comment=""
; the default is comment="##"
).
1 function (x, df1, df2, ncp, log = FALSE)
2 {
3 if (missing(ncp))
4 .External(C_df, x, df1, df2, log)
5 else .External(C_dnf, x, df1, df2, ncp, log)
6 }
1 function (x, df1, df2, ncp, log = FALSE)
2 {
3 if (missing(ncp))
4 .External(C_df, x, df1, df2, log)
5 else .External(C_dnf, x, df1, df2, ncp, log)
6 }
The following is an example of a smaller figure using fig.width
and fig.height
options.
plot(x)
## Error: object 'x' not found
plot(x)
## Error: object 'x' not found
Caching analyses is straightforward. Here’s example code. On the first run on my computer, this took about 10 seconds. On subsequent runs, this code was not run.
If you want to rerun cached code chunks, just delete the contents of the cache
folder
For those not familiar with standard Markdown, the following may be useful. See the source code for how to produce such points. However, RStudio does include a Markdown quick reference button that adequatly covers this material.
Simple dot points:
and numeric dot points:
Let’s quote some stuff:
To be, or not to be, that is the question: Whether ’tis nobler in the mind to suffer The slings and arrows of outrageous fortune,
2 + 3
## [1] 5
A friend once said:
It’s always better to give than to receive.
“–” and “—” “bdsjahgdjsgdjasd”
This text is displayed verbatim / preformatted
strikethrough
options(keep.blank.line = FALSE)
A friend once said:
> It's always better to give
> than to receive.