Skip to content

Instantly share code, notes, and snippets.

@edonnachie
Created July 25, 2018 12:12
Show Gist options
  • Save edonnachie/5d7e45a9a4836cd91aa0f5e290b44156 to your computer and use it in GitHub Desktop.
Save edonnachie/5d7e45a9a4836cd91aa0f5e290b44156 to your computer and use it in GitHub Desktop.
Pattern rules for using GNU make to build R projects on Windows
################################################################################
## Automatic rules ----
################################################################################
# First problem on windows: where to find Rscript if not on path
# RSCRIPT = "C:/Program Files/R/R-3.4.2/bin/x64/Rscript" --verbose
RSCRIPT = "Rscript" --verbose
# R files with Rout as target
# (including in subdirectories of ./R)
# Note: Input file needs to be sourced because input is UTF-8:
# http://r.789695.n4.nabble.com/R-CMD-BATCH-Unicode-td4670311.html
# https://stackoverflow.com/questions/1035388/unicode-output-on-windows-command-line
R/%.Rout:
$(RSCRIPT) -e 'source("$<", encoding = "UTF-8", echo = TRUE)' > $@
# R data files with data file as target
# (including in subdirectories of ./data)
data/%.rds:
$(RSCRIPT) -e 'source("$<", encoding = "UTF-8", echo = TRUE)' > $<.Rout
# Reports using Rmarkdown
# (including in subdirectories of ./rerpots)
# Need to ensure that target file extension corresponds to output type in file
reports/%.html: reports/%.Rmd
$(RSCRIPT) -e 'rmarkdown::render("$<", encoding = "UTF-8", output_options = list(cache.rebuild = TRUE))' > $<.Rout
reports/%.pdf: reports/%.Rmd
$(RSCRIPT) -e 'rmarkdown::render("$<", encoding = "UTF-8", output_options = list(cache.rebuild = TRUE))' > $<.Rout
# Phony target to find and make all markdown (Rmd) reports
RMD = $(wildcard reports/*.Rmd)
REPORTS = $(addsuffix .html, $(basename $(RMD)))
.PHONY : reports
reports : $(REPORTS) data
# A basic clean routine
.PHONY : clean
clean :
rm -f R/*.Rout reports/*.Rout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment