Search code examples
rflextablereportersofficer

R ReporteRs: Adding a regulartable object to a pptx presentation


I'm having trouble adding a flextable to a PowerPoint presentation with the ReporteRs package, since the column names I want to display are not syntactically valid.

So, apparently flextable only accepts data frames with syntactically valid column names, which means in particular that the names can't be empty strings nor contain spaces. My first solution was to simply use the regulartable function, which works nicely. But when I then want to add my regulartable object to my PowerPoint presentation, it tells me that I can only add flextable objects (...and the circularity begins...).

Minimal example:

x <- c('a', 'b')
y <- c(1, 2)
d <- data.frame(x, y)
names(d) <- c(" ", "this column") #not syntactically valid column names!

#trying to call flextable(d) leads to an error
#so, try to use regulartable:
table <- regulartable(x)

#now try to create a pptx with this:
mySlides <- pptx()
mySlides <- addSlide(mySlides, 'Blank')
mySlides <- addFlexTable(mySlides, flextable = test) #Error: argument flextable must be a FlexTable object.

Does anyone know how to go around this issue?

I would be happy if I either knew how to add a regurtable to my slides, or how to make flextable take column names that are empty or that contain spaces.

Thanks in advance for any help!


Solution

  • flextable is designed to work with officer not with ReporteRs. Note that package ReporteRs will be removed from CRAN the 16th of July 2018 (due to incompatibility with java >=9), package officer is replacing ReporteRs.

    Your code should be something like:

    library(flextable)
    library(officer)
    
    x <- c('a', 'b')
    y <- c(1, 2)
    d <- data.frame(x, y)
    names(d) <- c(" ", "this column") #not syntactically valid column names!
    table <- flextable(d)
    
    ppt <- read_pptx()
    ppt <- add_slide(ppt, layout = "Title and Content", master = "Office Theme")
    ppt <- ph_with_flextable(ppt, value = table, type = "body") 
    
    print(ppt, target = "example.pptx")
    

    About the column names. You don't have to change names of your data.frame only to display them. You can use different functions to do that:

    library(flextable)
    library(officer)
    
    x <- c('a', 'b')
    y <- c(1, 2)
    d <- data.frame(x, y)
    
    meta <- data.frame(
      col_keys = c("x", "y"), 
      upper_labels = c("hi", "world"), 
      labels = c(" ", "this column"), 
      stringsAsFactors = FALSE)
    
    table <- flextable(d)
    table <- set_header_df( table, mapping = meta )
    
    d <- data.frame(a = x, b = y)# must not contain x as set_header_labels first arg is also x...
    ft <- flextable(d)
    ft <- set_header_labels( ft, a = "", b = "this column" )
    
    ppt <- read_pptx()
    ppt <- add_slide(ppt, layout = "Two Content", master = "Office Theme")
    ppt <- ph_with_flextable(ppt, value = table, type = "body", index = 1) 
    ppt <- ph_with_flextable(ppt, value = ft, type = "body", index = 2) 
    
    print(ppt, target = "example.pptx")