问题
I have a csv file such as this:
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."))
I am trying to render some of this data into a neatly formatted word document. This is my desired output:
I tried playing around with Rmarkdown, but I don't think it has the functionality to create this kind of document.
Any suggestions for how I can do this? Appreciate any suggestions! Thank you!
What I tried:
ReporteRs I found this http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package, but this package does not exist for R version 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/ The rcom dependency is causing issues.
回答1:
Have you thought about rendering straight to HTML? Then you could just print to pdf from the web browser if you needed a static copy besides HTML. Rmarkdown plays very nicely with these 2 file types, making it easier for you to create customized reports.
---
title: "Rmarkdown report"
output: html_document
---
##
**First:** John   **First:** Smith <br>
**Age:** 12   **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   **First:** Smith <br>
**Age:** 12   **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.
But if you wanted to index your .csv file, you could call it directly inside Rmarkdown using indexing and inline R code. But for simplicity I had to edit the submissions part of the .csv as I had issues with syntax.
---
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]`   **First:** `r data[1,2]` <br>
**Age:** `r data[1,"Age"]`   **Sport:** `r data[1,"Sport"]` <br>
**submission** <br>
`r data[[5]][2]`
***
**First:** `r data[2,1]`   **First:** `r data[2,2]` <br>
**Age:** `r data[2,"Age"]`   **Sport:** `r data[2,"Sport"]` <br>
**submission** <br>
`r data[[5]][1]`
Which gives us a similar output like you requested with indexing your .csv
回答2:
You can use cat
to add the HTML code provided by Daniel Jachetta to an R markdown chunk in order to loop through your data.
Important
You have to add results = "asis"
to {r}
Here is the loop:
{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], "  **Last:** ", data[i, 2], "<br> \n")
cat("\n **Age:** ", data[i, 3], "  **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
}
Here is the result:
来源:https://stackoverflow.com/questions/64807883/render-data-file-into-a-formatted-word-document-in-r