How to read knitr/Rmd cache in interactive session?

后端 未结 3 1283
无人及你
无人及你 2020-12-15 20:56

I have an Rmd file with a lot of cached code chunks.

Now I want to keep developing that script using an interactive session to play around and test diff

相关标签:
3条回答
  • 2020-12-15 21:35

    I think that running library("knitr"); knit("foo.Rmd") in the console/R session is the easiest way to do this, although it will rewrite foo.md, figures, etc.. (Too busy/lazy to test it at the moment.)

    You could probably poke around in the cache directory and read the cached files directly, but that would be a lot more work/trickier.

    0 讨论(0)
  • 2020-12-15 21:47

    Internally, knitr uses lazyLoad to load cached results, and so can you:

    lazyLoad('knitr_cache_dir/chunk_2c7d6b477306be1d4d4ed451f2f1b52a')
    

    Make sure to supply the filename without the suffix.

    0 讨论(0)
  • 2020-12-15 21:51

    I've created functions load the objects from cached chunks into an interactive R session. The functions are lazyload_cache_dir and lazyload_cache_labels and are available in qwraps2 version > 0.2.4

    A detailed example of the use of these functions is here:

    Quick overview:

    Say you have the file report.Rmd

    ---
    title:  "A Report"
    output: html_document
    ---
    
    ```{r first-chunk, cache = TRUE}
    fit <- lm(mpg ~ wt + hp, data = mtcars)
    x <- pi
    ```
    
    ```{r second-chunk, cache = TRUE}
    fit <- lm(mpg ~ wt + hp + am, data = mtcars)
    xx <- exp(1)
    ```
    

    After knitting you end up with a this project directory

    .
    ├── report_cache
    │   └── html
    │       ├── first-chunk_bf368425c25f0c3d95cac85aff007ad1.RData
    │       ├── first-chunk_bf368425c25f0c3d95cac85aff007ad1.rdb
    │       ├── first-chunk_bf368425c25f0c3d95cac85aff007ad1.rdx
    │       ├── __packages
    │       ├── second-chunk_2c7d6b477306be1d4d4ed451f2f1b52a.RData
    │       ├── second-chunk_2c7d6b477306be1d4d4ed451f2f1b52a.rdb
    │       └── second-chunk_2c7d6b477306be1d4d4ed451f2f1b52a.rdx
    ├── report.html
    └── report.Rmd
    

    and you want to load the objects from first-chunk.

    lazyload_cache_labels("first-chunk", path = "report_cache/html")
    ## Lazyloading report_cache/html/first-chunk_bf368425c25f0c3d95cac85aff007ad1
    ls()
    ## [1] "fit" "x"
    

    See the blog post for details on loading only a whole directory of cached objects or loading specific objects from within a cached chunk.

    0 讨论(0)
提交回复
热议问题