数据框到字表?

时间:2014-08-21 12:08:05

标签: r r-markdown

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

如果我在RStudio中使用rmarkdown创建一个Word文档,我会得到一个很好的打印表,但它不会被识别为Word表。它可以完成吗?

   ```{r}
   name_of_df
   ```

3 个答案:

答案 0 :(得分:18)

编辑:仍然保留了ReporteRs,但不再发展。请改用officerflextable

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)

结束编辑

您好,您也可以尝试使用包ReporteRs将data.frame转换为Word表格,执行此操作的函数是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")

enter image description here

使用markdown,我认为这只适用于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))
```

这是另一个关于如何使用ReporteRs创建word文档的示例:

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")

enter image description here

有关更多示例,您可以访问http://davidgohel.github.io/ReporteRs/word.html

答案 1 :(得分:4)

这是一种非常低技术(快速)的方法

source("http://michael.hahsler.net/SMU/EMIS7332/R/copytable.R") 

并运行示例

copytable(summary(iris))

现在复制并粘贴,你就完成了!

仅供参考,这里也是copytable函数的源代码:

library(xtable)

copytable <- function(x, ...) {
  f <- tempfile(fileext=".html")
  print(xtable(x, ...), "html", file = f)
  browseURL(f)
}

答案 2 :(得分:1)

您可以使用rmarkdownknitr个套件。这是一个示例函数,其中dfdata.frameoutput_file是输出文件名,output_dir是输出文件夹。额外命名的参数将发送到rmarkdown::render

df2word <- function(df, output_file = "table.doc", output_dir = ".", ...) {
  f <- tempfile(fileext =".Rmd" )
  cat(file = f, '```{r, echo = FALSE}
                  knitr::kable(df)
                 ```', append = TRUE)
  rmarkdown::render(f, output_file = output_file, output_dir = output_dir, ...)
  unlink(f)
  file.path(output_dir, output_file)
}

out <- df2word(mtcars)
browseURL(out)

当然,您可以使代码更灵活。

虽然足够灵活,但您也可以提取其他格式,例如HTML:

out <- df2word(mtcars, output_file = "table.html")
browseURL(out)

或pdf:

out <- df2word(mtcars, output_file = "table.pdf", output_format = rmarkdown::pdf_document())
browseURL(out)