如何使用sweave R处理不同的长任务?

时间:2013-11-12 14:39:12

标签: r latex sweave

我正在使用Rstudio和sweave来构建报告。 一切都运作良好,但我必须做很多不同的繁重计算。 他们每个人花费不同的时间。

我的文件现在看起来像这样:

    \documentclass{article}

    \usepackage{amsmath}
    \usepackage{amscd}
    \usepackage[tableposition=top]{caption}
    \usepackage{ifthen}
    \usepackage[utf8]{inputenc}

    \begin{document}
    \SweaveOpts{concordance=TRUE}

    \title{OES dataset}
    \author{Luca Puggini}

    \maketitle

    \section{The dataset}
   This is my data:
<<echo=T>>=
#suppose this  is a very heavy task that takes a lot of time
x=matrix(rnorm(100),10,10)
y=rnorm(10)
@
\section{operation}
Now let's do some operations 
<<echo=T>>=
x=x+1
y=y+10
@

\end{documents}

现在我更改了最后一行,例如

y=y+10000 # instead of y=y+10

如何在不重新计算所有任务(但只包括最后一项)的情况下编译pdf?

编辑: 可能最好的办法是切换到knitr并使用缓存。 有人可以写一下如何用缓存解决这个问题的代码吗?

1 个答案:

答案 0 :(得分:2)

查看涵盖了许多缓存解决方案的Task View for Reproducible Research

缓存解决方案也很容易在一个块中自行完成:

 if (!exists(someVar)) {
     if (file.exists("cache/someVar.rds")) {
         someVar <- readRDS("cache/someVar.rds")
     } else {
         someVar <- reallyExpensiveComputation()
         saveRDS(someVar, "cache/someVar.rds")
     }
 }

knitr也从一开始就提供它。