davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
565 stars 82 forks source link

flextable_to_rmd() failing to process embedded ggplot objects in R markdown as of R 4.4.1. #651

Closed pjledge closed 3 months ago

pjledge commented 3 months ago

Not a bug or feature request? If you are looking for help on how to use the package correctly, please read the doc first at https://davidgohel.github.io/flextable/. You can also visit Stackoverflow and tag your question with [flextable]. We usually read them and answer WHEN possible.

I've discovered that embedded ggplot charts inside a flextable column will generate errors when knit under markdown, complaining about missing files, while using R 4.4.1. The code generates tables when manually stepped through from within RStudio.

When submitting a new issue:

- [ ] Provide the code that is producing the error, it has to be a minimal reproducible example. Stackoverflow is providing good explanations about it: https://stackoverflow.com/help/mcve. You can use package reprex to help you: http://reprex.tidyverse.org/. The most popular R stackoverflow question is about the subject: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example.

A small example created using basic data frames, not sure if it can be reduced further. Code can be stepped through inside of RStudio and generate a table, fails with the following error inside of markdown:

output file: Example.knit.md

! Package pdftex.def Error: File `Example_files/figure-latex/unnamed-chunk-4-1. png' not found: using draft setting.

Error: LaTeX failed to compile Example.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Example.log for more info. Execution halted



title: "Example" output: pdf_document: default html_document: default classoption: landscape, a4paper date: "2024-08-12"

knitr::opts_chunk$set(echo = TRUE)

library('flextable')
library('ggplot2')

a <- mpg[1:10,]

addbar <- function(x) {
    g <- ggplot(mpg, aes(class)) +
        geom_bar() +
        theme_void() +
        theme(legend.position = 'none')
    return(g)
}

a$mybar <- apply(a[,c('model')], FUN=addbar,
                                   MARGIN=1)

b <- flextable(a)

fmttable <- mk_par(b, 
                   j=c('mybar'),
                   value = as_paragraph(
                                      gg_chunk(value = .,
                                               height = 0.3,
                                               width= 1)),
                   use_dot = T)
flextable_to_rmd(fmttable)

- [ ] Provide the results of R command sessionInfo(). It had to be executed after you loaded the packages used by your example. This will let me know what is your version of R and what are the versions of the packages you used in your example.

sessionInfo() R version 4.4.1 (2024-06-14) Platform: aarch64-apple-darwin20 Running under: macOS Sonoma 14.5

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York tzcode source: internal

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

other attached packages: [1] timeDate_4032.109 RPostgreSQL_0.7-6 DBI_1.2.3

loaded via a namespace (and not attached): [1] vctrs_0.6.5 cli_3.6.3 knitr_1.48
[4] xfun_0.46 rlang_1.1.4 renv_1.0.7
[7] generics_0.1.3 glue_1.7.0 colorspace_2.1-1 [10] htmltools_0.5.8.1 scales_1.3.0 fansi_1.0.6
[13] rmarkdown_2.27 grid_4.4.1 munsell_0.5.1
[16] evaluate_0.24.0 tibble_3.2.1 fastmap_1.2.0
[19] yaml_2.3.10 lifecycle_1.0.4 bookdown_0.40
[22] compiler_4.4.1 dplyr_1.1.4 pkgconfig_2.0.3
[25] rstudioapi_0.16.0 digest_0.6.36 R6_2.5.1
[28] tidyselect_1.2.1 utf8_1.2.4 pillar_1.9.0
[31] magrittr_2.0.3 tools_4.4.1 gtable_0.3.5
[34] ggplot2_3.5.1

Also reproduced (and originally found) on AWS Linux from 4.4 sources.

- [ ] Make sure you did checked you had the latest version of the package on CRAN (and on github if issue exists with CRAN version).

Downloaded as of 12 Aug 2024

- [ ] Make sure you searched in the open and closed issues on the github repository.

Confirmed.

davidgohel commented 3 months ago

Why not using the default method to print here?

---
title: "Example"
output:
  pdf_document: default
  html_document: default
classoption: landscape, a4paper
date: "2024-08-12"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library("flextable")
library("ggplot2")

a <- mpg[1:10, ]

addbar <- function(x) {
  g <- ggplot(mpg, aes(class)) +
    geom_bar() +
    theme_void() +
    theme(legend.position = "none")
  return(g)
}

a$mybar <- apply(a[, c("model")],
  FUN = addbar,
  MARGIN = 1
)

b <- flextable(a)

fmttable <- mk_par(b,
  j = c("mybar"),
  value = as_paragraph(
    gg_chunk(
      value = .,
      height = 0.3,
      width = 1
    )
  ),
  use_dot = T
)
fmttable
pjledge commented 3 months ago

The actual code from which this was found is inside a function... I perhaps oversimplified the example.

davidgohel commented 3 months ago

OK The issue is about fig.path, I will try to send a fix tomorrow

davidgohel commented 3 months ago

it should be fixed now

pjledge commented 3 months ago

I've confirmed that it worked. Thanks again for the remarkably quick turnaround!