Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

r - How to print (to paper) a nicely-formatted data frame

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...