如何生成不带尖括号“>”的 sweave 文档在代码块前面?
如何生成不带尖括号“>”的 Sweave(或 pgfSweave)文档在代码块前面?我希望人们能够直接从 pdf 输出中剪切并粘贴我的代码。这是我的文档的一个代码块片段:
请注意,因为我们的关联矩阵由 0 和 1 组成,所以非对角线条目表示公共列的总数,这正是我们想要的。我们将使用 %*% 运算符告诉 R 执行此操作。让我们首先看一个使用人员及其所属组的玩具数据的小示例。我们将使用 table() 函数将数据强制转换为关联矩阵,然后将关联矩阵与其转置相乘,以获得人与人之间共同群体的数量。
>\> ToyDat <- data.frame(person = c("Sam", "Sam", "Sam", "Greg", "Tom", "Tom"), group = c("a", "b", "c", "a", "b", "c"), stringsAsFactors = F)
>\> ToyDatM <- as.matrix(table(ToyDat))
>\> ToyDatM
但我希望代码看起来像:
> ToyDat <- data.frame(person = c("Sam", "Sam", "Sam", "Greg", "Tom", "Tom"), group = c("a", "b", "c", "a", "b", "c"), stringsAsFactors = F)
> ToyDatM <- as.matrix(table(ToyDat))
> ToyDatM
How can I produce a Sweave (or pgfSweave) document without angle brackets ">" in front of code chunks? I want people to be able to cut and paste my code directly from the pdf output. Here's a snippet of my document with a code chunk:
Notice that because our incidence matrix consists of 0’s and 1’s, the off-diagonal entries represent the total number of common columns, which is exactly what we wanted. We’ll use the %*% operator to tell R to do this. Let’s first take a look at a small example using toy data of people and groups to which they belong. We’ll coerce the data to an incidence matrix using the table() function, then multiply the incidence matrix by its transpose to get the number of common groups between people.
>\> ToyDat <- data.frame(person = c("Sam", "Sam", "Sam", "Greg", "Tom", "Tom"), group = c("a", "b", "c", "a", "b", "c"), stringsAsFactors = F)
>\> ToyDatM <- as.matrix(table(ToyDat))
>\> ToyDatM
But I want the code to look like:
> ToyDat <- data.frame(person = c("Sam", "Sam", "Sam", "Greg", "Tom", "Tom"), group = c("a", "b", "c", "a", "b", "c"), stringsAsFactors = F)
> ToyDatM <- as.matrix(table(ToyDat))
> ToyDatM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为脚本顶部的
options(prompt = " ")
可以做到这一点。prompt
(在options()
中)控制交互式会话中用于提示的文本字符串,我假设它会对通过 Sweave 处理的文档执行相同的操作。编辑:感谢 Ben Bolker 指出
options(prompt = " ", continue = " ")
也将解决“+”问题。I think
options(prompt = " ")
at the top of your script will do it.prompt
(inoptions()
) controls the text string used for the prompt in an interactive session and I'm assuming it will do the same for a document processed through Sweave.EDIT: Thanks to Ben Bolker for pointing out that
options(prompt = " ", continue = " ")
will also take care of the "+" problem as well.也许更好的策略是在 Rnw 文件上提供
Stangle
的结果 - 它将包含整个文档中的命令,作为可以提交到 R 或剪切和粘贴的脚本进入 R 会话。Maybe a better strategy is to provide the result of
Stangle
on your Rnw file -- it'll contain the commands from the entire document, as a script that can be submitted to R or cut-and-paste into an R session.