我分析来自许多城市的测量数据(数百个),并且需要为每个城市创建单独的报告(采用Adobe pdf格式).
我的过程是这样的:
这非常繁琐,看起来非常适合每个城市的循环,但它怎么能完成呢?
谢谢r-contributors!
/克里斯
在这个答案中,@Yihui表示knitr利用全球环境.这让我很困惑 - 我的经历是,它没有.我从来没有真正使用过knit,我通常直接使用PDF.
在一个小实验中,似乎knit确实使用了全局环境(或者使用envir参数指定的任何环境),但knit2pdf事实并非如此.
最小的例子:global_test.Rnw文件
\documentclass{article}
\begin{document}
<<>>=
print(x)
@
\end{document}
Run Code Online (Sandbox Code Playgroud)
R脚本:
x <- "Hello World"
knit(input="global_test.Rnw")
# Works as expected, could now call tools::texi2pdf to generate pdf.
knit2pdf(input="global_test.Rnw")
# Doesn't
Run Code Online (Sandbox Code Playgroud)
后者生成PDF文件,不会显示并发出警告:
running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --quiet --pdf
"global.pdf" -I "C:/PROGRA~1/R/R-215~1.3/share/texmf/tex/latex" -I
"C:/PROGRA~1/R/R-215~1.3/share/texmf/bibtex/bst"' had status 1
Run Code Online (Sandbox Code Playgroud)
我尝试将环境传递给knit2pdf(envir = globalenv())希望它会被...传递,我只是得到一个未使用的参数错误.
一般来说,我知道,参照全球环境表现欠佳,但有没有办法做到这一点的knit2pdf,或者明确地传递一个环境,还是我最好使用brew并sprintf作为@ Ramnath的回答上述同样的问题?
在我的用例中,我认为tools::texi2pdf没有用,因为我需要使用XeLaTeX进行编译,XeLaTeX可以knit2pdf …
我需要生成一个由几个部分组成的报告,所有部分看起来都很相似,只有一些数据差异.部分的数量也取决于数据.我最终想拥有的是这样的:
```{r}
section_names = c("A","B","C")
section_data = c(13,14,16)
```
# some looping mechanism here with variable i
This is section `r section_names[i]`
This section's data is `r section_data[i]`
#more things go here for the section
#end of loop should go here
Run Code Online (Sandbox Code Playgroud)
结果应该是一个单独的html /文档,所有部分一个接一个.
你能指点我用循环生成这样一个Rmd文件的方法吗?
理想情况下,我希望看到像PHP中的东西:
<$php for(i=0;i<10;i++) { ?>
## some html template + code chunks here
<$php } ?>
Run Code Online (Sandbox Code Playgroud) 我想创建一个循环,它允许我自动保存从.Rmd文件生成的PDF报告.例如,如果变量"ID"有10行,我希望R自动将10个报告保存到特定目录中.这些报告应根据所选ID而有所不同.
之前的帖子(使用循环与knitr生成多个pdf报告......需要一些帮助让我超越驼峰)处理了从.Rnw文件生成的多个pdf报告的创建.我尝试按如下方式应用该方法:
#Data
```{r, include=FALSE}
set.seed(500)
Score <- rnorm(40, 100, 15)
Criteria1<-rnorm(40, 10, 5)
Criteria2<-rnorm(40, 20, 5)
ID <- sample(1:1000,8,replace=T)
df <- data.frame(ID,Score,Criteria1,Criteria2)
#instead of manually choosing the ID:
subgroup<- subset(df, ID==1)
# I would like to subset the Data through a loop. My approach was like like this:
for (id in unique(df$ID)){
subgroup<- df[df$ID == id,]}
```
```{r, echo=FALSE}
#Report Analysis
summary(subgroup)
```
#Here will be some text about the summary.
# At …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在 R 中自动填写 PDF 表单的方法。我找不到为此而编写的包。有选择吗?
我能想到的替代解决方案:
所有这些事情在 Python 中似乎都是可行的。然而,我的组织强烈倾向于 R,并且过去一直依赖软件开发人员编写 C# 来填写表格。我希望使用 R 来跳过这一步。
谢谢!