There are some tasks I do a lot and it'd be good to have general .Rmd documents available to report on the outcome of those tasks. Some examples would be a standard report on an ML experiment, or an overview from a data curation pipeline. I've been using rmarkdown for generating reports, but it has some limitations for this. I don't want to have a new Rmd file for every report. I'd like to have a configurable template file that lives in a shared location that I can run each time.
Unfortunately, if you try to do this you'll soon find that rmarkdown expects the Rmd file to be in your working directory - well, the working directory is set to the location of the Rmd file. See the following line in render.R
: github-render.R_L96
My current work around for this (and please let me know if you have a better way) is to have a helper script which copies over the Rmd file to the current working directory, generates the report and then removes the temporary files:
#!/bin/bash -eu | |
if [[ $# -ne 4 ]]; then | |
echo "Usage: $0 <Rmd file> <config file> <output file> <doc type {html, pdf}>" | |
exit 1 | |
fi | |
RMD=$1 | |
CONFIG=$2 | |
OUTPUT_FILE=$3 | |
DOC_TYPE=$4 | |
RMD_BASE=$(basename $RMD .Rmd) | |
TMP_RMD=${RMD_BASE}.$RANDOM.Rmd | |
echo $RMD $TMP_RMD $DOC_TYPE $OUTPUT_FILE | |
cp $RMD $TMP_RMD | |
cp $CONFIG model_report.yaml | |
R --vanilla --slave -e "library('rmarkdown'); render('$TMP_RMD', '${DOC_TYPE}_document', output_file='$OUTPUT_FILE')" | |
rm $TMP_RMD | |
rm model_report.yaml |
library(yaml)
config <- yaml.load_file("model_report.yaml")
The YAML configuration file contains the project specific variables (e.g. output suffix, flags for adding/subtracting items from a report, important dir locations). This file could easily be generated on the fly via arguments to the bash script.
No comments:
Post a Comment