Created
October 12, 2017 05:36
-
-
Save muschellij2/3028cd956404597a78550e1c1d9e072c to your computer and use it in GitHub Desktop.
Get All Examples from an R Packag
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
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