-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmake-exercises.R
30 lines (24 loc) · 1.16 KB
/
make-exercises.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
files <- list.files(here::here(), pattern = "*.Rmd$|*.R$|*.cpp$",
recursive = TRUE)
ignore <- c("install\\.R$", "^data-raw", "^exercises\\/", "^make-exercises\\.R$")
for (i in seq_along(ignore)) files <- files[!grepl(ignore[i], files)]
folders <- c("data", "data-raw", "functions", "parallel", "performance", "rcpp")
if (file.exists("exercises"))
stop("Stopping unless you manually delete the 'exercises' folder.")
dir.create("exercises", showWarnings = FALSE)
purrr::walk(folders, function(x)
dir.create(file.path("exercises", x), showWarnings = FALSE))
file.copy("data", file.path("exercises"), recursive = TRUE)
remove_exercises <- function(x) {
f <- readLines(x)
f_ex <- ifelse(grepl("# exercise", f), "# exercise", f)
f_ex <- ifelse(grepl("<!-- exercise -->", f_ex), "<!-- exercise -->", f_ex)
f_ex <- ifelse(grepl("// exercise", f_ex), "// exercise", f_ex)
f_ex <- purrr::map_chr(f_ex, ~
gsub('here::here\\(\"', 'here::here\\(\"exercises/', .x))
writeLines(as.character(f_ex), con = here::here("exercises", x))
}
purrr::walk(files, remove_exercises)
## knit all exercises (slow)
# f <- files[grepl("\\.Rmd$", files)]
# purrr::walk(f, rmarkdown::render)