Last active
September 9, 2020 23:41
-
-
Save nischalshrestha/83e826607a569201c8349b115a35c971 to your computer and use it in GitHub Desktop.
Raw knitr chunks (inside hook context)
This file contains hidden or 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
# test file | |
sample_chunk <- " | |
```{r hello, testing=as.logical(1), another=1 + 1} | |
1 + 1 | |
```" | |
file = "sample.Rmd" | |
writeLines(sample_chunk, con = file) | |
foo_hook <- function(before, options, envir) { | |
# note: this returns raw knitr chunks in list format | |
# has attribute of "chunk_opts" | |
print(knitr::knit_code$get(options$label)) | |
} | |
knitr::knit_hooks$set(testing = foo_hook) | |
outfile <- knitr::knit(file) | |
# delete temp md file | |
on.exit(unlink(outfile)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment