davidgohel / officer

:cop: officer: office documents from R
https://ardata-fr.github.io/officeverse/
Other
606 stars 107 forks source link

While adding plot to pptx, misleading error message occurs if code generates output #485

Closed GegznaV closed 2 months ago

GegznaV commented 1 year ago

Issue

While trying to add a plot to pptx slide, if the code provided via argument code (in dml() that is evaluated in ph_with()) generates output (e.g., print(10)) or plot + output (see the example with DescToolsDesc()), ph_with() generates uninformative/misleading error message invalid element name [68] while the true issue is that the code generated output while only a plot was expected. And the place where the error occurs is shown as read_xml.raw(... while the true place is the code provided via dml() and evaluated in with_ph().

Example output of the function: plot + output ``` r # Example output: plot + printed output DescTools::DescToolsOptions(stamp = NULL) DescTools::Desc(~Species, data = iris) #> ------------------------------------------------------------------------------ #> iris$Species (factor) #> #> length n NAs unique levels dupes #> 150 150 0 3 3 y #> 100.0% 0.0% #> #> level freq perc cumfreq cumperc #> 1 setosa 50 33.3% 50 33.3% #> 2 versicolor 50 33.3% 100 66.7% #> 3 virginica 50 33.3% 150 100.0% ``` ![](https://i.imgur.com/nybVhjU.png)

# officer gave uninformative/misleading error message
library(rvg)
library(officer)

dml_plot <- dml(bg = NULL, code = DescTools::Desc(~Species, data = iris))

read_pptx() |>
  add_slide(layout = "Blank") |>
  ph_with(dml_plot, location = ph_location_fullsize())
#> Error in read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, : StartTag: invalid element name [68]

Created on 2023-03-29 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.3 (2023-03-15 ucrt) #> os Windows 10 x64 (build 19045) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.utf8 #> ctype English_United States.utf8 #> tz Europe/Helsinki #> date 2023-03-29 #> pandoc 2.19.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> askpass 1.1 2019-01-13 [1] CRAN (R 4.2.0) #> boot 1.3-28.1 2022-11-22 [1] CRAN (R 4.2.2) #> cachem 1.0.7 2023-02-24 [1] CRAN (R 4.2.2) #> cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.2.0) #> class 7.3-21 2023-01-23 [2] CRAN (R 4.2.3) #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.2.3) #> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.1) #> crul 1.3 2022-09-03 [1] CRAN (R 4.2.1) #> curl 5.0.0 2023-01-12 [1] CRAN (R 4.2.2) #> data.table 1.14.8 2023-02-17 [1] CRAN (R 4.2.2) #> DescTools 0.99.48.1 2023-02-22 [1] local #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.2) #> e1071 1.7-13 2023-02-01 [1] CRAN (R 4.2.2) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.20 2023-01-17 [1] CRAN (R 4.2.2) #> Exact 3.2 2022-09-25 [1] CRAN (R 4.2.1) #> expm 0.999-7 2023-01-09 [1] CRAN (R 4.2.2) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.2) #> fontBitstreamVera 0.1.1 2017-02-01 [1] CRAN (R 4.2.0) #> fontLiberation 0.1.0 2016-10-15 [1] CRAN (R 4.2.0) #> fontquiver 0.2.1 2017-02-01 [1] CRAN (R 4.2.2) #> fs 1.6.1 2023-02-06 [1] CRAN (R 4.2.2) #> gdtools * 0.3.2 2023-03-13 [1] CRAN (R 4.2.2) #> gfonts 0.2.0 2023-01-08 [1] CRAN (R 4.2.2) #> gld 2.6.6 2022-10-23 [1] CRAN (R 4.2.1) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> highr 0.10 2022-12-22 [1] CRAN (R 4.2.2) #> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.2.3) #> httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.2.1) #> httpuv 1.6.9 2023-02-14 [1] CRAN (R 4.2.2) #> httr 1.4.5 2023-02-24 [1] CRAN (R 4.2.2) #> jsonlite 1.8.4 2022-12-06 [1] CRAN (R 4.2.2) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.2.2) #> later 1.3.0 2021-08-18 [1] CRAN (R 4.2.0) #> lattice 0.20-45 2021-09-22 [2] CRAN (R 4.2.3) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.1) #> lmom 2.9 2022-05-29 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> MASS 7.3-58.3 2023-03-07 [1] CRAN (R 4.2.2) #> Matrix 1.5-3 2022-11-11 [1] CRAN (R 4.2.2) #> memoise 2.0.1 2021-11-26 [1] CRAN (R 4.2.0) #> mime 0.12 2021-09-28 [1] CRAN (R 4.2.0) #> mvtnorm 1.1-3 2021-10-08 [1] CRAN (R 4.2.0) #> officer * 0.6.2 2023-03-28 [1] CRAN (R 4.2.3) #> openssl 2.0.6 2023-03-09 [1] CRAN (R 4.2.2) #> promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.2.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.2.0) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.2) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.2) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> ragg 1.2.5 2023-01-12 [1] CRAN (R 4.2.2) #> Rcpp 1.0.10 2023-01-22 [1] CRAN (R 4.2.2) #> readxl 1.4.2 2023-02-09 [1] CRAN (R 4.2.2) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.1) #> rlang 1.1.0 2023-03-14 [1] CRAN (R 4.2.2) #> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.2.2) #> rootSolve 1.8.2.3 2021-09-29 [1] CRAN (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.1) #> rvg * 0.3.2 2023-01-07 [1] CRAN (R 4.2.2) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> shiny 1.7.4 2022-12-15 [1] CRAN (R 4.2.2) #> styler 1.9.1 2023-03-04 [1] CRAN (R 4.2.2) #> systemfonts 1.0.4 2022-02-11 [1] CRAN (R 4.2.0) #> textshaping 0.3.6 2021-10-13 [1] CRAN (R 4.2.0) #> uuid 1.1-0 2022-04-19 [1] CRAN (R 4.2.0) #> vctrs 0.6.1 2023-03-22 [1] CRAN (R 4.2.3) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.38 2023-03-24 [1] CRAN (R 4.2.3) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.0) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.2.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.2) #> zip 2.2.2 2022-10-26 [1] CRAN (R 4.2.1) #> #> [1] C:/Users/ViG/AppData/Local/R/win-library/4.2 #> [2] C:/Program Files/R/R-4.2.3/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

Expected result

Either the function should fail with an error message that indicates the essence of the issue and the correct place where it occurred. E.g., Error in code provided in dml(): Code [...] generated output [...] while only a plot was expected.

On the other hand, capture.output() or similar can be applied to suppress the output and use only the plot generated by the code that produces both the plot and the output.

GegznaV commented 1 year ago

As I understand, this issue is present in rvg:::ph_with.dml() so maybe instead of

eval_tidy(value$code)

using

nothing = capture.output(eval_tidy(value$code))

would work?

GegznaV commented 1 year ago

Otherwise, the issue is caused by xml2::as_xml_document(dml_str).

davidgohel commented 2 months ago

closing as fixed in rvg and sent to cran