Skip to content

Instantly share code, notes, and snippets.

@jnhutchinson
Created June 11, 2012 16:38
Show Gist options
  • Save jnhutchinson/2911139 to your computer and use it in GitHub Desktop.
Save jnhutchinson/2911139 to your computer and use it in GitHub Desktop.
example knitr.r
### KNITR SETUP
```{r setup, echo=FALSE}
opts_chunk$set(tidy=TRUE, cache=FALSE, highlight=TRUE, figalign="center", warning=FALSE, error=FALSE, message=FALSE, fig.height=11, fig.width=11)
```
### EXAMPLE CHUNK
```{r libraries}
library(ggplot2)
library(xtable)
```
### EXAMPLE ggplot generation and printing
```{r generate_plot}
colors <- factor(df$foo)
p <- ggplot(data=df, aes(x=xval, y=-log10(yval), label=symbol, color=colors)) + geom_point(size=8, alpha=0.7) + scale_colour_discrete(name = "legend title") + xlab("xlab") + ylab("ylab")
```
#### print out plot
```{r print_plot, fig.cap="figure legend"}
print(p)
```
### EXAMPLE table generation and printing
#### Table1 - Table title
```{r table, results='asis'}
table1 <- xtable(df, label=NULL, digits=c(1,1,1,6,0,1)) ## here you have 6 columns and "digits = the number of digits to round to for each column
print(table1, type="html",include.rownames=FALSE)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment