Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flextable_to_rmd function ignores local chunk settings in v0.9.6 update #631

Open
jfraper opened this issue May 9, 2024 · 1 comment
Open
Labels

Comments

@jfraper
Copy link

jfraper commented May 9, 2024

Hi!

First, thank you for your continuous efforts in developing this package; it's excellent!

I've recently encountered a potential bug with the flextable_to_rmd function in version 0.9.6. It appears that local Rmarkdown chunk options are ignored by this function, leading to potential issues. This wasn't a problem before 0.9.6.

See a minimal reproducible example below where that issue causes the table not to be displayed because the local eval option is ignored.

---
output: html_document
---

  
```{r}
library(flextable)
ft <- flextable(head(iris))
```
  
  
# Section 1 - It works

```{r, eval = TRUE}
# Set global `eval` to `TRUE`
knitr::opts_chunk$set(eval = TRUE)
```

```{r, eval = TRUE, results = "asis"}
# This works as expected
flextable_to_rmd(ft)
```

# Section 2 - It does not work

```{r}
# Set global `eval` to `FALSE`
knitr::opts_chunk$set(eval = FALSE)
```


```{r, eval = TRUE, results = "asis"}
message("Current eval: ", knitr::opts_current$get("eval"))  # TRUE
message("Global eval: ", knitr::opts_chunk$get("eval"))     # FALSE

# This won't print anything although the `eval` is set to `TRUE` in this chunk
flextable_to_rmd(ft)
```

I suspect the issue stems from how flextable_to_rmd handles chunk options, possibly around lines 100-105 in the source code, where it might not properly account for local settings over global defaults. Ideally, it would respect the local chunk options setting even when the global option is set differently. Currently, the echo option is hardcoded to FALSE, which seems appropriate; however, other relevant options should also be considered and passed accordingly.

writeLines(
    c("```{r echo=FALSE}",
      "x", "```", ""),
    tmp_file,
    useBytes = TRUE)

I believe it would be beneficial if the chunk containing the flextable object could inherit the local chunk options in which it is executed. Perhaps, extracting these options from the current chunk with knitr::opts_chunk$get(...) or an equivalent method might resolve the issue. While it may not be necessary to pass all chunk options to the child chunk, the eval option at least should be considered.

I am including my sessionInfo() output as advised, to provide details on my R environment and package versions.

I have checked both open and closed issues and updated to the latest package version to ensure this issue is not already addressed.

Thank you for looking into this, and I appreciate any guidance or corrections if my understanding is incorrect.

R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.4.1

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: Europe/Madrid
tzcode source: internal

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

other attached packages:
[1] flextable_0.9.6 knitr_1.46      rmarkdown_2.26 

loaded via a namespace (and not attached):
 [1] jsonlite_1.8.8          compiler_4.4.0         
 [3] renv_1.0.7              crayon_1.5.2           
 [5] promises_1.3.0          zip_2.3.1              
 [7] Rcpp_1.0.12             xml2_1.3.6             
 [9] jquerylib_0.1.4         later_1.3.2            
[11] fontquiver_0.2.1        textshaping_0.3.7      
[13] systemfonts_1.0.6       uuid_1.2-0             
[15] yaml_2.3.8              fastmap_1.1.1          
[17] mime_0.12               R6_2.5.1               
[19] gfonts_0.2.0            gdtools_0.3.7          
[21] curl_5.2.1              openssl_2.1.2          
[23] crul_1.4.2              shiny_1.8.1.1          
[25] bslib_0.7.0             rlang_1.1.3            
[27] cachem_1.0.8            httpcode_0.3.0         
[29] httpuv_1.6.15           xfun_0.43              
[31] sass_0.4.9              cli_3.6.2              
[33] magrittr_2.0.3          digest_0.6.35          
[35] grid_4.4.0              xtable_1.8-4           
[37] askpass_1.2.0           lifecycle_1.0.4        
[39] evaluate_0.23           glue_1.7.0             
[41] data.table_1.15.4       fontLiberation_0.1.0   
[43] officer_0.6.5           ragg_1.3.0             
[45] fontBitstreamVera_0.1.1 tools_4.4.0            
[47] htmltools_0.5.8.1      

@davidgohel
Copy link
Owner

Thanks, I think I messed up something with knit_child(envir = ...)

@davidgohel davidgohel added the bug label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants