问题描述
我想将格式良好的数据帧打印到纸上,最好是从脚本中打印出来.(我正在尝试使用仪器收集数据并使用 R 脚本自动处理和打印).
I'd like to print nicely-formatted data frames to paper, ideally from within a script. (I am trying to collect data using an instrument and automatically process and print it using an R script).
现在我可以使用 write.table()
将数据框写入文本文件,但这有两个问题:
Right now I can write a data frame to a text file using write.table()
, but this has two problems:
- 生成的文本文件格式不正确(列不一定与其标题对齐)并且
- 我不知道如何从 R 中打印文本文件.
我更多的是寻找通用策略而不是特定代码(尽管代码也很棒!).Sweave 会是最方便的解决方案吗?原则上我可以使用 socketConnection()
打印到打印机 - 如果可以,我在哪里可以了解如何使用它(我没有发现文档很有帮助).
I'm looking more for general strategies than for specific code (although code would be great too!). Would Sweave be the most convenient solution? In principle can I use socketConnection()
to print to a printer - and if so, where can I learn about how to use it (I didn't find the documentation to be very helpful).
推荐答案
下面是使用 gridExtra 包中的 grid.table
的一种快速简便的方法:
Here is a quick and easy possibility using grid.table
from the gridExtra package:
library(gridExtra)
pdf("data_output.pdf", height=11, width=8.5)
grid.table(mtcars)
dev.off()
如果您的数据不适合页面,您可以减小文本大小grid.table(mtcars, gp=gpar(fontsize=8))
.这可能不是很灵活,也不容易泛化或自动化.
If your data doesn't fit on the page, you can reduce the text size grid.table(mtcars, gp=gpar(fontsize=8))
. This may not be very flexible, nor easy to generalize or automate.
这篇关于如何打印(到纸上)格式良好的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!