【问题标题】:A Way in Knitr to Copy a Chunk?一种在knitr中复制块的方法?
【发布时间】:2015-08-22 00:02:28
【问题描述】:

针织专家,

背景:使用 knitr 报告带有许多嵌入式图表的报告。在报告的正文中,合适的只是图表,而不是代码。

例如:

```{r graph_XYZ_subset, echo = FALSE, message = TRUE, 
                        fig.cap = "Text that explains the graph"}

graph.subset <- ggplot() + ...

```

这部分工作得很好。

但是,需要显示代码的关键部分(例如关键统计分析和关键图表生成)...但在附录中

这导致了这个问题:有没有办法将 knitr 块从脚本的早期部分复制到后面的部分?

为确保准确性,附录中的代码最好列出(显示)报告中实际执行的所有代码。

例如:

# ADDENDUM - Code Snippets

### Code to Generate Subset Graph

\\SOMEHOW COPY the code from graph_XYZ_subset to here without executing it.

### Code to Compute the Mean of Means of the NN Factors

\\Copy another knitr chunk which computes the mean of means, etc.

### And So On...

\\Copy chunks till done

* * * * * * * * 

有什么想法吗? knitr 中有没有办法执行这些类型的块副本?

【问题讨论】:

    标签: r knitr reproducible-research


    【解决方案1】:

    有几个选项,其中有四个 listet 并在下面简要说明。易慧在How to reuse chunks的解释也可能有帮助。

    \documentclass{article}
    \begin{document}
    
    \section{Output}
    <<mychunk, echo = FALSE>>=
      print("Hello World!")
    @
    
    \section{Source code}
    Option 1: Use an empty chunk with the same label.
    <<mychunk, eval = FALSE>>=
    @
    
    Option 2: Embed in other chunk (no advantage in this case). Note that there is no equality sign and no at for the inner chunk.
    <<myOtherChunk, eval = FALSE>>=
    <<mychunk>>
    @
    
    Option 3: Use \texttt{ref.label}.
    <<ref.label = "mychunk", eval = FALSE>>=
    @
    
    Option 4: Define the chunk in an external file you read in using \texttt{read\_chunk}. Then use Option 1--3 to execute the chunk (with \texttt{eval = TRUE}; default) or show it's code (with \texttt{eval = FALSE}).
    \end{document}
    

    我通常更喜欢选项 4。这允许您将编程逻辑与编写文档分开。

    在执行mychunk 并且图形将出现在PDF 中的地方,您的Rnw 文件中只有&lt;&lt;mychunk&gt;&gt;=,并且不必费心处理生成图形的所有代码。开发代码也更容易,因为在交互式会话中,您可以将所有代码放在一个位置,并且在从一个块转到下一个块时不必滚动浏览报告的所有文本。

    编辑: 上述选项的共同点是您需要手动维护要在附录中显示的块列表。这里有两个选项可以避免这种情况;不幸的是,两者都有一些缺点:

    选项 1:自动创建所有已执行块的列表并显示其代码。

    这可以使用注册所有块名称的chunk hook 来实现。在文档中的所有其他块之前包含以下块:

    <<echo = FALSE>>=
    library(knitr)
    myChunkList <- c()
    
    listChunks <- function(before, options, envir) {
      if (before) {
        myChunkList <<- c(myChunkList, options$label)
      }
      return(NULL)
    }
    
    knit_hooks$set(recordLabel = listChunks) # register the new hook
    opts_chunk$set(recordLabel = TRUE) # enable the new hook by default
    @
    

    在您想要显示代码的地方(例如在附录中),插入以下代码块:

    <<showCode, ref.label = unique(myChunkList), eval = FALSE>>=
    @
    

    不幸的是,块之间没有边距或任何其他视觉分隔。

    选项 2:并不总是需要使用块挂钩,因为有函数 all_labels() 返回所有块标签的列表。但是,您的文件中可能有未执行的块,您可能不想看到它们的代码。此外,选项 1 允许通过在其块选项中设置 recordLabel = FALSE 来跳过某些块。

    【讨论】:

    • 您在纯乳胶中提供了一个带有 sweave 代码块的解决方案,OP 要求提供 knitr 解决方案。我认为你根本没有回答这个问题。
    • 如果您将我发布的代码粘贴到 Rnw 文件中,您可以 knit 将其转换为 PDF。为什么这不是knitr 解决方案?
    • 因为发布的示例 OP 使用了 tripe 反引号,它们表示 R 降价中的块......所以我认为 LaTeX 方法是无用的。
    • 没有。 RmdRnw 文件对代码块使用的语法略有不同(参见 knitr reference card),但逻辑是相同的。我直接发布的所有内容也适用于Rmd
    猜你喜欢
    • 1970-01-01
    • 2017-04-22
    • 2016-06-27
    • 2015-11-03
    • 2014-01-03
    • 2016-06-14
    • 1970-01-01
    • 2014-07-30
    • 2021-02-04
    相关资源
    最近更新 更多