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

后端 未结 10 2034
深忆病人
深忆病人 2020-12-07 08:41

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

相关标签:
10条回答
  • 2020-12-07 08:48

    The RStudio IDE gives another nice option to print out a data.table:

    1. Open the data in the viewer, e.g. View(data_table) or via the GUI
    2. Open the view in a seperate window (icon at the top left corner: "Show in new window")
    3. The seperate window now supports a print dialog (incl. preview)

    This works in RStudio V0.98.1103 (and probably newer versions)

    0 讨论(0)
  • 2020-12-07 08:49

    Not as fancy, but very utilitarian:

    print.data.frame(iris)
    
    0 讨论(0)
  • 2020-12-07 08:54

    If you want to export as a png, you can do like this:

    library(gridExtra)
    png("test.png", height = 50*nrow(df), width = 200*ncol(df))
    grid.table(df)
    dev.off()
    

    If you want to export as a pdf, you can do like this:

    library(gridExtra)
    pdf("test.pdf", height=11, width=10)
    grid.table(df)
    dev.off()
    
    0 讨论(0)
  • 2020-12-07 08:59

    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.

    0 讨论(0)
  • 2020-12-07 09:02

    The printr package is a good option for printing data.frames, help pages, vignette listings, and dataset listings in knitr documents.

    From the documentation page:

    options(digits = 4)
    set.seed(123)
    x = matrix(rnorm(40), 5)
    dimnames(x) = list(NULL, head(LETTERS, ncol(x)))
    knitr::kable(x, digits = 2, caption = "A table produced by printr.")
    
    0 讨论(0)
  • 2020-12-07 09:02

    For long/wide tables you could use pander.

    It will automatically split long tables into shorter parts that fit the page, e.g. using knitr insert this chunk into your Rmd file:

    pander::pander(mtcars)
    

    If you want something that looks more like Excel tables (even with editing options in html) then use rhandsontable. More info about usage and formatting in the vignette. You will need to knit your Rmd into an html file:

    library(rhandsontable)
    rhandsontable(mtcars, rowHeaders = NULL)
    

    0 讨论(0)
提交回复
热议问题