Description Usage Arguments Details Value Author(s) See Also Examples
View source: R/extractCached.R
Extract specific R objects from the knitr cache of a previously compiled Rmarkdown file (the “donor”) so that it can be used in the compilation process of another Rmarkdown file (the “acceptor”).
1 | extractCached(path, chunk, objects, envir = topenv(parent.frame()))
|
path |
String containing the path to the donor Rmarkdown file. |
chunk |
String containing the name of the requested chunk. |
objects |
Character vector containing variable names for one or more objects to be extracted. |
envir |
Environment where the loaded objects should be stored. |
Each R object is extracted in its state at the requested chunk
and inserted into envir
.
Note that the object does not have to be generated or even referenced in chunk
,
provided it was generated in a previous chunk.
The parser in this function is rather limited, so the donor Rmarkdown file is subject to several constraints:
All chunks involved in generating the requested objects (indirectly or otherwise) should be named.
All named chunks should be executed; eval=FALSE
is not respected.
All relevant code occurs within triple backticks, i.e., any inline code should be read-only.
Unnamed chunks are allowed but cannot be referenced and will not be shown in the output of this function.
This should not be used for code that might affect variables in the named chunks,
i.e., code in unnamed chunks should be “read-only” with respect to variables in the named chunks.
Chunks with names starting with unref-
are considered to be the same as unnamed chunks and will be ignored;
this is useful for figure-generating chunks that need to be referenced inside the donor report.
Obviously, this entire process assumes that donor report has already been compiled with cache=TRUE
.
If not, extractCached
will compile it (and thus generate the cache) using compileChapter
.
Variables with names objects
are created in envir
.
A markdown chunk (wrapped in a collapsible element) is printed that contains all commands needed to generate those objects,
based on the code in the named chunks of the donor Rmarkdown file.
Aaron Lun
setupHTML
and chapterPreamble
, to set up the code for the collapsible element.
compileChapter
, to compile a Rmarkdown report to generate the cache.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | # Mocking up an Rmarkdown report.
donor <- tempfile(fileext=".Rmd")
write(file=donor, "```{r some-monsters}
destoroyah <- 1
mecha.king.ghidorah <- 2
```
```{r more-monsters}
space.godzilla <- 3
```
```{r}
msg <- 'I am not referenced.'
```
```{r unref-figure}
plot(1, 1, main='I am also not referenced.')
```
```{r even-more-monsters}
megalon <- 4
```")
# Extracting stuff from it in another report.
acceptor <- tempfile(fileext=".Rmd")
dpb <- deparse(basename(donor))
write(file=acceptor, sprintf("```{r, echo=FALSE, results='asis'}
chapterPreamble()
```
```{r, results='asis', echo=FALSE}
extractCached(%s, chunk='more-monsters',
objects=c('space.godzilla', 'destoroyah'))
```
```{r}
space.godzilla * destoroyah
```
```{r, results='asis', echo=FALSE}
extractCached(%s, chunk='even-more-monsters',
objects=c('megalon', 'mecha.king.ghidorah'))
```
```{r}
mecha.king.ghidorah * megalon
```", dpb, dpb))
rmarkdown::render(acceptor)
if (interactive()) browseURL(sub(".Rmd$", ".html", acceptor))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.