2.5 makefiles
I like to use makefiles fairly extensively and there is a ton of information available [online](https://www.gnu.org/software/make/manual/html_node/Introduction.html.
GNU make has been around for a long time and is used a dependency matching tool.
By this in your makefile, you define the dependencies of each file.
For example my_script.R
generates my_analysis.csv
and then my_analysis.csv
is used in my_pdf.Rmd
that generated the report.pdf
.
If you define these hierarchies in your makefile you can just type “make” into the console and the makefile will only run those scripts that are necessary based on the time stamps.
If my_csv.csv
were older than the code in my_script.R
then entire dependency series would be run.
This is extremely useful when you get new data and have to re-run your analysis, or you have long running code and you only need to update that which changed.
Again, I like to use makfile to automate these processes, but you certainly don’t have to adopt my approach. In the next section I give examples of the makefiles that I often use.
2.5.1 projects
This is my standard makefile for projects.
# Usually, only these lines need changing
RDIR= .
MUNGEDIR= ./munge
SRCDIR= ./src
REPORTDIR= ./report
# List files for dependencies
MUNGE_RFILES := $(wildcard $(MUNGEDIR)/*.R)
SRC_RFILES := $(wildcard $(SRCDIR)/*.R)
REPORT_RFILES := $(wildcard $(SRCDIR)/*.Rmd)
# Indicator files to show R file has run
MUNGE_OUT_FILES:= $(MUNGE_RFILES:.R=.Rout)
SRC_OUT_FILES:= $(SRC_RFILES:.R=.Rout)
REPORT_OUT_FILES:= $(REPORT_RFILES:.Rmd=.pdf)
# Run everything
all: $(MUNGE_OUT_FILES) $(SRC_OUT_FILES) $(REPORT_OUT_FILES)
# Run Munging Operation
$(MUNGEDIR)/%.Rout: $(MUNGEDIR)/%.R
@echo starting muninging
R CMD BATCH --vanilla $<
@echo finished munging
# Run Analysis Operation
$(SRCDIR)/%.Rout: $(SRCDIR)/%.R
@echo starting analysis
R CMD BATCH --vanilla $<
# Compile Report
$(REPORTDIR)/%.pdf: $(REPORTDIR)/%.Rmd
@echo compiling report
-Rscript -e 'library(rmarkdown); render(here::here("report", "report.Rmd"))'
bash ./report/sort_log.sh
bash ./report/move_draft.sh
# Clean up
clean:
rm -fv $(MUNGE_OUT_FILES)
rm -fv $(SRC_OUT_FILES)
rm -fv $(REPORT_OUT_FILES)
.PHONY: all clean
2.5.2 packages
The package makefile is mainly used for building the package and generating the pkgdown website.
2.5.3 websites
This makefile template is useful when building generic websites with R Markdown.
# Usually, only these lines need changing
RDIR= .
DOCS= ./docs
# List files for dependencies
DOCS_RFILES := $(wildcard $(DOCS)/*.html)
# Indicator files to show R file has run
DOCS_OUT_FILES:= $(DOCS_RFILES:.Rmd=.html)
# Run everything
all: $(DOCS_OUT_FILES) preview
# Compile Report
$(DOCS)/%.html: $(RDIR)/%.Rmd
@echo compiling report
-Rscript -e 'rmarkdown::render_site("$<")'
preview:
open docs/index.html
# Once you are satisfied with the preview type "make update" in the terminal
update:
Rscript -e "irtools::move_ir_group_site()"