【发布时间】:2020-11-14 16:09:39
【问题描述】:
我想在 rmarkdown 中创建几个 pdf 文件。
这是我的数据样本:
mydata <- data.frame(First = c("John", "Hui", "Jared","Jenner"), Second = c("Smith", "Chang", "Jzu","King"), Sport = c("Football","Ballet","Ballet","Football"), Age = c("12", "13", "12","13"), submission = c("Microbes may be the friends of future colonists living off the land on the moon, Mars or elsewhere in the solar system and aiming to establish self-sufficient homes.
Space colonists, like people on Earth, will need what are known as rare earth elements, which are critical to modern technologies. These 17 elements, with daunting names like yttrium, lanthanum, neodymium and gadolinium, are sparsely distributed in the Earth’s crust. Without the rare earths, we wouldn’t have certain lasers, metallic alloys and powerful magnets that are used in cellphones and electric cars.", "But mining them on Earth today is an arduous process. It requires crushing tons of ore and then extracting smidgens of these metals using chemicals that leave behind rivers of toxic waste water.
Experiments conducted aboard the International Space Station show that a potentially cleaner, more efficient method could work on other worlds: let bacteria do the messy work of separating rare earth elements from rock.", "“The idea is the biology is essentially catalyzing a reaction that would occur very slowly without the biology,” said Charles S. Cockell, a professor of astrobiology at the University of Edinburgh.
On Earth, such biomining techniques are already used to produce 10 to 20 percent of the world’s copper and also at some gold mines; scientists have identified microbes that help leach rare earth elements out of rocks.", "Blank"))
在社区的帮助下,我得到了一个很酷的 rmarkdown 解决方案,它可以创建一个包含我想要的所有数据的 html 文件。
这被保存为 Word.Rmd 的文章
```{r echo = FALSE}
# using data from above
# mydata <- mydata
# Define template (using column names from data.frame)
template <- "**First:** `r First`   **Second:** `r Second` <br>
**Age:** `r Age`
**Submission** <br>
`r Submission`"
# Now process the template for each row of the data.frame
src <- lapply(1:nrow(mydata), function(i) {
knitr::knit_child(text=template, envir=mydata[i, ], quiet=TRUE)
})
```
# Print result to document
`r knitr::knit_child(text=unlist(src))`
```
这会创建一个文件:
我想为数据中列出的每个“运动”创建一个 html(或者最好是 PDF 文件)。因此,我会将“芭蕾舞”学生的所有提交内容放在一个文件中,并将所有踢足球的学生提交的文件放在一个单独的文件中。
我一直在寻找一些不同的解决方案,我发现这是最有帮助的: R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop?
按照套件,我创建了一个单独的 R 脚本来循环遍历数据并按运动对数据进行子集化: 不幸的是,这将创建一个包含所有学生的单独文件,而不仅仅是属于该运动的学生。
for (sport in unique(mydata$Sport)){
subgroup <- mydata[mydata$Sport == sport,]
render("Essay to Word.Rmd",output_file = paste0('report.',sport, '.html'))
}
- 知道上面这段代码会发生什么吗?
- 是否可以直接将这些文件创建为 PDF 文档而不是 html?我知道我可以在事后点击每个文件将它们保存为 pdf,但我将有 40 个不同的运动文件可供使用。
- 是否可以在文件中的每个“提交”文章之间添加细线?
任何帮助都会很棒,谢谢!!!
【问题讨论】:
标签: r r-markdown