如何打印(以纸张)格式很好的数据框架 [英] How to print (to paper) a nicely-formatted data frame

查看:186
本文介绍了如何打印(以纸张)格式很好的数据框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本格式的数据帧打印到纸张上,理想情况下是从脚本中打印出来。 (我正在尝试使用仪器收集数据,并使用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(),但这有两个问题:
1.)生成的文本文件格式不正确(列不一定符合标题)和
2.)我不知道如何从R中打印文本文件。

Right now I can write a data frame to a text file using write.table(), but this has two problems: 1.) the resulting text file is poorly formatted (columns do not necessarily line up with their headings) and 2.) I don't know how to print a text file from within 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.

这篇关于如何打印(以纸张)格式很好的数据框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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