7

我一直在使用 makefile 为我在 R 中的分析报告自动运行 Sweave,使用Jeromy Anglim概述的方法取得了巨大成功。我最近听说了 cacheSweave 包,我想将该功能合并到我的 Rnw 文件中。我使用 ProjectTemplate 包在启动时加载所有必要的文件,这需要一些时间,因为我必须预处理原始数据文件。cacheSweave 小插图中的示例展示了如何在 R 会话中使用 cacheSweave 驱动程序运行 Sweave:

library(cacheSweave)
Sweave("foo.Rnw", driver = cacheSweaveDriver)

如何在命令中使用 cacheSweaveDriver 以批处理模式运行 Sweave?在我的 makefile 中,这就是我调用 Sweave 的方式:

$(TEXFILE).tex: $(TEXFILE).Rnw
        R CMD SWeave $(TEXFILE).Rnw
        R CMD Stangle $(TEXFILE).Rnw

我正在使用 Emacs+ESS 创建 .Rnw 文件并运行 make。这是我的makefile的其余部分以供参考:

TEXFILE=report_presentation
PLOTDIR= ../graphs
PLOTS=
FIGURES= $(PLOTDIR)/$(PLOTS)
INPUTS=

all: $(TEXFILE).pdf; make clean

.PHONY: all clean

 $(TEXFILE).pdf: $(TEXFILE).tex $(FIGURES) $(INPUTS)
# Initial run
pdflatex $(TEXFILE)

# Run bibtex if missing citations
@if(grep "Citation" $(TEXFILE).log > /dev/null);\
then \
    bibtex $(TEXFILE);\
    pdflatex $(TEXFILE); \
fi

# Recompile if instructed
@if(grep "Rerun" $(TEXFILE).log > /dev/null);\
then \
    pdflatex $(TEXFILE); \
fi

    $(TEXFILE).tex: $(TEXFILE).Rnw
        R CMD Sweave $(TEXFILE).Rnw
        R CMD Stangle $(TEXFILE).Rnw

    ## Remove unnecessary files
    clean:
       -rm -f $(TEXFILE).log $(TEXFILE).aux $(TEXFILE).out $(TEXFILE).blg $(TEXFILE).bbl $(TEXFILE).nav $(TEXFILE).snm $(TEXFILE).toc Rplots.pdf
4

1 回答 1

3

Gregor Gorjanc 有一个 shell 脚本允许这样做:

http://ggorjan.blogspot.com/2008/11/sweavesh-plays-with-cachesweave.html

它比我自制的解决方案更优雅:创建一个名为“runcachesweave.R”的简单文件,其中包含:

library(cacheSweave)
Sweave("foo.Rnw", driver = cacheSweaveDriver)

然后调用 R CMD BATCH runcachesweave.R;latexmk -pdf foo.tex

于 2011-02-08T16:16:25.453 回答