knitr (version 0.3)

knit: Knit a document

Description

This function takes an input file, extracts the R code in it according to a list of patterns, evaluates the code and writes the output in another file. It can also tangle R source code from the input document (purl() is a wrapper to knit(..., tangle = TRUE)).

Usage

knit(input, output = NULL, tangle = FALSE, text = NULL)

purl(...)

Arguments

input
path of the input file
output
path of the output file; if NULL, this function will try to guess and it will be under the current working directory
tangle
whether to tangle the R code from the input file (like Stangle)
text
a character vector as an alternative way to provide the input file (text is written into a temporary file as input); this argument is mainly for test purposes only
...
arguments passed to knit

Value

  • The compiled document is written into the output file, and the path of the output file is returned.

Details

For most of the time, it is not necessary to set any options outside the input document; in other words, a single call like knit('my_input.Rnw') is usually enough. This function will try to determine many internal settings automatically. For the sake of reproducibility, it is a better practice to include the options inside the input document (to be self-contained), instead of setting them before knitting the document.

First the filename of the output document is determined in this way: foo.Rnw generates foo.tex, and for other types of files, the file extension is reserved; if the filename contains _knit_, this part will be removed in the output file, e.g., foo_knit_.html creates the output foo.html, so you can use files named in this way as templates; if _knit_ is not found in the filename, foo.ext will produce foo-out.ext. If tangle = TRUE, foo.ext generates an R script foo.R.

Based on the file extension of the input document, a list of patterns will be used to extract R code in the document. All built-in pattern lists can be found in opts_knit$get('all.patterns') (call it apat). Rnw files use the list apat$rnw, tex uses the list apat$tex, brew uses apat$brew and HTML-like files use apat$html (e.g. html and md files). You can manually set the pattern list using the knit_patterns object, and knitr will respect the setting.

According to the output format (opts_knit$get('out.format')), a set of output hooks will be set to mark up results from R (see render_latex). The output format can be LaTeX, Sweave and HTML, etc. The output hooks decide how to mark up the results (you can customize the hooks).

See the package website and manuals in the references to know more about knitr, including the full documentation of chunk options and demos, etc.

References

Package homepage: http://yihui.name/knitr/

The knitr main manual: https://github.com/downloads/yihui/knitr/knitr-manual.pdf

The knitr graphics manual: https://github.com/downloads/yihui/knitr/knitr-graphics.pdf

Examples

Run this code
library(knitr)
(f = tempfile(fileext = ".Rnw"))
file.copy(system.file("examples", "knitr-minimal.Rnw", 
    package = "knitr"), f, overwrite = TRUE)
knit(f)
## or setwd(dirname(f)); knit(basename(f))

purl(f)  # extract R code only

Run the code above in your browser using DataCamp Workspace