【问题标题】:Dynamic Reporting in RR中的动态报告
【发布时间】:2014-11-11 07:37:33
【问题描述】:

我正在寻求帮助以从 R(数据框)生成“rtf”报告。

我正在尝试使用以下代码将包含多列的数据输出到“rtf”文件中

library(rtf)
inp.data <- cbind(ChickWeight,ChickWeight,ChickWeight)

outputFileName = "test.out"
rtf<-RTF(paste(".../",outputFileName,".rtf"), width=11,height=8.5,font.size=10,omi=c(.5,.5,.5,.5))
addTable(rtf,inp.data,row.names=F,NA.string="-",col.widths=rep(1,12),header.col.justify=rep("C",12))
done(rtf)

我面临的问题是,一些列正在隐藏(如您所见,最后 2 列正在隐藏)。我希望这些列在下一页打印(不减少列宽)。

任何人都可以为这种情况推荐包/技术吗?

谢谢

【问题讨论】:

  • rmarkdown,输出可以保存为Word、PDF、HTML。

标签: r dynamic reporting rtf


【解决方案1】:

六年后,终于有一个包可以完全满足您的需求。它被称为reporter(小“r”,没有“s”)。如果超过可用的内容宽度,它会将列换行到下一页。

library(reporter)
library(magrittr)

# Prepare sample data
inp.data <- cbind(ChickWeight,ChickWeight,ChickWeight)

# Make unique column names
nm <- c("weight", "Time", "Chick", "Diet")
nms <- paste0(nm, c(rep(1, 4), rep(2, 4), rep(3, 4)))
names(inp.data) <- nms

# Create table
tbl <- create_table(inp.data) %>% 
  column_defaults(width = 1, align = "center") 

# Create report and add table to report
rpt <- create_report("test.rtf", output_type = "RTF", missing = "-") %>% 
  set_margins(left = .5, right = .5) %>% 
  add_content(tbl)

# Write the report
write_report(rpt)

唯一的问题是您需要唯一的列名。所以我添加了一些代码来做到这一点。

【讨论】:

    【解决方案2】:

    如果docx格式可以替换rtf格式,使用包ReporteRs

    library( ReporteRs )
    
    inp.data <- cbind(ChickWeight,ChickWeight,ChickWeight)
    doc = docx( )
    
    # uncomment addSection blocks if you want to change page
    # orientation to landscape
    # doc = addSection(doc, landscape = TRUE ) 
    
    doc = addFlexTable( doc, vanilla.table( inp.data ) )
    
    # doc = addSection(doc, landscape = FALSE ) 
    
    writeDoc( doc, file = "inp.data.docx" )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多