yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

`version >= 1.41` breaks natural html `flextable` to `webshot` image integration in GitHub/md document #2265

Closed rempsyc closed 1 year ago

rempsyc commented 1 year ago

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.


knitr version 1.41 and above now breaks the natural html flextable to webshot image integration for GitHub/md documents. Reprex:

knitr 1.40 (works)

---
output: github_document
---

```{r}
# remotes::install_version("knitr", version = "1.40", repos = "http://cran.us.r-project.org")
packageVersion("knitr")
## [1] '1.40'
flextable::flextable(head(iris))
![table](https://github.com/yihui/knitr/assets/13123390/906875d4-0405-4489-b2a4-c516d4394b41)

```` r
xfun::session_info()

## R version 4.2.2 (2022-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19045)
## 
## Locale:
##   LC_COLLATE=English_Canada.utf8  LC_CTYPE=English_Canada.utf8   
##   LC_MONETARY=English_Canada.utf8 LC_NUMERIC=C                   
##   LC_TIME=English_Canada.utf8    
## 
## Package version:
##   askpass_1.1             base64enc_0.1.3         bslib_0.5.0            
##   cachem_1.0.8            cli_3.6.1               commonmark_1.9.0       
##   compiler_4.2.2          cpp11_0.4.3             crayon_1.5.2           
##   crul_1.4.0              curl_5.0.1              data.table_1.14.8      
##   digest_0.6.31           ellipsis_0.3.2          evaluate_0.21          
##   fastmap_1.1.1           flextable_0.9.2.002     fontawesome_0.5.1      
##   fontBitstreamVera_0.1.1 fontLiberation_0.1.0    fontquiver_0.2.1       
##   fs_1.6.2                gdtools_0.3.3           gfonts_0.2.0           
##   glue_1.6.2              graphics_4.2.2          grDevices_4.2.2        
##   grid_4.2.2              highr_0.10              htmltools_0.5.5        
##   httpcode_0.3.0          httpuv_1.6.11           jquerylib_0.1.4        
##   jsonlite_1.8.5          knitr_1.40              later_1.3.1            
##   lifecycle_1.0.3         magrittr_2.0.3          memoise_2.0.1          
##   methods_4.2.2           mime_0.12               officer_0.6.2          
##   openssl_2.0.6           promises_1.2.0.1        R6_2.5.1               
##   ragg_1.2.5              rappdirs_0.3.3          Rcpp_1.0.10            
##   rlang_1.1.1             rmarkdown_2.22          rstudioapi_0.14        
##   sass_0.4.6              shiny_1.7.4             sourcetools_0.1.7.1    
##   stats_4.2.2             stringi_1.7.12          stringr_1.5.0          
##   sys_3.4.2               systemfonts_1.0.4       textshaping_0.3.6      
##   tinytex_0.45            tools_4.2.2             triebeard_0.4.1        
##   urltools_1.7.3          utils_4.2.2             uuid_1.1-0             
##   vctrs_0.6.3             withr_2.5.0             xfun_0.39              
##   xml2_1.3.4              xtable_1.8-4            yaml_2.3.7             
##   zip_2.3.0

Note that while writing this I had to change from R 4.3.1 to 4.2.2 otherwise I am getting a different error:
``` r
Error in agg png(filename = path, width = dims$width + expand/72, height
Graphics APT version mismatch
calls: <anonymous> ... knit_print.flextable -> in base_dir -> in_dir -> save as_image -> agg_png
In addition: warning message:
In do_once((if (is_R_cMD_check()) stop else warning) ("The function xfun::isFALSEQ) will be deprecated in the future. Please ", :
The function xfun::isFALSEQ) will be deprecated in the future. Please consider using base: :isFALSE(x) or identical(x, FALSE) instead.
Execution halted

dimsgheight + :

knitr 1.43.1 (errors)

---
output: github_document
---

```{r}
# remotes::install_github('yihui/knitr', upgrade = TRUE)
packageVersion("knitr")
## [1] '1.43.1'
flextable::flextable(head(iris))

> Error: Functions that produce HTML output found in document targeting gfm+tex_math_dollars-yaml_metadata_block output.
Please change the output type of this document to HTML.
If your aiming to have some HTML widgets shown in non-HTML format as a screenshot,
please install webshot or webshot2 R package for knitr to do the screenshot.
Alternatively, you can allow HTML output in non-HTML formats
by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.

Execution halted

xfun::session_info()

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045), RStudio 2023.6.0.421

Locale:
  LC_COLLATE=English_Canada.utf8  LC_CTYPE=English_Canada.utf8    LC_MONETARY=English_Canada.utf8
  LC_NUMERIC=C                    LC_TIME=English_Canada.utf8    

time zone: America/Toronto
tzcode source: internal

Package version:
  askpass_1.1             base64enc_0.1.3         bslib_0.5.0             cachem_1.0.8           
  cli_3.6.1               commonmark_1.9.0        compiler_4.3.1          cpp11_0.4.3            
  crayon_1.5.2            crul_1.4.0              curl_5.0.1              data.table_1.14.8      
  digest_0.6.31           ellipsis_0.3.2          evaluate_0.21           fastmap_1.1.1          
  flextable_0.9.2.002     fontawesome_0.5.1       fontBitstreamVera_0.1.1 fontLiberation_0.1.0   
  fontquiver_0.2.1        fs_1.6.2                gdtools_0.3.3           gfonts_0.2.0           
  glue_1.6.2              graphics_4.3.1          grDevices_4.3.1         grid_4.3.1             
  highr_0.10              htmltools_0.5.5         httpcode_0.3.0          httpuv_1.6.11          
  jquerylib_0.1.4         jsonlite_1.8.5          knitr_1.43.1            later_1.3.1            
  lifecycle_1.0.3         magrittr_2.0.3          memoise_2.0.1           methods_4.3.1          
  mime_0.12               officer_0.6.2           openssl_2.0.6           promises_1.2.0.1       
  R6_2.5.1                ragg_1.2.5              rappdirs_0.3.3          Rcpp_1.0.10            
  rlang_1.1.1             rmarkdown_2.22          rstudioapi_0.14         sass_0.4.6             
  shiny_1.7.4             sourcetools_0.1.7.1     stats_4.3.1             stringi_1.7.12         
  stringr_1.5.0           sys_3.4.2               systemfonts_1.0.4       textshaping_0.3.6      
  tinytex_0.45            tools_4.3.1             triebeard_0.4.1         urltools_1.7.3         
  utils_4.3.1             uuid_1.1-0              vctrs_0.6.3             withr_2.5.0            
  xfun_0.39               xml2_1.3.4              xtable_1.8-4            yaml_2.3.7             
  zip_2.3.0   
cderv commented 1 year ago

I think this is a change that is expected as it fixed an issue.

As explained in NEWS for 1.41:

Added an argument exact to pandoc_to() and pandoc_from() to decide whether to use/return the exact Pandoc output/input format name. If not (default), Pandoc extensions will be removed from the format name, e.g., latex-smart will be treated as latex.

Change was made at https://github.com/yihui/knitr/commit/96f3ff76407fb3c29663a95887ef6b131e3725fb

Before this knitr was not correctly considering some formats with extensions like html-native_spans or gfm+tex_math_dollars, as respectively html or gfm. The change fixes it. This means is_html_output() has been fixed also, returning TRUE when gfm+tex_math_dollars-yaml_metadata_block was the format used (as showned in the error message)

The side effect of the issue was that flextable in github_document() was not seeing this as a HTML output so doing some saving to image, and after the knitr change, it was considered as a HTML output and then some HTML dependencies were introduced in a GFM output which is not expected usually (hence the issue).

This is fixed in flextable now (https://github.com/davidgohel/flextable/commit/6e53256e04afbecae3e72415630b6991084eb2aa) where gfm is excluded of format considered as being HTML.

Thanks for the report in here ! It will be useful to others that will also bump into this necessary change.

rempsyc commented 11 months ago

I would like to add that this also breaks flextable for reprexes:

reprex::reprex(flextable::flextable(mtcars), std_out_err = TRUE)

This reprex appears to crash R. See standard output and standard error for more details.

Standard output and error

Error : Functions that produce HTML output found in document targeting gfm-yaml_metadata_block output.
Please change the output type of this document to HTML.
If your aiming to have some HTML widgets shown in non-HTML format as a screenshot,
please install webshot or webshot2 R package for knitr to do the screenshot.
Alternatively, you can allow HTML output in non-HTML formats
by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.

Not sure if that message is still valid:

If your aiming to have some HTML widgets shown in non-HTML format as a screenshot, please install webshot or webshot2 R package for knitr to do the screenshot.

packageVersion("webshot")
#> [1] '0.5.4'
packageVersion("webshot2")
#> [1] '0.1.0'

Created on 2023-08-04 with reprex v2.0.2

Even adding venue = "html" creates the same error:

reprex::reprex(flextable::flextable(mtcars), venue = "html")
#> ℹ Non-interactive session, setting `html_preview = FALSE`.
#> ℹ Rendering reprex...
#> Error in `reprex_render()`:
#> ! This reprex appears to crash R. Call `reprex()` again with
#>   `std_out_err = TRUE` to get more info.
#> Backtrace:
#>     ▆
#>  1. └─reprex::reprex(flextable::flextable(mtcars), venue = "html")
#>  2.   └─reprex:::reprex_impl(...)
#>  3.     └─reprex:::reprex_render_impl(...)
#>  4.       └─cli::cli_abort(...)
#>  5.         └─rlang::abort(...)

Created on 2023-08-04 with reprex v2.0.2

Edit: The development (but not CRAN) version of flextable fixes this issue.

packageVersion("flextable")
#> [1] '0.9.3.2'

Created on 2023-08-04 with reprex v2.0.2

github-actions[bot] commented 5 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.