Last active
December 7, 2022 13:15
-
-
Save brshallo/e963b9dca5e4e1ab12ec6348b135362e to your computer and use it in GitHub Desktop.
Function for sourcing individual or multiple chunks from an RMD document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
library(stringr) | |
library(readr) | |
library(purrr) | |
library(glue) | |
library(knitr) | |
source_rmd_chunks <- function(file, chunk_labels, skip_plots = TRUE, output_temp = FALSE){ | |
temp <- tempfile(fileext=".R") | |
knitr::purl(file, output = temp) | |
text <- readr::read_file(temp) | |
text <- purrr::map(chunk_labels, ~stringr::str_extract(text, glue::glue("(## ----{var})(.|[:space:])*?(?=(## ----)|$)", var = .x))) %>% | |
stringr::str_c(collapse = "\n") | |
readr::write_file(text, temp) | |
if(skip_plots) { | |
old_dev = getOption('device') | |
options(device = function(...) { | |
.Call("R_GD_nullDevice", PACKAGE = "grDevices") | |
}) | |
} | |
source(temp) | |
if(skip_plots) { | |
options(device = old_dev) | |
} | |
if(output_temp) temp | |
} |
@py9mrg I wasn't really planning on it. My impression is Yihui isn't crazy about knitr::purl()
so didn't know if he'd want to incorporate support for something like this...
Wouldn't hurt to open an issue though, feel free to @ me on jt.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @brshallo
This is really helpful for what I want to do, thanks! One quick question/comment, have you considered making a PR to knitr for this? I think it would be really helpful to have this as part of the standard package.
Edit: oh also I noticed a potential improvement in the regex. As you currently have:
It can get confused with chunk labels that start the same. For example, if I have labels that are
tuning
andtuning-grid
and I put in the chunk label as just "tuning" then, in my case, it will only find thetuning-grid
chunk (presumably because this one comes first) and never return thetuning
chunk (unless I hack it by putting something like "tuning----" as the chunk label to find). But this can be mitigated by changing the regex to:(i.e. changing
----{var}
to----{var}----
)Edit 2 - balls, that idea doesn't work if there are any chunk options... so now it has to be something like:
But now we're back at
tuning-grid
nottuning
being found! I'm not great with regex so I'll keep working on it.Ok, I think this works, at least it does for me. From my limited regex understanding this should exclude any chunks where
var
is followed by a single hyphen or single underscore and then any letter or digit. So searching for a chunk calledtuning
won't accidentally pick uptuning-grid
ortuning-2
ortuning_grid
, you'd have to actually specify those full chunk names. But I'm not sure how robust it is - as long as no one ever names a chunk something liketuning--grid
!I don't think I need all the options in
(----|,|.|[:space:])
but I'm trying to explicitly cover all the cases that might occur. For me it works without all of these anyway.