Search code examples
rdynamicreportingrtf

Dynamic Reporting in R


I am looking for a help to generate a 'rtf' report from R (dataframe).

I am trying output data with many columns into a 'rtf' file using following code

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)

The problem I face is, some of the columns are getting hide (as you can see last 2 columns are getting hide). I am expecting these columns to print in next page (without reducing column width).

Can anyone suggest packages/techniques for this scenario?

Thanks


Solution

  • Six years later, there is finally a package that can do exactly what you wanted. It is called reporter (small "r", no "s"). It will wrap columns to the next page if they exceed the available content width.

    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)
    

    Only thing is you need unique columns names. So I added a bit of code to do that.