Skip to content

Instantly share code, notes, and snippets.

@muschellij2
Created July 17, 2018 22:42
Show Gist options
  • Save muschellij2/edd6b78b4d5eb13a457852288d616bec to your computer and use it in GitHub Desktop.
Save muschellij2/edd6b78b4d5eb13a457852288d616bec to your computer and use it in GitHub Desktop.
Coverage for each example file
library(dplyr)
library(covr)
library(tidyr)
library(desc)
library(purrr)
library(devtools)
# get package name
desc = desc::desc(file = "DESCRIPTION")
package_name = desc$get("Package")
# get all the man files
mans = list.files(pattern = ".Rd", path = "man", full.names = TRUE)
get_source = function(man_file) {
x = readLines(man_file)
string = "% Please edit documentation in "
x = x[grepl(string, x, fixed = TRUE)]
x = sub(string, "", x)
x = trimws(x)
return(x)
}
# get the sources
rds = sapply(mans, get_source)
df = data_frame(man = names(rds), r_file = rds)
df = df %>%
arrange(r_file, man)
# run Rd to example
example_code = function(path, test = TRUE, run = TRUE) {
tmp <- tempfile(fileext = ".R")
tools::Rd2ex(path, out = tmp, commentDontrun = !run,
commentDonttest = !test)
res = ""
if (file.exists(tmp)) {
res = readLines(tmp)
}
res = data_frame(code = res)
return(res)
}
# get the code
res = lapply(df$man, example_code)
names(res) = df$man
code_df = bind_rows(res, .id = "man")
code_df = code_df %>%
mutate(index = seq(n())) %>%
group_by(man)
df = left_join(code_df, df)
make_r_file = function(code) {
tfile = tempfile(fileext = ".R")
writeLines(code, sep = "\n", con = tfile)
return(tfile)
}
# collapse it all together
run_df = df %>%
group_by(r_file) %>%
summarize(test_code = make_r_file(code))
devtools::load_all(".")
res = purrr::map2(run_df$r_file, run_df$test_code, file_coverage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment