数据框到字表? [英] Data frame to word table?

查看:50
本文介绍了数据框到字表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过 rmarkdown 轻松将数据框转换为 Word 表格?

如果我在 RStudio 中使用 rmarkdown 创建一个 Word 文档,我会得到一个打印精美的表格,但它不会被识别为 Word 表格.完全可以做到吗?

 ```{r}name_of_df``

解决方案

ReporteRs 仍在维护,但不会再发展.使用

Is there a way to easily turn a data frame into a Word table via rmarkdown?

If I use rmarkdown in RStudio to create a Word document I get a nicely printed table but it is then not recognised as a Word-table. Can it be done at all?

   ```{r}
   name_of_df
   ```

解决方案

EDIT: ReporteRs is still maintained but will not evolve anymore. Use officer and flextable instead :

library(officer)
library(flextable)
library(magrittr)

# Create flextable object
ft <- flextable(data = mtcars) %>% 
  theme_zebra %>% 
  autofit
# See flextable in RStudio viewer
ft

# Create a temp file
tmp <- tempfile(fileext = ".docx")

# Create a docx file
read_docx() %>% 
  body_add_flextable(ft) %>% 
  print(target = tmp)

# open word document
browseURL(tmp)

END EDIT

Hi you can also try package ReporteRs to turn a data.frame into a Word table, the function to do it is FlexTable :

library(ReporteRs)
library(magrittr)

docx( ) %>% 
  addFlexTable( mtcars %>%
                  FlexTable( header.cell.props = cellProperties( background.color =  "#003366" ),
                             header.text.props = textBold( color = "white" ),
                             add.rownames = TRUE ) %>%
                  setZebraStyle( odd = "#DDDDDD", even = "#FFFFFF" ) ) %>%
  writeDoc( file = "exemple.docx" )

# open the Word document
browseURL("exemple.docx")

With markdown, i think this work only with HTML :

---
title: "HTML table"
output: html_document
---

```{r, results='asis'}
library(ReporteRs)
tabl = FlexTable( mtcars,
                  header.cell.props = cellProperties( background.color = "#003366" ),
                  header.text.props = textBold( color = "white" ),
                  add.rownames = TRUE )
tabl = setZebraStyle( tabl, odd = "#DDDDDD", even = "#FFFFFF" )
cat(as.html(tabl))
```

Here an other example on how to create a word document with ReporteRs :

library(ReporteRs)
# Create a docx object
doc = docx()
# add a document title
doc = addParagraph( doc, "A FlexTable example", stylename = "TitleDoc" )
# add a section title
doc = addTitle( doc, "How to turn a data.frame into a Word table", level = 1 )
# some text
doc = addParagraph( doc, "We use the mtcars dataset as example.", stylename = "DocDefaults" )
# add a table
MyFTable = FlexTable( data = mtcars[1:10, ], add.rownames = TRUE )
# format body content
MyFTable[3:4, "cyl"] = cellProperties( background.color = "red" )
MyFTable["Valiant", ] = cellProperties( background.color = "blue" )
doc = addFlexTable(doc, MyFTable)
# write the doc
writeDoc( doc, file = "exemple.docx" )
# open the Word doc
browseURL("exemple.docx")

For more example you can visit http://davidgohel.github.io/ReporteRs/word.html

这篇关于数据框到字表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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