【问题标题】:Create multiiple rmarkdown reports with one dataset使用一个数据集创建多个降价报告
【发布时间】: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` &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))`

```

这会创建一个文件:


我想为数据中列出的每个“运动”创建一个 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'))    
}
  1. 知道上面这段代码会发生什么吗?
  2. 是否可以直接将这些文件创建为 PDF 文档而不是 html?我知道我可以在事后点击每个文件将它们保存为 pdf,但我将有 40 个不同的运动文件可供使用。
  3. 是否可以在文件中的每个“提交”文章之间添加细线?

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

【问题讨论】:

    标签: r r-markdown


    【解决方案1】:

    这可以通过像这样的参数化报告来实现:

    1. 为数据添加参数,例如适合您 Rmd 的运动类型
    2. lapply 内部,通过参数paramssubgroup 数据集传递给render
    3. 您可以通过***添加水平线
    4. 如果您想要 pdf,请使用 output_format="pdf_document"。此外,为了呈现您的文档,我必须通过output_options 切换乳胶引擎

    Rmd:

    ---
    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))
    }
    

    【讨论】:

    • 感谢您的回答!我已经调整了您的代码以生成 word 格式,它看起来很棒!是否可以在每篇文章之间添加分页符而不是一行?我试过 \newpage 但它不起作用.. bookdown.org/yihui/rmarkdown-cookbook/pagebreaks.html
    • 嗯。您是否使用或尝试过使用双反斜杠,即\\newpage?这样做对我来说很好。
    【解决方案2】:

    为了直接从您的 rmd 文件创建 pdf,您可以在加载数据的单独 R 脚本中使用以下函数,然后使用 purrr 包中的 map 来迭代数据(在 rmd -file 输出必须设置为 pdf_document):

    library(tidyverse)
    library(lazyeval)
    
        get_report <- function(sport){
          sport <- enquo(sport)
          mydata <- mydata %>%  
            filter(Sport == !!sport)
          render("test.rmd", output_file = paste('report_', as_name(sport), '.pdf', sep=''))
        }
        
        map(as.vector(data$Sport), get_report)
    

    希望这就是你要找的吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2021-12-22
      • 1970-01-01
      • 2017-03-16
      • 2018-03-12
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多