Skip to content

Instantly share code, notes, and snippets.

@halpo
Created November 29, 2011 18:55
Show Gist options
  • Save halpo/1405945 to your computer and use it in GitHub Desktop.
Save halpo/1405945 to your computer and use it in GitHub Desktop.
knitr makefile
################################################################################
# Copyright 2011
# Andrew Redd
# 11/23/2011
#
# Description of File:
# Makefile for knitr compiling
#
################################################################################
all:pdf # default rule DO NOT EDIT
################################################################################
MAINFILE := test
RNWFILES :=
RFILES :=
TEXFILES :=
CACHEDIR := cache
FIGUREDIR := figures
LATEXMK_FLAGS :=
##### Explicit Dependencies #####
################################################################################
RNWTEX = $(RNWFILES:.Rnw=.tex)
ROUTFILES = $(RFILES:.R=.Rout)
RDATAFILES= $(RFILES:.R=.Rdata)
MAINTEX = $(MAINFILE:=.tex)
MAINPDF = $(MAINFILE:=.pdf)
ALLTEX = $(MAINTEX) $(RNWTEX) $(TEXFILES)
# Dependencies
$(RNWTEX): $(RDATAFILES)
$(MAINTEX): $(RNWTEX) $(TEXFILES)
$(MAINPDF): $(MAINTEX) $(ALLTEX)
.PHONY:pdf tex clean clearcache cleanall
pdf: $(MAINPDF)
tex: $(RDATAFILES) $(ALLTEX)
$(CACHEDIR):
mkdir $(CACHEDIR)
$(FIGUREDIR):
mkdir $(FIGUREDIR)
%.tex:%.Rnw
Rscript \
-e "library(knitr)" \
-e "knitr::opts_chunk[['set']](fig.path='$(FIGUREDIR)/$*-')" \
-e "knitr::opts_chunk[['set']](cache.path='$(CACHEDIR)/$*-')" \
-e "knitr::knit('$<','$@')"
%.R:%.Rnw
Rscript -e "Sweave('$^', driver=Rtangle())"
%.Rout:%.R
R CMD BATCH "$^" "$@"
%.pdf: %.tex
latexmk -pdf $<
clean:
-latexmk -c -quiet $(MAINFILE).tex
-rm -f $(MAINTEX) $(RNWTEX)
-rm -rf $(FIGUREDIR)
-rm *tikzDictionary
-rm $(MAINPDF)
clearcache:
-rm -rf cache
cleanall: clean clearcache
@yihui
Copy link

yihui commented Jan 20, 2012

prefix.string and prefix.cache have been renamed to fig.path and cache.path respectively, so you might want to tweak line 28 and 29

@halpo
Copy link
Author

halpo commented Jan 20, 2012

I replaced it in my personal copy but forgot here. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment