Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (38 sloc) 1.82 KB
# https://github.com/dgrtwo/dgrtwo.github.com/blob/master/_scripts/knitpages.R
# 2019-12-04
#!/usr/bin/Rscript --vanilla
# compiles all .Rmd files in _R directory into .md files in Pages directory,
# if the input file is older than the output file.
# run ./knitpages.R to update all knitr files that need to be updated.
KnitPost <- function(input, outfile, figsfolder, cachefolder, base.url="/") {
# this function is a modified version of an example here:
# http://jfisher-usgs.github.com/r/2012/07/03/knitr-jekyll/
require(knitr);
opts_knit$set(base.url = base.url)
fig.path <- paste0(figsfolder, sub(".Rmd$", "", basename(input)), "/")
cache.path <- file.path(cachefolder, sub(".Rmd$", "", basename(input)), "/")
opts_chunk$set(fig.path = fig.path)
opts_chunk$set(cache.path = cache.path)
opts_chunk$set(fig.cap = "center")
render_jekyll()
knit(input, outfile, envir = parent.frame())
}
knit_folder <- function(infolder, outfolder, figsfolder, cachefolder, since) {
for (infile in list.files(infolder, pattern = "*.Rmd", full.names = TRUE)) {
pattern = "\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d\\-"
# folder = ifelse(grepl(pattern, infile), outfolder, "pages")
outfile = paste0(outfolder, "/", sub(".Rmd$", ".md", basename(infile)))
print(outfile)
# knit only if the input file is the last one modified
if ((!file.exists(outfile) | file.info(infile)$mtime > file.info(outfile)$mtime) & infile > paste0(infolder, "/", since)) {
print(infile)
KnitPost(infile, outfile, figsfolder, cachefolder)
}
# delete html file if exists (generated by RStudio)
htmlfile <- sub("\\.Rmd$", ".html", infile)
if (file.exists(htmlfile)){
file.remove(htmlfile)
}
}
}
knit_folder("_Rmd", "_posts", "img/blog/", "_caches/", "2019-12-04")
You can’t perform that action at this time.