在 R 中将数据文件渲染为格式化的 Word 文档 [英] Render data file into a formatted word document in R

查看:36
本文介绍了在 R 中将数据文件渲染为格式化的 Word 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 csv 文件:

data <- data.frame(First = c(John", Hui", Jared"), Second = c(Smith", Chang", Chang")Jzu"), Sport = c("Football","Soccer","Ballet"), Age = c("12", "13", "12"), submit = c(";微生物可能是未来殖民者的朋友,他们生活在月球、火星或太阳系其他地方的陆地上,旨在建立自给自足的家园.太空殖民者,就像地球上的人一样,将需要所谓的稀土元素,这对现代技术至关重要.这 17 种元素的名称令人生畏,例如钇、镧、钕和钆,它们稀疏地分布在地壳中.没有稀土,我们就不会有用于手机和电动汽车的某些激光器、金属合金和强力磁铁.",但今天在地球上开采它们是一个艰巨的过程.它需要粉碎成吨的矿石,然后使用化学物质提取这些金属,这些化学物质会留下有毒的废水河流.在国际空间站上进行的实验表明,一种可能更清洁、更有效的方法可能适用于其他世界:让细菌完成从岩石中分离稀土元素的繁琐工作.",这个想法是生物学本质上是催化爱丁堡大学天体生物学教授查尔斯·S·科克尔说,如果没有生物学,这种反应会发生得非常缓慢.在地球上,这种生物采矿技术已经用于生产世界上 10% 到 20% 的铜以及一些金矿;科学家们已经确定了有助于从岩石中浸出稀土元素的微生物."))

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

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

对我如何做到这一点有什么建议吗?感谢任何建议!谢谢!

我的尝试:

记者我发现了这个

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.

解决方案

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], "&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
}

Here is the result:

这篇关于在 R 中将数据文件渲染为格式化的 Word 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆