Title

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.

Getting started

To work with R Markdown, if necessary:

  • Install R
  • Install the lastest version of RStudio (at time of posting, this is 0.96)
  • Install the latest version of the knitr package: install.packages("knitr")

To run the basic working example that produced this blog post:

  • Open R Studio, and go to File - New - R Markdown
  • If necessary install ggplot2 and lattice packages: install.packages("ggplot2"); install.packages("lattice")
  • Paste in the contents of this gist (which contains the R Markdown file used to produce this post) and save the file with an .rmd extension
  • Click Knit HTML

Prepare for analyses

set.seed(1234)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.0.3
library(lattice)

Basic console output

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.

Simple plot

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.

Multiple plots

Also, unlike traditional Sweave, you can include multiple plots in one code chunk:

```r
boxplot(1:10 ~ rep(1:2, 5))
```

![plot of chunk unnamed-chunk-4](figure/unnamed-chunk-4.png) 

```r
plot(x, y)
```

```
## Error: object 'x' not found
```
boxplot(1:10 ~ rep(1:2, 5))

plot of chunk multipleplots

plot(x, y)
## Error: object 'x' not found

ggplot2 plot

Ggplot2 plots work well:

qplot(x, y, data = df)
## Error: ggplot2 doesn't know how to deal with data of class function

lattice plot

As 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.

R Code chunk features

Create Markdown code from R

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

Create Markdown table code from R

x | y — | —

## Error: dim(X) must have a positive length

x | y — | —

## Error: dim(X) must have a positive length

’ Control output display

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 }                                               

Control figure size

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

Cache analysis

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

Basic markdown functionality

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.

Dot Points

Simple dot points:

  • Point 1
  • Point 2
  • Point 3

and numeric dot points:

  1. Number 1
  2. Number 2
  3. Number 3

Quote

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,

‘dnsajndkjsdn #’

’` hjGJH fsdajfhkds

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.


************