【问题标题】:Render data file into a formatted word document in R将数据文件渲染为 R 中的格式化 word 文档
【发布时间】:2020-11-12 16:33:45
【问题描述】:

我有一个这样的 csv 文件:

data <- data.frame(First = c("John", "Hui", "Jared"), Second = c("Smith", "Chang", "Jzu"), Sport = c("Football","Soccer","Ballet"), Age = c("12", "13", "12"), 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."))

我正在尝试将其中的一些数据呈现为格式整齐的 Word 文档。这是我想要的输出:

我尝试使用 Rmarkdown,但我认为它没有创建此类文档的功能。

关于如何做到这一点的任何建议?感谢任何建议!谢谢!

我尝试了什么:

记者 我找到了这个http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package,但是对于 R 版本 3.6,这个包不存在。

install.packages('ReporteRs') # Install
Warning in install.packages :
  package ‘ReporteRs’ is not available (for R version 3.6.2)

R2wd https://www.r-bloggers.com/2010/05/exporting-r-output-to-ms-word-with-r2wd-an-example-session/ rcom 依赖导致了问题。

【问题讨论】:

    标签: r r-markdown reporters


    【解决方案1】:

    您是否考虑过直接呈现为 HTML?然后,如果您需要 HTML 之外的静态副本,则可以从 Web 浏览器打印到 pdf。 Rmarkdown 可以很好地处理这两种文件类型,让您更轻松地创建自定义报告。

    ---
    title: "Rmarkdown report"
    output: html_document
    ---
    
    ## 
    
    **First:** John &emsp; **First:**  Smith <br>
    **Age:** 12     &emsp;  **Sport:** Football <br>
    
    
    **submission** <br>
    
    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.
    
    ***
    
    **First:** John &emsp; **First:**  Smith <br>
    **Age:** 12     &emsp;  **Sport:** Football <br>
    
    **submission** <br>
    
    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.
    

    但是,如果您想为 .csv 文件编制索引,您可以使用索引和内联 R 代码在 Rmarkdown 中直接调用它。但为简单起见,我不得不编辑 .csv 的提交部分,因为我遇到了语法问题。

    ---
    title: "Rmarkdown report"
    output: html_document
    ---
        
    ```{r echo = FALSE}
    data <- data.frame(First = c("John", "Hui", "Jared"), Second = c("Smith", "Chang", "Jzu"), Sport = c("Football","Soccer","Ballet"), Age = c("12", "13", "12"), 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 Earths 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.",
    "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."))
        
    ```
        
        
        
        
    ## 
    
    **First:** `r data[1,1]` &emsp; **First:**  `r data[1,2]` <br>
    **Age:** `r data[1,"Age"]`    &emsp;  **Sport:** `r data[1,"Sport"]` <br>
    
    
    **submission** <br>
    
    `r data[[5]][2]`
    
    ***
    
    **First:** `r data[2,1]`  &emsp; **First:**  `r data[2,2]` <br>
    **Age:** `r data[2,"Age"]`    &emsp;  **Sport:** `r data[2,"Sport"]` <br>
    
    **submission** <br>
    
    `r data[[5]][1]`
        
    

    这为我们提供了与您在索引 .csv 时要求的类似输出

    【讨论】:

    • 非常感谢!这真的很有帮助。我想知道是否有一种方法可以循环通过 df 来创建它?我实际的 csv 文件有超过 50 条记录......所以索引似乎有点麻烦。
    • 你当然可以用 R 循环遍历 df!但我建议在另一个问题中提出这个问题,只关注循环方面。然后按照我的建议将其插入您的 Rmd 模板。您可以使用 Base R 或 purrr 库。
    【解决方案2】:

    您可以使用catDaniel Jachetta 提供的HTML 代码添加到R markdown 块中,以便循环访问您的数据。

    重要

    您必须将results = "asis" 添加到{r}

    这是循环:

    {r results="asis", echo = FALSE}
    
    i = 1
    
    NR_OF_ROWS <-
      nrow(data) # number of rows that the loop will go through
    
    while (i <= NR_OF_ROWS) {
      cat("\n **First:** ", data[i, 1], "&emsp; **Last:** ", data[i, 2], "<br> \n")
      
      
      cat("\n **Age:** ", data[i, 3], "&emsp; **Sport:** ", data[i, 4], "<br> \n")
      
      cat("\n **submission** ", data[i, 5], "<br> \n")
      # cat("\n <br> \n") extra space between entries
      cat("\n *** \n") line between entries
      
      i = i + 1
    }
    
    

    结果如下:

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 2016-11-25
      • 2010-09-26
      • 2018-02-20
      • 2016-07-17
      相关资源
      最近更新 更多