-1

I want to use xtable layouts in my powerpoint presentations and I need a way to directly convert a data.frame into a picture or plot of an xtable such as the ones displayed here.

The ideal solution would give me a ggplot object as I have the most flexibility from there, but I can work with another output as long as I can see a xtable in a pic (raster or vector) or plot window.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • Related / Possible duplicate? https://stackoverflow.com/q/9298765/680068 – zx8754 Feb 21 '18 at 10:48
  • Or this: https://stackoverflow.com/a/12318578/680068 – zx8754 Feb 21 '18 at 10:53
  • Great thanks, please don't mark as duplicate, I'll compile the relevant info and will post an easy solution here. – moodymudskipper Feb 21 '18 at 11:10
  • 1
    Just pointing to relevant posts, not closing. If you think this is a dupe, then please close and post your answer on the target. – zx8754 Feb 21 '18 at 11:16
  • Actually first link require at least `yap` and `dvipng` that I cannot install. Second solution uses `ggplot2` and `gridExtra` and can be summarized as `ggplot() + annotation_custom(tableGrob(head(iris))) + theme_void()` and `tableGrob` has a `theme` parameter that allows enough flexibility to reproduce something close to `xtable`. See this link https://cran.r-project.org/web/packages/gridExtra/vignettes/tableGrob.html . – moodymudskipper Feb 21 '18 at 13:36
  • Why as an image? If you do want as a real table in pptx, you can use flextable package: https://davidgohel.github.io/flextable/articles/examples.html#xtable-objects – David Gohel Feb 24 '18 at 09:29
  • I'll take a look as it seems very interesting! But `officer` creates or edits the powerpoint code, it doesn't edit an open presentation on the fly. I've built a package that does that (FYI, though not intended for public consumption: https://github.com/moodymudskipper/ppt) and I intend to copy my tables at specific positions using this package, though pasting powerpoint tables looking just the same would be amazing. – moodymudskipper Feb 24 '18 at 10:20
  • 1
    OK, you could have a look at package R2PPT. Use webshot to get a screen shot of xtable and then use R2PPT to add the content in your open pptx file. – David Gohel Feb 26 '18 at 08:45
  • great thanks, I didn't know `webshot` and was thinking of developing my own – moodymudskipper Feb 26 '18 at 09:43

1 Answers1

0

Using ggplot2 and grid.extra we can achieve decently looking tables:

library(ggplot2)
library(gridExtra)
ggplot() + annotation_custom(tableGrob(head(iris))) + theme_void()

tableGrob has a theme parameter that allows enough flexibility to reproduce something close to xtable. see this vignette.

Here's a convenient function to do a bit more, you'll need packages grid and gtable :

table_plot <- function(x,
                       theme_fun= gridExtra::ttheme_minimal,
                       base_size = 12,
                       base_colour = "black",
                       base_family = "",
                       parse = FALSE,
                       padding = unit(c(4, 4), "mm"),
                       col = base_colour,
                       lwd=1,
                       lty=1
                       ){
  g <- gridExtra::tableGrob(x,
    theme = theme_fun(base_size, base_colour, base_family, parse, padding))
  separators <- replicate(ncol(g) - 2,
                          grid::segmentsGrob(x1 = unit(0, "npc"), gp=gpar(col=col,lwd=lwd,lty=lty)),
                          simplify=FALSE)
  g <- gtable::gtable_add_grob(g, grobs = separators,
                               t = 2, b = nrow(g), l = seq_len(ncol(g)-2)+2)
  ggplot2::ggplot() + ggplot2::annotation_custom(g) + ggplot2::theme_void()
}

simple example:

table_plot(head(iris))

complicated example :

iris2 <- setNames(head(iris)[1:3],c("alpha*integral(xdx,a,infinity)", "this text\nis high", 'alpha/beta'))
table_plot(iris2,
           base_size=10,
           base_colour="darkred",
           base_family = "serif",
           parse=TRUE,
           theme=ttheme_default,
           lwd=3,
           lty=2,
           col="darkblue")
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167