davidgohel / rvg

https://davidgohel.github.io/rvg/
132 stars 15 forks source link

Shapes generated by ph_with_vg are counted in slide_summary #20

Closed benoitrauturier closed 1 year ago

benoitrauturier commented 6 years ago

Hi David,

Not sure that one is a rvg or a officer issue. Apprently the shapes generated by ph_with_vg are counted while computing the id of each shape in a particular slide. The consequence for that is that when generating multiple reports, the id_chr argument is unpredictable from one version to another.

The reproducible example below describes the problem.

library(tidyverse)
library(officer)
library(rvg)

graph_generating_function = function(i){

gg <- mtcars %>% filter(cyl == i) %>% 
  ggplot(aes(x = disp, y = drat)) + 
  geom_point()+
  theme_bw()

pptx <- read_pptx() %>% 
  add_slide(layout = "Two Content", master = "Office Theme") %>% 
  ph_with_vg(ggobj = gg, type = "body", index = 1) %>% 
  ph_with_text("Some text", type = "body", index = 2) %>% 
  ph_add_text(" some following text", type = "body", id_chr = "56")

pptx %>% print(target = paste0("cyl_", i, ".pptx"))
}

c(8,4,6) %>% map(graph_generating_function)

Here the loop works for i = 8 but for the other values it will throw the error "Error: selection does not match any row in slide_summary. Use function slide_summary.".

A workaround here is to rewrite the slide generating part but this limits the possibility to control perfectly which placeholder must be at the top and which one must be a the bottom.

pptx <- read_pptx() %>% 
  add_slide(layout = "Two Content", master = "Office Theme") %>% 
  ph_with_text("Some text", type = "body", index = 2) %>% 
  ph_add_text(" some following text", type = "body", id_chr = "1") %>% 
  ph_with_vg(ggobj = gg, type = "body", index = 1)  

Here are my session info :

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] gdtools_0.1.6     bindrcpp_0.2      rvg_0.1.7         officer_0.2.2.002 forcats_0.2.0     stringr_1.2.0     dplyr_0.7.4      
 [8] purrr_0.2.4       readr_1.1.1       tidyr_0.8.0       tibble_1.4.2      ggplot2_2.2.1     tidyverse_1.2.1  

loaded via a namespace (and not attached):
 [1] reshape2_1.4.3    haven_1.1.1       lattice_0.20-35   colorspace_1.3-2  htmltools_0.3.6   yaml_2.1.16       base64enc_0.1-3  
 [8] rlang_0.1.6       R.oo_1.21.0       pillar_1.1.0      foreign_0.8-69    glue_1.2.0        R.utils_2.6.0     modelr_0.1.1     
[15] readxl_1.0.0      uuid_0.1-2        bindr_0.1         plyr_1.8.4        munsell_0.4.3     gtable_0.2.0      cellranger_1.1.0 
[22] rvest_0.3.2       R.methodsS3_1.7.1 zip_1.0.0         psych_1.7.8       labeling_0.3      parallel_3.4.2    broom_0.4.3      
[29] Rcpp_0.12.15      scales_0.5.0      jsonlite_1.5      mnormt_1.5-5      hms_0.4.1         digest_0.6.15     stringi_1.1.6    
[36] grid_3.4.2        cli_1.0.0         tools_3.4.2       magrittr_1.5      lazyeval_0.2.1    crayon_1.3.4      pkgconfig_2.0.1  
[43] xml2_1.2.0        lubridate_1.7.1   assertthat_0.2.0  httr_1.3.1        rstudioapi_0.7    R6_2.2.2          nlme_3.1-131     
[50] compiler_3.4.2   
davidgohel commented 6 years ago

Hi Benoît,

:) Well, I can admit it's not obvious where to open that issue.

It seems @marleendv ' PR is the solution as you could use id that you can control: https://github.com/davidgohel/officer/pull/91

You could reuse her code/ideas until it's integrated in officer (I don't have time for now but will give a try later).

davidgohel commented 1 year ago

Closing that issue as I think it's being solved in officer and also ph_with_vg() has been replaced by ph_with()