将数据文件渲染为R

问题描述

我有一个这样的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文档。这是我想要的输出

enter image description here

我尝试使用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依赖性导致问题。

解决方法

您是否考虑过直接渲染为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,"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.

enter image description here

但是,如果您想索引.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,which are critical to modern technologies. These 17 elements,are sparsely distributed in the Earths crust. Without the rare earths,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.",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编制索引时,这会给我们类似的输出

enter image description here

,

您可以使用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:** ",2],"<br> \n")
  
  
  cat("\n **Age:** ",3],"&emsp; **Sport:** ",4],"<br> \n")
  
  cat("\n **submission** ",5],"<br> \n")
  # cat("\n <br> \n") extra space between entries
  cat("\n *** \n") line between entries
  
  i = i + 1
}

结果如下: enter image description here