dbosak01 / reporter

An R package to write statistical reports.
Creative Commons Zero v1.0 Universal
16 stars 3 forks source link

multiple figures issue in docx output #294

Closed kevin199011 closed 1 year ago

kevin199011 commented 1 year ago

Hi, I was trying to insert multiple figures to a docx output, however I noticed that the first figure is inserted again and again, instead of inserting the correct figure. I was using reporter version 1.3.5 in R 4.0.5. Output to pdf, rtf has no such issue.

library(reporter)
library(ggplot2)
library(magrittr)

# Create temporary path
tmp <- file.path(tempdir(), "test.docx")

# Prepare data
dat <- mtcars[order(mtcars$cyl), ]

# Generate plot1
p1 <- ggplot(dat, aes(x=disp, y=mpg)) + geom_point()

# Define plot object1
plt1 <- create_plot(p1, height = 4, width = 8)

# Add plot1 to report
rpt <- create_report(tmp, output_type = "DOCX") %>%
  set_margins(top = 1, bottom = 1) %>%
  options_fixed(font_size = 12) %>% 
  page_header("Sponsor", "Study: cars") %>%
  add_content(plt1) %>%
  page_footer(Sys.time(), "Confidential", "Page [pg] of [tpg]")

# Generate plot2
p2 <- ggplot(dat, aes(x=wt, y=qsec)) + geom_point()

# Define plot object2
plt2 <- create_plot(p2, height = 4, width = 8)

# Add plot2 to report
rpt <- add_content(rpt, plt2)

# Write out report
res <- write_report(rpt)

sessionInfo()
#> R version 4.0.5 (2021-03-31)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> system code page: 936
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] magrittr_2.0.3 ggplot2_3.3.6  reporter_1.3.5
#> 
#> loaded via a namespace (and not attached):
#>  [1] styler_1.7.0      common_1.0.3      tidyselect_1.1.1  xfun_0.23        
#>  [5] purrr_0.3.4       colorspace_2.0-1  vctrs_0.4.1       generics_0.1.0   
#>  [9] htmltools_0.5.2   yaml_2.2.1        utf8_1.2.1        rlang_1.0.2      
#> [13] R.oo_1.24.0       pillar_1.7.0      glue_1.6.2        withr_2.4.2      
#> [17] DBI_1.1.1         R.utils_2.11.0    logr_1.3.3        jpeg_0.1-8.1     
#> [21] R.cache_0.15.0    lifecycle_1.0.1   stringr_1.4.0     munsell_0.5.0    
#> [25] gtable_0.3.0      ragg_1.2.2        R.methodsS3_1.8.1 zip_2.1.1        
#> [29] evaluate_0.14     labeling_0.4.2    knitr_1.33        fastmap_1.1.0    
#> [33] fansi_0.4.2       highr_0.9         Rcpp_1.0.6        scales_1.2.0     
#> [37] this.path_0.11.0  farver_2.1.0      fs_1.5.2          systemfonts_1.0.4
#> [41] textshaping_0.3.6 digest_0.6.27     stringi_1.6.2     dplyr_1.0.8      
#> [45] grid_4.0.5        fmtr_1.5.9        cli_3.4.0         tools_4.0.5      
#> [49] tibble_3.1.6      crayon_1.4.1      pkgconfig_2.0.3   ellipsis_0.3.2   
#> [53] reprex_2.0.0      assertthat_0.2.1  rmarkdown_2.8     rstudioapi_0.13  
#> [57] R6_2.5.0          compiler_4.0.5

Created on 2023-03-17 by the reprex package (v2.0.0)

image

dbosak01 commented 1 year ago

You're right. Let me investigate.

dbosak01 commented 1 year ago

Thanks for identifying this issue. It was a very simple fix: 2 lines. I pushed to GitHub and you can pull down from there. It will be another month until I can post to CRAN.