purl()
is a
wrapper to knit(..., tangle = TRUE)
).knit(input, output = NULL, tangle = FALSE, text = NULL, quiet = FALSE,
envir = parent.frame(), encoding = getOption("encoding"))purl(..., documentation = 1L)
knit()
; if NULL
, this
function will try to guess and it will be under the current working
directoryStangle
)parent.frame()
, new.env()
, or
globale
file
knit()
from purl()
0
means pure code (discard all text chunks);
1
(default) means add the chunk headers to code; 2
means add
all text chunks to code as roxygen text
argument is not
NULL
, the compiled output is returned as a character vector. In
other words, if you provide a file input, you get an output filename; if
you provide a character vector input, you get a character vector output.knit('my_input.Rnw')
is usually enough. This function will try to
determine many internal settings automatically. For the sake of
reproducibility, it is 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:
ext
is not txt
, otherwise the output is tangle = TRUE
,
We need a set of syntax to identify special markups for R code chunks and R
options, etc. The syntax is defined in a pattern list. All built-in pattern
lists can be found in all_patterns
(call it apat
). First
apat$rnw
, tex uses the list apat$tex
, brew uses
apat$brew
and HTML files use apat$html
; for unkown extensions,
the content of the input document is matched against all pattern lists to
automatically determine which pattern list is being used. You can also manually
set the pattern list using the knit_patterns
object or the
pat_rnw
series functions in advance and
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).
The name knit
comes from its counterpart weave (as in Sweave),
and the name purl
(as tangle in Stangle) comes from a knitting
method `knit one, purl one'.
If the input document has child documents, they will also be compiled
recursively. See knit_child
.
See the package website and manuals in the references to know more about
See citation('knitr')
for the citation information.
library(knitr)
(f = system.file("examples", "knitr-minimal.Rnw", package = "knitr"))
knit(f) # compile to tex
purl(f) # tangle R code
purl(f, documentation = 0) # extract R code only
purl(f, documentation = 2) # also include documentation
Run the code above in your browser using DataLab