| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/render.R |
| 3 |
\name{compile_notebook} |
| 4 |
\alias{compile_notebook} |
| 5 |
\title{Compiling R scripts to a notebook} |
| 6 |
\description{ |
| 7 |
R Markdown can also compile R scripts to a notebook which includes |
| 8 |
commentary, source code, and script output. Notebooks can be compiled to any |
| 9 |
output format including HTML, PDF, and MS Word. |
| 10 |
} |
| 11 |
\section{Overview}{ |
| 12 |
|
| 13 |
To compile a notebook from an R script you simply pass the script to |
| 14 |
\code{\link{render}}. For example: |
| 15 |
\preformatted{ |
| 16 |
rmarkdown::render("analysis.R") |
| 17 |
rmarkdown::render("analysis.R", "pdf_document") |
| 18 |
} |
| 19 |
The first call to \code{\link{render}} creates an HTML document, whereas |
| 20 |
the second creates a PDF document. |
| 21 |
|
| 22 |
By default the name of the script, username, and current date and time are |
| 23 |
included in the header of the generated notebook. You can override this |
| 24 |
default behavior by including explicit metadata in a specially formatted R |
| 25 |
comment: |
| 26 |
\preformatted{ |
| 27 |
#' --- |
| 28 |
#' title: "Crop Analysis Q3 2013" |
| 29 |
#' author: "John Smith" |
| 30 |
#' date: "May 3rd, 2014" |
| 31 |
#' --- |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
\section{Including Markdown}{ |
| 36 |
|
| 37 |
Note that the R comment used above to add a title, author, and date |
| 38 |
includes a single-quote as a special prefix character. This is a |
| 39 |
\pkg{roxygen2} style comment, and it's actually possible to include many |
| 40 |
such comments in an R script, all of which will be converted to markdown |
| 41 |
content within the generated notebook. For example: |
| 42 |
\preformatted{#' A script comment that includes **markdown** formatting.} |
| 43 |
Rather than displaying as an R comment in the compiled notebook any |
| 44 |
\pkg{roxygen2} style comment will be treated as markdown and rendered |
| 45 |
accordingly. |
| 46 |
} |
| 47 |
|
| 48 |
\section{knitr Spin}{ |
| 49 |
|
| 50 |
Including markdown within R comments is possible because \code{\link{render}} |
| 51 |
calls the \code{\link[knitr:spin]{knitr spin}} function to convert the R |
| 52 |
script to an Rmd file. The \code{spin} function also enables you to add |
| 53 |
knitr |
| 54 |
chunk options with another special comment prefix (\code{#+}). |
| 55 |
|
| 56 |
Here's an example of a script that uses the various features of \code{spin}: |
| 57 |
|
| 58 |
\url{https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R} |
| 59 |
|
| 60 |
For more details on \code{knitr::spin} see the following documentation: |
| 61 |
|
| 62 |
\url{https://yihui.org/knitr/demo/stitch/} |
| 63 |
} |
| 64 |
|