Skip to content

Instantly share code, notes, and snippets.

@muschellij2
Created October 12, 2017 05:36
Show Gist options
  • Save muschellij2/3028cd956404597a78550e1c1d9e072c to your computer and use it in GitHub Desktop.
Save muschellij2/3028cd956404597a78550e1c1d9e072c to your computer and use it in GitHub Desktop.
Get All Examples from an R Packag
all_examples = function(path = ".", commentDontrun = FALSE) {
# list all the Rd files
files = list.files(
path = file.path(path, "man"),
pattern = "Rd$",
recursive = FALSE,
full.names = TRUE)
tfiles = sapply(files, function(x) {
tfile = tempfile(fileext = ".R")
# turn to examples
tools::Rd2ex(x, out = tfile, commentDontrun = commentDontrun)
if (!file.exists(tfile)) {
tfile = NA
}
return(tfile)
})
# if something went wrong - delete that
tfiles = tfiles[!is.na(tfiles)]
# read them all in
res = lapply(tfiles, readLines)
# you can unlist(res) and writeLines for a single Ex.R file
return(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment