我正在尝试使用Rmarkdown将数据转换为html文档,并且目前依靠转换为矢量和索引来解决我的问题。
尽管我的样本数据有4个观测值,但我的实际数据集却有30多个记录,因此索引似乎很麻烦且不自然。
是否有更好的方法按顺序提取这些元素?任何建议都很好。
--
title: "Rmarkdown report"
output: html_document
---
```{r echo = FALSE}
mydata <- 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<- as.vector(mydata$First)
sec <- as.vector(mydata$Second)
age <- as.vector(mydata$Age)
submission <- as.vector(mydata$submission)
```
##
**First:** `r first[1]`   **Second:** `r sec[1]` <br>
**Age:** `r age[1]`
**submission** <br>
`r submission[1]`
***
**First:** `r first[2]`   **Second:** `r sec[2]` <br>
**Age:** `r age[2]`
**submission** <br>
`r submission[2]`
这是一种遍历所有行的方法
---
title: "Rmarkdown report"
output: html_document
---
```{r echo = FALSE}
# using data from above
# mydata <- data.frame(...)
# Define template (using column names from data.frame)
template <- "**First:** `r First`   **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))`
在这里,我们使用knit_child
一个模板字符串,然后将其用于data.frame的每一行。我在这里使用了一个技巧,将data.frame的行作为环境传递,以便模板可以将所有列视为变量,因此我们无需创建所有data.frame列的向量版本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句