占位符R输出

时间:2014-01-07 22:38:12

标签: r knitr

我想生成几个PDF页面。每个人都有相同的内容,除了我想从R填写的占位符。我真的不知道如何搜索它,单词占位符可能不正确... 这就是我的意思:

\documentclass[a4paper]{article}
\begin{document}
This will be the header of each page, with formatted text etc.\\
Then we need a placeholder for output from R:\\
<<fakedata, echo=FALSE>>=
Names <- c("John", "Mary", "Tom")
# In reality, there is more and more complex data
for(i in 1:3)
print(Names[i])
@
But I only want one name here, and then the rest of the Latex code output. On the next page should be all the rest again, with the second name, and so on.\\[\baselineskip]
Then there is more stuff filling the page, optimally also including the i-th output of other stuff. This also includes a table and form fields (thus LATEX code), so I can not just use the R commands paste and cat...\\
Thanks for any ideas!
\end{document}

3 个答案:

答案 0 :(得分:1)

1)创建一个名为doc.Rnw.0的文件,例如,包含:

\documentclass[a4paper]{article}
\begin{document}
This will be the header of each page, with formatted text etc.\\
Then we need a placeholder for output from R:\\
<<fakedata, echo=FALSE>>=
# In reality, there is more and more complex data
$Name
@
But I only want one name here, and then the rest of the Latex code output. On the next 
page should be all the rest again, with the second name, and so on.\\[\baselineskip]
Then there is more stuff filling the page, optimally also including the i-th output of other 
stuff. This also includes a table and form fields (thus LATEX code), so I can not just use 
the R commands paste and cat...\\
Thanks for any ideas!
\end{document}

然后在R中运行:

library(gsubfn)

doc <- readLines("doc.Rnw.0")
doc <- paste(doc, collapse = "\n")
Names <- c("John", "Mary", "Tom")

for(i in 1:3) {
    Name <- Names[i]
    fn$cat(doc, file = paste("doc", Name, "Rnw", sep = "."))
}

这将为Name的每个值创建一个单独的文件。

以上假设我们不在文件替换之外的文件中使用美元或反引号 如果不成功,请将$Name中的doc.Rnw.0替换为@Name,并将fn$cat(...)语句替换为:

pat <- pattern <- "@([[:alpha:]][[:alnum:].]*)
doc2 <- gsubfn(pat,, doc)
cat(doc2, file = paste("doc", Name, "Rnw", sep = "."))

请注意,我们可能会有很多替换。例如,如果我们NameAddress只在R中定义,则使用$Name$Address(或@Name@Address我们在doc.Rnw.0文件中的适当位置使用替代方案。

2) brew包是另一种方法。

答案 1 :(得分:1)

我的pander package中具有降价风格brew的替代解决方案,内部处理此类循环(基于brew <% ... %>标记。快速演示:

使用以下内容创建文件(如demo.pander):

# This will be the header of each page, with formatted text etc.

Then we need a placeholder for output from R.

<% 
Names <- c("John", "Mary", "Tom")
for(i in 1:3) {
%>

This sentence will be printed for each `Names` and the R code run: <%= Names[i] %>

<% } %>

But I only want one name here, and then the rest of the Latex code output. On the next page should be all the rest again, with the second name, and so on.

请注意,我使用<% ... %>标记来运行R代码(如循环),但<%= ... %>用于打印结果。后者也在返回的R对象上应用pander,因此将在Pandoc的降价中返回。

然后只需致电Pandoc.brew

> library(pander)
> Pandoc.brew('demo.pander')
# This will be the header of each page, with formatted text etc.

Then we need a placeholder for output from R.

This sentence will be printed for each `Names` and the R code run: John

This sentence will be printed for each `Names` and the R code run: Mary

This sentence will be printed for each `Names` and the R code run: Tom

But I only want one name here, and then the rest of the Latex code output. On the next page should be all the rest again, with the second name, and so on. 

现在您有一个可以转换为texpdf或任何其他格式的降价文件。

答案 2 :(得分:0)

感谢目前为止的两个回复 - 相当不错的想法。当我醒来时,我还有另一种解决方案,我也想尝试一下。没有包,纯R代码然后创建一个创建单个pdf的tex文档。在我的情况下,我想要一个包含许多(几乎)相同页面的PDF,我可以像这样打印。 这是代码:

# Create Latex File with Placeholder for R output
# Berry Boessenkool, Jan 2014, berry-b@gmx.de
# Remember to replace Latex's Backslash with two \\

# What file to create:
outputfile <- "Test3.tex"

# Document preamble:
cat("\\documentclass[a4paper]{article}
\\begin{document}\n", file=outputfile)

# R results for placeholder:
Names <- c("John", "Mary", "Tom")

# Main content, to be repeated on each page:
for(i in 1:3)
{
cat(paste(
"This will be the header of each page, with formatted text etc.\\\\
Then we need a placeholder for output from R:\\\\",
Names[i],
"\\\\and then the rest of the Latex code output. \\\\[\\baselineskip]
On the next page should be all the rest again, with the second name, and so on.\n
\\newpage\n"
, sep="\n"), file=outputfile, append=T)
}

# End of document:
cat("\n\\end{document}", file=outputfile, append=T)

最有可能的是,也可以从单独的文件中读取不变的页面内容。烦人的\\可以避免...... 谢谢你的想法,我可能会在以后需要它们!