Jessica Minnier, BSR Reproducible Research Journal Club
August 17, 2016
If we have a .Rmd file with code and text, we can use the knit
command to run the R code and convert the document to html:
library(knitr)
knit("myfile.Rmd")
or we can use the “Knit HTML” button in Rstudio which uses the knitr
package to do the same thing.
Three parts:
{r} and ends with
)This is a code chunk with no name, no options, but it will run:
```{r}
x = rnorm(100)
plot(x)
```
This is a code chunk with name and chunk options:
```{r normaloutput, fig.width=5, echo=FALSE}
x = rnorm(100)
plot(x)
```
fig.keep='all'
)fig.show='animate'
fig.width
and fig.height
for examplecache=TRUE
---
title: "Habits"
author: "Jane Doe"
output:
html_document:
theme: united
toc: true
toc_depth: 2
toc_float: true
code_folding: hide
---
toc
, change theme appearance theme
or with custom CSS files, include other code in header (i.e. latex code or html code), figure options with fig_width
and fig_height
eval=FALSE
)dothis
earlier in the document, you can even use eval=dothis
' '
and periods .
in chunk labels and directory namesAn example:
```{r eval=TRUE, fig.width=5, echo=FALSE}
x = rnorm(100)
plot(x)
```
Also set global cunk options in the beginning
```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=FALSE, warning=FALSE, message=FALSE)
```
Common:
echo=FALSE Don't include the code
results="hide" Don't include the output
include=FALSE Don't show code or output
eval=FALSE Don't evaluate the code at all
warning=FALSE Don't show R warnings
message=FALSE Don't show R messages
fig.width=# Width of figure
fig.height=# Height of figure
fig.path="Figs/" Path for figure files
Advanced:
cache=TRUE Cache code chunk
results="asis" Use with pandoc.table or other html generating code
tidy=TRUE Tidy up your code output
fig.show="hide" Generate plot files but hide them in report
dev="png" Change file type of figure/plot
purl()
)You can extract all R code from your knitr document into an R file, with or without documentation.
library(knitr)
setwd("slides-info-reproducible-research/study-group-2016/2016_08_10_intro-rstudio-knitr/")
purl("minnier-2016-08-17-knitr.Rmd", output="minnier-2016-08-17-knitr-no-doc.R")
purl("minnier-2016-08-17-knitr.Rmd",
output="minnier-2016-08-17-knitr-with-doc.R",
documentation = 2)
(Since all the code in this file has eval=FALSE
the R code is commented out. Try with a regular .Rmd file to see what evaluated code looks like.)
spin()
)#' # This is a header
#'
#' I can write comments like this that will be turned into
#' markdown text.
#+ chunkname, fig.width=10
x = rnorm(100)
# regular R comment, within my code chunk
plot(x)
#' This is the end of the report. Now click "Compile Notebook"
#' button in Rstudio. Or use `spin()` function
#' in knitr package, and we will have a weaved html document!
#' Not as pretty as regular .Rmd+knitr but we can add
#' many options to make it just as nice, even YAML!
#'
You can compile this in Rstudio (notebook button) or R with the function spin()
:
library(knitr)
spin("example_notebook_code.R")
knitr
Instead of using Rstudio’s built in buttons, we can perform all the same tasks in pure R code.
To knit a .Rmd file to .md and then convert to .html we can use the code:
setwd("~/Documents/") # change working directory to directory with .Rmd file
knit(input = "Example.Rmd", output = "Example.md") # .Rmd to .md
mardownToHTML(file = "Example.md", output = "Example.html") #.md to .html
or, in one line:
knit2html(file = "~/Documents/Example.Rmd", output = "~/Documents/Example.html")
rmarkdown
If we have a markdown file with YAML heading specifying the type of output and options, we can also use the rmarkdown
package in R:
render(file = "~/Documents/Example.Rmd")
we can even render multiple types of files from the same document:
render("Example.Rmd", c("pdf_document", "html_document"))
WhateverYouWant
SomewhereOnYourComputer/
analysis-code/analysis-datasharing.Rmd
render-analysis-report.R
What does this code do?
source("render-analysis-report.R")
setwd("analysis-code")
purl("analysis-datasharing.Rmd", output = "mycode.R")
purl("analysis-datasharing.Rmd", output = "mycode-withdoc.R", documentation = 2)
knitr::knit2html("analysis-data-sharing.Rmd", output = "myoutput.html")
rmarkdown::render("analysis-datasharing.Rmd",
output_format = c("html_document", "pdf_document"),
params = list(showcode=TRUE))