【问题标题】:Is it possible to export a large R table into a PDF file?是否可以将大型 R 表导出为 PDF 文件?
【发布时间】:2021-08-26 00:39:05
【问题描述】:

我需要通过绑定不同的值导出我在 r 中创建的表 enter image description here

我尝试过使用library(gridExtra),但它不起作用(也许我用错了)

FA=table(ant$municipionombre)
View(FA)
prop.table(table(ant$municipionombre))


#Percentage Relative Frequency/Frecuencia relativa porcentual
FR=FA/margin.table(FA)
View(FR)


#Cumulative Absolute Frequency/Frecuencia absoluta acumulada
FAA=cumsum(FA)
View(FAA)

#Cumulative Percentage Relative Frequency/Frecuencia relativa porcentual acumulada
FRP=cumsum(FA)/margin.table(FA)
View(FRP)

#Percentage by entry
perc=FR*100
perc

#Build a table with all variables (FA, FAA, FR, FRP, perc)
tab1=cbind(FA,FAA,FR,FRP,perc)
tab1
View(tab1)```

【问题讨论】:

  • 您是否考虑过rmarkdown 用于将表格导出为pdf 的包和kableExtra 用于表格样式?

标签: r pdf export-to-pdf


【解决方案1】:

我不太了解您上面的代码,因为您没有提供可用的示例数据。但是如何将数据导出为 PDF 的基本问题可以通过报告程序包完成。

分为三个步骤:

  1. 创建表格内容。
  2. 创建报告。
  3. 写出报告。

这是一个使用 iris 数据框的示例。


library(reporter)


tbl <- create_table(iris) %>% 
  titles("My PDF Report")

rpt <- create_report("c:\\temp\\test.pdf", output_type = "PDF") %>% 
  add_content(tbl)


write_report(rpt)

这是 PDF 的一部分:

【讨论】:

    【解决方案2】:

    使用 gridExtra 包,你可以考虑下面的代码

        maxrow = 35;
        npages = ceiling(nrow(df)/maxrow)
        pdf("test.pdf", height = 11, width = 8.5)  
        idx = seq(1, maxrow)        
        grid.table(df[idx,], rows = NULL)
        for (i in 2:npages){
          grid.newpage()
          if(i * maxrow <= nrow(df)) {
            idx = seq(1 + ((1 - 1) * maxrow), i*maxrow)
          } else {
            idx = seq(1 + ((i - 1) * naxriw), nrow(df))
          }
          grid.table(df[idx,], rows = NULL)
        }
        dev.off()
    

    您可以更改高度或宽度,因此我强烈建议您尝试使用单页制作 pdf。 df也是写pdf的表。

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2014-09-07
      相关资源
      最近更新 更多