0

I have a datatable such as this one :

library(stringi)
library(tidyverse)
library(data.table)  

MWE <- 
  data.table("Zone"=stri_rand_strings(9, 10, pattern = "[A-Za-z0-9]")) %>%
  .[,paste0("Var",2:11):= round(rnorm(9,2,4),1)]

From that I want to output a nice table. I have managed to do something satisfying through the gt package, but I am opn to switch if necessary

library(gt)
MWE_Table <-  gt(MWE)%>%
  tab_spanner(label="PIB", columns = c(2:7))%>%
  tab_spanner(label="IPC", columns = c(8:11))%>%
  tab_style(
    style = list(
      cell_text(weight = "lighter", style="italic")),
    locations = cells_body(columns = c("Var3","Var5","Var7","Var9","Var11"))) %>% 
  tab_style(
    style = list(
      cell_text(weight = "normal", style="italic")),
    locations = cells_body(columns = c("Var3","Var5","Var7","Var9","Var11"),rows = (Zone == "France"))) %>%   
  cols_label(Zone="",
             Var2=Annee1, Var3="(rev)",
             Var4=Annee2, Var5="(rev)",
             Var6="Cumul", Var7="(rev)",
             Var8=paste0("   ",Annee1), Var9="(rev)",
             Var10=Annee2, Var11="(rev)")%>%
  cols_align(., align = c("center"), columns = c(2:11)) %>%
  tab_header(
    title = md(paste("Title ")),
    subtitle = md("Subtitle")
  )
              

Which gives me the following table : enter image description here

I would like to export is as a vector graphic, but the metafile option that I usually use is not listed as an option :

enter image description here

  • Can I do something to add this option ?
  • If it is that gt does not support it, which alternative to produce tables exists that allows to export as a metafile ?
Anthony Martin
  • 767
  • 1
  • 9
  • 28
  • Looks like you will not be able to do with solely using R, but if you have microsoft word, you can export your image to Word, then export from word to a metafile... Have a look at [this answer](https://stackoverflow.com/questions/9555889/producing-a-vector-graphics-image-i-e-metafile-in-r-suitable-for-printing-in) and go down to `Tom Wenseleers` answer, looks like he created an *export* package that can be used, please see his code and answer, let me know if you have questions I will try to help – Daniel_j_iii Oct 26 '20 at 09:59
  • @DanielJachetta it does not seem to be available for my R version (4.0.2). And by looking a bit more in depth, it seems to have been deleted ? https://cran.r-project.org/web/packages/export/index.html – Anthony Martin Oct 26 '20 at 10:14
  • It's available on the github `devtools::install_github("tomwenseleers/export")`, github is [here](https://github.com/tomwenseleers/export) – Daniel_j_iii Oct 26 '20 at 10:16
  • Thx, it is now installed, but does not seem to recognize the gt as a something that can be exported : I get `Error in captureplot() : no device to print from ` – Anthony Martin Oct 26 '20 at 10:45
  • `dev.print(file="test.png", device=png, width=800)` what if you save your gt table as a PNG, and then use that png as if it was a plot – Daniel_j_iii Oct 26 '20 at 10:49
  • `dev.print` gives me the same error. But I can export a gt as a png, that is a given. `MWE_Table %>% gtsave("CF_Ecart.png",path="") ` – Anthony Martin Oct 26 '20 at 11:07
  • Looks like my link uses a qplot/ggplot object, maybe you can use ggplot to “plot” your png so it’s a ggplot imagine, then use my link to export it? – Daniel_j_iii Oct 26 '20 at 11:50

0 Answers0