【问题标题】:Only run knitr on parts of Rnw file that have changed仅对 Rnw 文件中已更改的部分运行 knitr
【发布时间】:2015-06-14 23:14:21
【问题描述】:

假设我在 Rnw 文件中包含两个代码块,code_block_1code_block_2。假设我对code_block_1 进行了更改,但code_block_2 保持不变。

我正在使用knitrRnw 文件转换为tex 文件。因为code_block_2 保持不变,我可以让knitr 只评估和运行code_block_1 吗?

【问题讨论】:

    标签: r latex knitr sweave rnw


    【解决方案1】:

    首先在此处查看knitr 的选项:http://yihui.name/knitr/options/。我认为您正在寻找的是 cache 选项。试试这个小例子,你会发现时间从一次运行到另一次只发生在你实际更改代码的块中:

    第一次运行:

    \documentclass{article}
    \begin{document}
    
    <<code_block_1, cache=TRUE>>=
    set.seed(123)
    x <- rnorm(10)
    summary(x)
    Sys.time()
    @
    
    <<code_block_2, cache=TRUE>>=
    set.seed(123)
    y <- rnorm(10)
    summary(y)
    Sys.time()
    @
    
    \end{document}
    

    输出:

    第二次运行(在第二个块中添加注释后):

    \documentclass{article}
    \begin{document}
    
    <<code_block_1, cache=TRUE>>=
    set.seed(123)
    x <- rnorm(10)
    summary(x)
    Sys.time()
    @
    
    <<code_block_2, cache=TRUE>>=
    # Just added a comment in this chunk
    set.seed(123)
    y <- rnorm(10)
    summary(y)
    Sys.time()
    @
    
    \end{document}
    

    输出:

    【讨论】:

      猜你喜欢
      • 2014-01-24
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      相关资源
      最近更新 更多