编织器中复制块的一种方法? [英] A Way in Knitr to Copy a Chunk?

查看:107
本文介绍了编织器中复制块的一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针织机专家,

背景:使用knitr报告包含许多嵌入式图形的报告.在报告的正文中,所有合适的是图形,而不是代码.

Background: Using knitr to report a report with many embedded graphs. In the body of the report, all that's appropriate is the graph, not the code.

例如:

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

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

```

这部分工作正常.

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

However, there is a need to display the key parts of the code (e.g., key statistical analyses and key graph generations)...but in an Addendum.

哪个导致了这个问题:是否有办法将编织程序块从脚本的早期部分复制到后期部分?

Which leads to this question: is there a way to copy a knitr chunk from the early parts of a script to a later part?

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

To ensure accuracy, it's ideal that the code in the Addendum list (display) all the code that was actually executed in the report.

例如:

# 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

* * * * * * * * 

有什么想法吗?编织器中是否可以执行这些类型的块副本?

Any ideas? Is there a way in knitr to perform these types of chunk copies?

推荐答案

有几个选项,其中四个是listet,下面简要说明. 如何重用块中的Yihui解释也可能有帮助.

There are several options, four of them listet and shortly explained below. Yihui's explanations in How to reuse chunks might also help.

\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.这使您可以将编程逻辑与编写文档分开.

I usually prefer Option 4. This allows you to separate the programming logic from writing the document.

在要执行mychunk的位置,图形将显示在PDF中,您在Rnw文件中仅包含<<mychunk>>=,而不必理会生成图形的所有代码.开发代码也更加容易,因为在交互式会话中,您可以将所有代码放在一个位置,并且在从一个块切换到下一个块时不必滚动查看报告的所有文本.

At the place mychunk is to be exectued and the graph will appear in the PDF, you only have <<mychunk>>= in your Rnw file and don't have to bother with all the code that generates your graph. Developing your code is also easier, because in an interactive session you have all your code at one spot and don't have to scroll through all the text of the report when going from one chunk to the next one.

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

The options mentioned above have in common that you need to manually maintain a list of the chunks to show in the appendix. Here two options to avoid this; unfortunately, both have some drawbacks:

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

Option 1: Automatically create a list of all chunks that have been executed and show their code.

这可以通过注册所有块名称的 chunk hook 来实现.在文档中的所有其他块之前包括以下块:

This can be achieved using a chunk hook that registers all chunk names. Include the following chunk before all other chunks in the document:

<<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
@

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

Where you want to show the code (for example in the appendix), insert the following chunk:

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

不幸的是,这些块之间没有空白或任何其他视觉上的分隔.

Unfortunately, there will be no margin or any other visual separation between the chunks.

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

Option 2: Using the chunk hook is not always necessary because there is the function all_labels() that returns a list of all chunk labels. However, there might be chunks in your file that don't get executed and you probably don't want to see their code. Moreover, option 1 allows skipping certain chunks simply by setting recordLabel = FALSE in their chunk options.

这篇关于编织器中复制块的一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆