Skip to content

Instantly share code, notes, and snippets.

@nischalshrestha
Last active September 9, 2020 23:41
Show Gist options
  • Save nischalshrestha/83e826607a569201c8349b115a35c971 to your computer and use it in GitHub Desktop.
Save nischalshrestha/83e826607a569201c8349b115a35c971 to your computer and use it in GitHub Desktop.
Raw knitr chunks (inside hook context)
# 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