Skip to content

Instantly share code, notes, and snippets.

@topepo
Created April 13, 2022 21:55
Show Gist options
  • Save topepo/d2c33745c17c345b398e375f17ae5158 to your computer and use it in GitHub Desktop.
Save topepo/d2c33745c17c345b398e375f17ae5158 to your computer and use it in GitHub Desktop.
extract_code for purl'ing
extract_code <- function(file) {
# TODO we could put in an argument for the pattern
# or something more sophisticated...
target <- tempfile()
knitr::purl(file, output = target, quiet = TRUE)
code <- readLines(target)
code <- gsub("^##.*", "", code)
r_file <- gsub("[Rq]md$", "R", file)
writeLines(code, r_file)
invisible(TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment