简体   繁体   English

如何将table1 package output表保存为.doc格式? R

[英]How to save table1 package output table to .doc format? R

Package "table1" allows for some amazing tables to be made. Package "table1" 允许制作一些惊人的表格。 However, i can't figure how to export one to.doc format.但是,我不知道如何导出一种 to.doc 格式。

Here is an example code:这是一个示例代码:

table1( ~ x | Y, data = df)

I wonder if it is possible to somehow save resulting table into.doc format so it still looks like this:我想知道是否有可能以某种方式将结果表保存为 .doc 格式,因此它仍然看起来像这样:

在此处输入图像描述

Turns out it's very simple:结果很简单:

Just cmd+a the table in Rstudio viewer -> cmd+c -> paste Special in MS Word -> chose .html format!只需 cmd+a Rstudio 查看器中的表格 -> cmd+c -> 在 MS Word 中粘贴 Special -> 选择 .html 格式!

I'm sorry to dig this up.我很抱歉挖掘这个。 However I learnt to love the two packages table1 and aswell flextable and one other route would be to fastly convert the table1 object to a flextable然而,我学会了喜欢这两个包table1flextable ,另一种方法是快速将 table1 object 转换为 flextable

library(table1)
library(flextable)
library(magrittr)

# Create Table1 Object
tbl1<- table1(~Sepal.Length+Sepal.Width|Species,data=iris)
# Convert to flextable
t1flex(tbl1) %>% 
  save_as_docx(path="Iris_table1.docx")

Note that this give you the full flexibility over working with a flextable and an easy and save way to export to word.请注意,与使用 flextable 相比,这为您提供了充分的灵活性,并提供了一种简单且保存的导出到 word 的方式。 A nice guide on additional formatting with flextable can be found here可以在此处找到有关使用 flextable 进行额外格式化的不错指南

Another possible solution: The above strategy did not work for me when I had a similar issue, but it was resolved once I knitted the table1 object and opened the html in browser to copy the html table and successfully paste into word.另一种可能的解决方案:当我遇到类似问题时,上述策略对我不起作用,但是一旦我编织 table1 对象并在浏览器中打开 html 以复制 html table 并成功粘贴到 word 中,它就解决了。 Doing it within RStudio viewer would not work for me for some reason.由于某种原因,在 RStudio 查看器中执行此操作对我不起作用。

You can also save it as a.csv file for further manipulation (and from there to word)您还可以将其另存为 a.csv 文件以供进一步操作(并从那里到 word)

mytable <- table1( ~ x | Y, data = df)

write.table (mytable , "my_table_1_file.csv", col.names = T, row.names=F, append= T, sep=',')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM