gpt4 book ai didi

r - 使用一个数据集创建多个 rmarkdown 报告

转载 作者:行者123 更新时间:2023-12-04 08:35:11 26 4
gpt4 key购买 nike

我想在 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 文件。
这被保存为 Essay to Word.Rmd
```{r echo = FALSE}
# using data from above
# mydata <- mydata

# Define template (using column names from data.frame)
template <- "**First:** `r First` &emsp; **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))`

```
这将创建一个文件:
enter image description here

我想为数据中列出的每个“运动”创建一个 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 个不同的体育文件可供使用。
  • 是否可以在文件中的每篇“提交”文章之间添加一条细线?

  • 任何帮助都会很棒,谢谢!!!

    最佳答案

    这可以通过参数化报告来实现,如下所示:

  • 为数据添加参数,例如适合您的运动类型
  • lapply传递您的 subgroup数据集到 render通过参数 params
  • 您可以通过 *** 添加水平线
  • 如果您想要 pdf,请使用 output_format="pdf_document" .另外为了渲染你的文档,我不得不通过 output_options 切换 latex 引擎

  • 编号:
    ---
    params:
    data: null
    sport: null
    ---

    ```{r echo = FALSE}
    # using data from above
    data <- params$data

    # Define template (using column names from data.frame)
    template <- "
    ***

    **First:** `r First` &emsp; **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(data), function(i) {
    knitr::knit_child(text=template, envir=data[i, ], quiet=TRUE)
    })
    ```
    # Print result to document. Sport: `r params$sport`
    `r knitr::knit_child(text=unlist(src))`
    R脚本:
    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"))

    for (sport in unique(mydata$Sport)){
    subgroup <- mydata[mydata$Sport == sport,]
    rmarkdown::render("test.Rmd", output_format = "html_document", output_file = paste0('report.', sport, '.html'), params = list(data = subgroup, sport = sport))
    rmarkdown::render("test.Rmd", output_format = "pdf_document", output_options = list(latex_engine = "xelatex"), output_file = paste0('report.', sport, '.pdf'), params = list(data = subgroup, sport = sport))
    }
    enter image description here

    关于r - 使用一个数据集创建多个 rmarkdown 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64835932/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com