Chunks can be put in an external script, and this function reads chunks into
the current knitr session;
read_demo() is a convenience function
to read a demo script from a package.
1 2 3 4 5 6 7 8 9 10 11 12
Path to the R script.
Character vector of lines of code. By default, this is read from
Character vector of chunk labels (default
Numeric vector specifying the starting/ending line numbers of code chunks, or a character vector; see Details.
Offsets to be added to
Logical dictating whether to keep trailing roxygen-style comments from code chunks in addition to whitespace
Name of the demo and the package. See
Arguments passed to
There are two approaches to read external code into the current session: (1)
Use a special separator of the from
## ---- chunk-label (at least four
dashes before the chunk label) in the script; (2) Manually specify the
labels, starting and ending positions of code chunks in the script.
The second approach will be used only when
labels is not
For this approach, if
NULL, the starting position is 1;
NULL, each of its element takes the next element of
from minus 1, and the last element of
to will be the length of
lines (e.g. when
from = c(1, 3, 8) and the script has 10 lines
to will be
c(2, 7, 10)). Alternatively,
to can be character vectors as regular expressions to specify the
positions; when their length is 1, the single regular expression will be
matched against the
lines vector, otherwise each element of
to is matched against
lines and the match is
supposed to be unique so that the numeric positions returned from
grep() will be of the same length of
labels always has to match the length of
As a side effect, code chunks are read into the current session so that future chunks can (re)use the code by chunk label references. If an external chunk has the same label as a chunk in the current session, chunk label references by future chunks will refer to the external chunk.
This function can only be used in a chunk which is not cached
cache = FALSE), and the code is read and stored in the
current session without being executed (to actually run the code,
you have to use a chunk with a corresponding label).
Yihui Xie; the idea of the second approach came from Peter Ruckdeschel (author of the SweaveListingUtils package)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
## put this in foo.R and read_chunk('foo.R') ## ---- my-label ---- 1 + 1 lm(y ~ x, data = data.frame(x = 1:10, y = rnorm(10))) ## later you can use <<my-label>>= to reference this chunk ## the 2nd approach code = c("#@a", "1+1", "#@b", "#@a", "rnorm(10)", "#@b") read_chunk(lines = code, labels = "foo") # put all code into one chunk named foo read_chunk(lines = code, labels = "foo", from = 2, to = 2) # line 2 into chunk foo read_chunk(lines = code, labels = c("foo", "bar"), from = c(1, 4), to = c(3, 6)) # automatically figure out 'to' read_chunk(lines = code, labels = c("foo", "bar"), from = c(1, 4)) read_chunk(lines = code, labels = c("foo", "bar"), from = "^#@a", to = "^#@b") read_chunk(lines = code, labels = c("foo", "bar"), from = "^#@a", to = "^#@b", from.offset = 1, to.offset = -1) ## later you can use, e.g., <<foo>>= knitr:::knit_code$get() # use this to check chunks in the current session knitr:::knit_code$restore() # clean up the session
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.