轻松将 R 数据框导出并 table 格式化为 Word?
Easy export and table formatting of R dataframe to Word?
是否有一种简单的方法可以将 R 数据框自动转换为 APA 格式的漂亮 Word table 以用于出版手稿?我目前通过将 table 保存在 csv 中,在 excel 中打开它,将 excel table 复制到 Word,并在那里格式化,但是我'我希望有一种方法可以在 R 中自动格式化,这样当我将它转换为 Word 时,它已经是 APA 格式了,因为 Word 的自动化很糟糕。
基本上,我想继续在 Word 中编写手稿本身,同时在 R 中进行分析。然后通过脚本将 R 中的所有结果收集到 table(具有可手动修改的格式)并转换将其转换为任何格式,然后我可以简单地复制粘贴到 Word(以便格式实际保留)。当我需要修改 table 时,我会在 R 中进行更改,然后再次 运行 脚本,而无需在 Word 中进行任何更改。
我不想学习 LaTeX,因为我所在领域的每个人都使用具有跟踪更改等功能的 Word,而我使用 Zotero 加载项进行引用,因此将写作与分析分开会更简单。另外,我是一名心理学家,而不是编码员,因此仅仅为此学习大量新技术对我来说可能不值得付出努力。通常新技术会带来新的技术问题,我的目标是加快我的工作流程,但不会以不可预测性为代价(这可能会在我负担不起的时候使其变慢)。
我发现 R+knitr+rmarkdown+pander+pandoc solution "with as little overhead as possible", but it seems to be quite heavy still because I don't know any of those technologies apart from R. And I'm not eager to start learning all that, as it seems to be aimed for doing the writing and all in R to the very end, while I want to separate my writing and my code - I never need code in my writing, only the result tables. In addition, based on the examples, it seems to fetch the values directly from R code (e.g., from summary()
to create a descriptive table), while I need to be able to tinker with my table manually before converting it, for instance, writing the title and notes (like a specific note to one cell and explaining it in the bottom). I also found R2wd, but it seems to be an older attempt for the same "whole workflow in R" problem as the solution above. SWord 似乎不再工作了。
有什么建议吗?
简短的回答是 "not really." 我从来没有运气将格式良好的表格导入 MS Word。我可以为您提供的最佳方法需要使用 Rmarkdown 将您的表格呈现到 HTML 文件中。您可以将 HTML 文件中的结果复制并粘贴到 MS Word,但我不保证格式的效果如何。
要格式化表格,您可以尝试使用 xtable
包或 pixiedust
包。但同样,不能保证格式会转移。
我也有同样的需求,最终使用了htmlTable, which is quite 'cost-efficient'. This creates a HTML table (in RStudio it is created in the "Viewer" windows in the bottom right which I just mark using the mouse copy-paste to Word. (Start marking form the bottom of the table and drag the mouse upwards, that way you are sure to include the start of the HTML code.) Word handles these tables quite nicely. The syntax of is quite simple involving just the function htmlTable()
, but is still able to make somewhat more complex tables, such as grouped rows and primary and secondary column headers (i.e. column headers spanning more than one column). Check out the examples in the vignette包。
注意事项:htmlTable 将无法处理因子变量,即,它们将以整数形式出现(根据因子水平)。所以使用 stringsAsFactors = FALSE
读取数据或使用 as.character()
转换它们。
可以使用 txtRound
函数来包含尾随零。示例:
mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)
txt <- txtRound(mini_table, 2)
将格式设置为粗体或斜体并不完全简单,但可以通过将 table 内容包装在 HTML 代码中来完成。例如,如果你想使整个列加粗,可以这样做(请注意 paste0
中单引号和双引号的使用):
library(plyr)
mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)
txt <- txtRound(mini_table, 2)
txt$x <- aaply(txt$x, 1, function(x)
paste0("<span style='font-weight:bold'>", x, "</span")
)
htmlTable(txt)
当然,这在 Word 中会更容易。但是,根据某些标准为数字添加格式会更有趣。例如,如果我们想通过应用bold字体来强调x
所有小于0.2的值,我们可以修改上面的代码如下:
library(plyr)
mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)
txt <- txtRound(mini_table, 2)
txt$x <- aaply(txt$x, 1, function(x)
if (as.numeric(x)<0.2) {
paste0("<span style='font-weight:bold'>", x, "</span>")
} else {
paste0("<span>", x, "</span>")
})
htmlTable(txt)
如果您想要更突出的重点,例如,您可以在上面的代码中使用 span style='background-color: red'
将粗体字体替换为红色背景色。所有这些更改都适用于 Word,至少在我的计算机上 (Windows 7)。
(只是让你知道,我是我推荐你的包的作者......)
您可以使用程序包 ReporteRs
将 table 输出到 Word。在这里查看教程(不是我的):
http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package
对象 FlexTable 让您可以使用一些标准的 R 代码轻松地格式化和排列 table。例如,要将第二列设置为粗体,代码如下所示:
myFlexTable[, 2] = textBold()
这里有(旧的)例子:
http://davidgohel.github.io/ReporteRs/flextable_examples.html
可以使用函数 addFlexTable 将这些对象添加到 Word 报告中。单词报告可以用函数writeDoc
生成。
如果您在 RStudio 中工作,您可以打印对象,它会在 html 查看器中呈现,因此您可以在对其内容满意时将其导出到 Word 中。
您甚至可以添加真正的 Word 脚注(请参阅下面的 link)
http://davidgohel.github.io/ReporteRs/pot_objects.html#pot_footnotes
如果您需要更多表格输出,我还向您推荐处理 xtable
对象的 rtable
包(以及我必须开发以满足我的同事或客户的其他东西)- 快速演示可以在这里看到:
http://davidgohel.github.io/tabular/
希望对您有所帮助...
是否有一种简单的方法可以将 R 数据框自动转换为 APA 格式的漂亮 Word table 以用于出版手稿?我目前通过将 table 保存在 csv 中,在 excel 中打开它,将 excel table 复制到 Word,并在那里格式化,但是我'我希望有一种方法可以在 R 中自动格式化,这样当我将它转换为 Word 时,它已经是 APA 格式了,因为 Word 的自动化很糟糕。
基本上,我想继续在 Word 中编写手稿本身,同时在 R 中进行分析。然后通过脚本将 R 中的所有结果收集到 table(具有可手动修改的格式)并转换将其转换为任何格式,然后我可以简单地复制粘贴到 Word(以便格式实际保留)。当我需要修改 table 时,我会在 R 中进行更改,然后再次 运行 脚本,而无需在 Word 中进行任何更改。
我不想学习 LaTeX,因为我所在领域的每个人都使用具有跟踪更改等功能的 Word,而我使用 Zotero 加载项进行引用,因此将写作与分析分开会更简单。另外,我是一名心理学家,而不是编码员,因此仅仅为此学习大量新技术对我来说可能不值得付出努力。通常新技术会带来新的技术问题,我的目标是加快我的工作流程,但不会以不可预测性为代价(这可能会在我负担不起的时候使其变慢)。
我发现 R+knitr+rmarkdown+pander+pandoc solution "with as little overhead as possible", but it seems to be quite heavy still because I don't know any of those technologies apart from R. And I'm not eager to start learning all that, as it seems to be aimed for doing the writing and all in R to the very end, while I want to separate my writing and my code - I never need code in my writing, only the result tables. In addition, based on the examples, it seems to fetch the values directly from R code (e.g., from summary()
to create a descriptive table), while I need to be able to tinker with my table manually before converting it, for instance, writing the title and notes (like a specific note to one cell and explaining it in the bottom). I also found R2wd, but it seems to be an older attempt for the same "whole workflow in R" problem as the solution above. SWord 似乎不再工作了。
有什么建议吗?
简短的回答是 "not really." 我从来没有运气将格式良好的表格导入 MS Word。我可以为您提供的最佳方法需要使用 Rmarkdown 将您的表格呈现到 HTML 文件中。您可以将 HTML 文件中的结果复制并粘贴到 MS Word,但我不保证格式的效果如何。
要格式化表格,您可以尝试使用 xtable
包或 pixiedust
包。但同样,不能保证格式会转移。
我也有同样的需求,最终使用了htmlTable, which is quite 'cost-efficient'. This creates a HTML table (in RStudio it is created in the "Viewer" windows in the bottom right which I just mark using the mouse copy-paste to Word. (Start marking form the bottom of the table and drag the mouse upwards, that way you are sure to include the start of the HTML code.) Word handles these tables quite nicely. The syntax of is quite simple involving just the function htmlTable()
, but is still able to make somewhat more complex tables, such as grouped rows and primary and secondary column headers (i.e. column headers spanning more than one column). Check out the examples in the vignette包。
注意事项:htmlTable 将无法处理因子变量,即,它们将以整数形式出现(根据因子水平)。所以使用 stringsAsFactors = FALSE
读取数据或使用 as.character()
转换它们。
可以使用 txtRound
函数来包含尾随零。示例:
mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)
txt <- txtRound(mini_table, 2)
将格式设置为粗体或斜体并不完全简单,但可以通过将 table 内容包装在 HTML 代码中来完成。例如,如果你想使整个列加粗,可以这样做(请注意 paste0
中单引号和双引号的使用):
library(plyr)
mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)
txt <- txtRound(mini_table, 2)
txt$x <- aaply(txt$x, 1, function(x)
paste0("<span style='font-weight:bold'>", x, "</span")
)
htmlTable(txt)
当然,这在 Word 中会更容易。但是,根据某些标准为数字添加格式会更有趣。例如,如果我们想通过应用bold字体来强调x
所有小于0.2的值,我们可以修改上面的代码如下:
library(plyr)
mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)
txt <- txtRound(mini_table, 2)
txt$x <- aaply(txt$x, 1, function(x)
if (as.numeric(x)<0.2) {
paste0("<span style='font-weight:bold'>", x, "</span>")
} else {
paste0("<span>", x, "</span>")
})
htmlTable(txt)
如果您想要更突出的重点,例如,您可以在上面的代码中使用 span style='background-color: red'
将粗体字体替换为红色背景色。所有这些更改都适用于 Word,至少在我的计算机上 (Windows 7)。
(只是让你知道,我是我推荐你的包的作者......)
您可以使用程序包 ReporteRs
将 table 输出到 Word。在这里查看教程(不是我的):
http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package
对象 FlexTable 让您可以使用一些标准的 R 代码轻松地格式化和排列 table。例如,要将第二列设置为粗体,代码如下所示:
myFlexTable[, 2] = textBold()
这里有(旧的)例子: http://davidgohel.github.io/ReporteRs/flextable_examples.html
可以使用函数 addFlexTable 将这些对象添加到 Word 报告中。单词报告可以用函数writeDoc
生成。
如果您在 RStudio 中工作,您可以打印对象,它会在 html 查看器中呈现,因此您可以在对其内容满意时将其导出到 Word 中。
您甚至可以添加真正的 Word 脚注(请参阅下面的 link) http://davidgohel.github.io/ReporteRs/pot_objects.html#pot_footnotes
如果您需要更多表格输出,我还向您推荐处理 xtable
对象的 rtable
包(以及我必须开发以满足我的同事或客户的其他东西)- 快速演示可以在这里看到:
http://davidgohel.github.io/tabular/
希望对您有所帮助...