{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
{r cars} summary(cars)
You can also embed plots, for example:
{r pressure, echo=FALSE} plot(pressure)
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
{r } set.seed(1234) library(ggplot2) 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:
```{r}
```
```{r }
x <- 1:10
y <- round(rnorm(10, x, 1), 2)
df <- data.frame(x, y)
df
```
```{r basicconsole}
x <- 1:10
y <- round(rnorm(10, x, 1), 2)
df <- data.frame(x, y)
df
```
### Simple plot Here is a basic plot using base graphics:
```{r }
plot(x)
```
```{r simpleplot}
plot(x)
```
Also, unlike traditional Sweave, you can include multiple plots in one code chunk:
```{r }
boxplot(1:10~rep(1:2,5))
plot(x, y)
```
ggplot2 plotGgplot2 plots work well:
```{r ggplot2ex}
qplot(x, y, data=df)
```
lattice plotAs do lattice plots:
```{r latticeex}
xyplot(y~x)
```
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).
```{r , results='asis', echo=FALSE}
cat("Here are some dot points\n\n")
cat(paste("* The value of y[", 1:3, "] is ", y[1:3], sep="", collapse="\n"))
```
```{r dotpointprint, results='asis', echo=FALSE}
cat("Here are some dot points\n\n")
cat(paste("* The value of y[", 1:3, "] is ", y[1:3], sep="", collapse="\n"))
```
```{r , results='asis', echo=FALSE}
cat("x | y", "--- | ---", sep="\n")
cat(apply(df, 1, function(X) paste(X, collapse=" | ")), sep = "\n")
```
```{r createtable, results='asis', echo=FALSE}
cat("x | y", "--- | ---", sep="\n")
cat(apply(df, 1, function(X) paste(X, collapse=" | ")), sep = "\n")
```
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="##").
```{r echo=FALSE, comment="", echo=FALSE}
head(df)
```
The following is an example of a smaller figure using fig.width and fig.height options.
```{r , fig.width=3, fig.height=3}
plot(x)
```
```{r smallplot, fig.width=3, fig.height=3}
plot(x)
```
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
```{r , cache=TRUE}
for (i in 1:5000) {
lm((i+1)~i)
}
```
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:
and nested dot points:
Equations are included by using LaTeX notation and including them either between single dollar signs (inline equations) or double dollar signs (displayed equations). If you hang around the Q&A site CrossValidated you’ll be familiar with this idea.
There are inline equations such as \(y_i = \alpha + \beta x_i + e_i\).
And displayed formulas:
\[\frac{1}{1+\exp(-x)}\]
knitr provides self-contained HTML code that calls a Mathjax script to display formulas. However, in order to include the script in my blog posts I took the script and incorporated it into my blogger template. If you are viewing this post through syndication or an RSS reader, this may not work. You may need to view this post on my website.
Tables can be included using the following notation
| A | B | C |
|---|---|---|
| 1 | Male | Blue |
| 2 | Female | Pink |
Here’s an example image:
image from redmond barry building unimelb
Here is Markdown R code chunk displayed as code:
```{r}
x <- 1:10
x
```
And then there’s inline code such as x <- 1:10.
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,