R Under development (unstable) (2024-10-14 r87233 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(qwraps2) > > # use the example and check the results > e <- new.env() > example("lazyload_cache_dir", local = e) lzyl__> # this example is based on \url{https://stackoverflow.com/a/41439691/1104685} lzyl__> lzyl__> # create a temp directory for a and place a .Rmd file within lzyl__> tmpdir <- normalizePath(paste0(tempdir(), "/llcache_eg"), mustWork = FALSE) lzyl__> tmprmd <- tempfile(pattern = "report", tmpdir = tmpdir, fileext = "Rmd") lzyl__> dir.create(tmpdir) lzyl__> oldwd <- getwd() lzyl__> setwd(tmpdir) lzyl__> # build and example .Rmd file lzyl__> # note that the variable x is created in the first chunck and then over lzyl__> # written in the second chunk lzyl__> cat("---", lzyl__+ "title: \"A Report\"", lzyl__+ "output: html_document", lzyl__+ "---", lzyl__+ "", lzyl__+ "```{r first-chunk, cache = TRUE}", lzyl__+ "mpg_by_wt_hp <- lm(mpg ~ wt + hp, data = mtcars)", lzyl__+ "x_is_pi <- pi", lzyl__+ "x <- pi", lzyl__+ "```", lzyl__+ "", lzyl__+ "```{r second-chunk, cache = TRUE}", lzyl__+ "mpg_by_wt_hp_am <- lm(mpg ~ wt + hp + am, data = mtcars)", lzyl__+ "x_is_e <- exp(1)", lzyl__+ "x <- exp(1)", lzyl__+ "```", lzyl__+ sep = "\n", lzyl__+ file = tmprmd) lzyl__> # knit the file. evaluate the chuncks in a new environment so we can compare lzyl__> # the objects after loading the cache. lzyl__> kenv <- new.env() lzyl__> knitr::knit(input = tmprmd, envir = kenv) processing file: D:\temp\RtmpieTnsE\llcache_eg\report2816c48f813a4Rmd output file: report2816c48f813a4Rmd.txt [1] "report2816c48f813a4Rmd.txt" lzyl__> # The objects defined in the .Rmd file are now in kenv lzyl__> ls(envir = kenv) [1] "mpg_by_wt_hp" "mpg_by_wt_hp_am" "x" "x_is_e" [5] "x_is_pi" lzyl__> # view the cache lzyl__> list.files(path = tmpdir, recursive = TRUE) [1] "cache/__packages" [2] "cache/first-chunk_4e3140196fb03528521fad11269d001c.RData" [3] "cache/first-chunk_4e3140196fb03528521fad11269d001c.rdb" [4] "cache/first-chunk_4e3140196fb03528521fad11269d001c.rdx" [5] "cache/second-chunk_8e4f33e2310468ad8f108c019d707830.RData" [6] "cache/second-chunk_8e4f33e2310468ad8f108c019d707830.rdb" [7] "cache/second-chunk_8e4f33e2310468ad8f108c019d707830.rdx" [8] "report2816c48f813a4Rmd" [9] "report2816c48f813a4Rmd.txt" lzyl__> # create three more environment, and load only the first chunk into the lzyl__> # first, and the second chunck into the second, and then load all of the lzyl__> # cache into the third lzyl__> env1 <- new.env() lzyl__> env2 <- new.env() lzyl__> env3 <- new.env() lzyl__> lazyload_cache_labels(labels = "first-chunk", lzyl__+ path = paste0(tmpdir, "/cache"), lzyl__+ envir = env1) Lazyloading D:\temp\RtmpieTnsE\llcache_eg/cache/first-chunk_4e3140196fb03528521fad11269d001c lzyl__> lazyload_cache_labels(labels = "second-chunk", lzyl__+ path = paste0(tmpdir, "/cache"), lzyl__+ envir = env2) Lazyloading D:\temp\RtmpieTnsE\llcache_eg/cache/second-chunk_8e4f33e2310468ad8f108c019d707830 lzyl__> lazyload_cache_dir(path = paste0(tmpdir, "/cache"), envir = env3) Lazyloading: D:\temp\RtmpieTnsE\llcache_eg/cache/first-chunk Lazyloading: D:\temp\RtmpieTnsE\llcache_eg/cache/second-chunk lzyl__> # Look at the conents of each of these environments lzyl__> ls(envir = kenv) [1] "mpg_by_wt_hp" "mpg_by_wt_hp_am" "x" "x_is_e" [5] "x_is_pi" lzyl__> ls(envir = env1) [1] "mpg_by_wt_hp" "x" "x_is_pi" lzyl__> ls(envir = env2) [1] "mpg_by_wt_hp_am" "x" "x_is_e" lzyl__> ls(envir = env3) [1] "mpg_by_wt_hp" "mpg_by_wt_hp_am" "x" "x_is_e" [5] "x_is_pi" lzyl__> # The regression models are only fitted once an should be the same in all the lzyl__> # environments where they exist, as should the variables x_is_e and x_is_pi lzyl__> all.equal(kenv$mpg_by_wt_hp, env1$mpg_by_wt_hp) [1] TRUE lzyl__> all.equal(env1$mpg_by_wt_hp, env3$mpg_by_wt_hp) [1] TRUE lzyl__> all.equal(kenv$mpg_by_wt_hp_am, env2$mpg_by_wt_hp_am) [1] TRUE lzyl__> all.equal(env2$mpg_by_wt_hp_am, env3$mpg_by_wt_hp_am) [1] TRUE lzyl__> # The value of x, however, should be different in the differnet lzyl__> # environments. For kenv, env2, and env3 the value should be exp(1) as that lzyl__> # was the last assignment value. In env1 the value should be pi as that is lzyl__> # the only relevent assignment. lzyl__> lzyl__> all.equal(kenv$x, exp(1)) [1] TRUE lzyl__> all.equal(env1$x, pi) [1] TRUE lzyl__> all.equal(env2$x, exp(1)) [1] TRUE lzyl__> all.equal(env3$x, exp(1)) [1] TRUE lzyl__> # cleanup lzyl__> setwd(oldwd) lzyl__> unlink(tmpdir, recursive = TRUE) > > stopifnot(isTRUE(all.equal(e$kenv$mpg_by_wt_hp, e$env1$mpg_by_wt_hp))) > stopifnot(isTRUE(all.equal(e$env1$mpg_by_wt_hp, e$env3$mpg_by_wt_hp))) > > stopifnot(isTRUE(all.equal(e$kenv$mpg_by_wt_hp_am, e$env2$mpg_by_wt_hp_am))) > stopifnot(isTRUE(all.equal(e$env2$mpg_by_wt_hp_am, e$env3$mpg_by_wt_hp_am))) > > stopifnot(isTRUE(all.equal(e$kenv$x, exp(1)))) > stopifnot(isTRUE(all.equal(e$env1$x, pi))) > stopifnot(isTRUE(all.equal(e$env2$x, exp(1)))) > stopifnot(isTRUE(all.equal(e$env3$x, exp(1)))) > > ################################################################################ > ## Extended testing ## > > > ################################################################################ > # End of File # > ################################################################################ > > proc.time() user system elapsed 0.37 0.15 0.51