davidgohel / flextable

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

Flextable hyperlink not working in .docx Quarto documents #659

Closed filipwastberg closed 1 month ago

filipwastberg commented 2 months ago

When creating a Quarto document with .docx format, hyperlinks seem to be broken.

This works fine:

library(officer)
library(flextable)

t <- data.frame(col = "https://www.bbc.com/news", href = "https://www.bbc.com/news")

ft <- flextable::flextable(t)
ft <- flextable::compose(x = ft, 
                         j = "col", 
                         value = flextable::as_paragraph(
                           flextable::hyperlink_text(x = `col`, url = `href`)
                         ))

print(ft, preview = "docx")

But when creating a Quarto document with docx format, the hyperlinks in the document look clickable (there is a hand when hoovering above them) but when you click it, nothing happens.

---
title: "test"
format: docx
---

## Test

```{r}
library(officer)
library(flextable)

t <- data.frame(col = "https://www.bbc.com/news", href = "https://www.bbc.com/news")

ft <- flextable::flextable(t)
ft <- flextable::compose(x = ft, 
                         j = "col", 
                         value = flextable::as_paragraph(
                           flextable::hyperlink_text(x = `col`, url = `href`)
                         ))

ft

Not sure if this is caused by Quarto or if it is a bug in flextable?

SessionInfo ```r > sessionInfo() R version 4.4.0 (2024-04-24) Platform: x86_64-pc-linux-gnu Running under: Ubuntu 22.04.4 LTS Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0 locale: [1] LC_CTYPE=sv_SE.UTF-8 LC_NUMERIC=C LC_TIME=sv_SE.UTF-8 LC_COLLATE=sv_SE.UTF-8 [5] LC_MONETARY=sv_SE.UTF-8 LC_MESSAGES=sv_SE.UTF-8 LC_PAPER=sv_SE.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=sv_SE.UTF-8 LC_IDENTIFICATION=C time zone: Europe/Stockholm tzcode source: system (glibc) attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] flextable_0.9.6 officer_0.6.6 loaded via a namespace (and not attached): [1] jsonlite_1.8.8 compiler_4.4.0 crayon_1.5.3 promises_1.3.0 Rcpp_1.0.13 [6] zip_2.3.1 xml2_1.3.6 later_1.3.2 fontquiver_0.2.1 systemfonts_1.1.0 [11] textshaping_0.4.0 uuid_1.2-1 fastmap_1.2.0 mime_0.12 R6_2.5.1 [16] gfonts_0.2.0 gdtools_0.3.7 curl_5.2.2 knitr_1.48 openssl_2.2.1 [21] crul_1.5.0 shiny_1.9.1 rlang_1.1.4 httpcode_0.3.0 httpuv_1.6.15 [26] xfun_0.44 cli_3.6.3 magrittr_2.0.3 digest_0.6.37 grid_4.4.0 [31] rstudioapi_0.16.0 xtable_1.8-4 askpass_1.2.0 lifecycle_1.0.4 evaluate_0.24.0 [36] glue_1.7.0 data.table_1.15.4 fontLiberation_0.1.0 ragg_1.3.2 fontBitstreamVera_0.1.1 [41] rmarkdown_2.28 tools_4.4.0 htmltools_0.5.8.1 ```
swagm91 commented 2 months ago

I am able to add hyperlinks in a flextable using mk_par. But when I am trying to update a ppt with a flextable it is showing an error : Error in doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html, : EntityRef: expecting ';' [23]

Can anyone please help me on this ?

@davidgohel

davidgohel commented 2 months ago

@swagm91 This works fine on my computer

library(officer)
library(flextable)

t <- data.frame(col = "https://www.bbc.com/news", href = "https://www.bbc.com/news")

ft <- flextable::flextable(t)
ft <- flextable::compose(x = ft, 
                         j = "col", 
                         value = flextable::as_paragraph(
                           flextable::hyperlink_text(x = `col`, url = `href`)
                         ))

print(ft, preview = "pptx")
davidgohel commented 2 months ago

@filipwastberg

Not sure if this is caused by Quarto or if it is a bug in flextable?

We can't post-process documents from Quarto as it is done with R Markdown, so hyperlinks and images are not supported in Quarto for Word. Maybe we will find a solution in the future but we can't dedicate time for it at the moment.

swagm91 commented 2 months ago

@swagm91 This works fine on my computer

library(officer)
library(flextable)

t <- data.frame(col = "https://www.bbc.com/news", href = "https://www.bbc.com/news")

ft <- flextable::flextable(t)
ft <- flextable::compose(x = ft, 
                         j = "col", 
                         value = flextable::as_paragraph(
                           flextable::hyperlink_text(x = `col`, url = `href`)
                         ))

print(ft, preview = "pptx")

Hi @davidgohel ,

Thank you for your response. I think some of the hyperlinks contains special characters like % which is causing the error. Is there any way these links cab be added in ppt ? Also, I was using props for font and color of the column containing hyperlinks but the color is not coming. Also after removing all the link containing special characters still when I am downloading the ppt it is first repairing and then opening. In R, I am able to do everything in flextable but not in ppt. Is there a way to save that in html and then put it in a deck later ?

davidgohel commented 2 months ago

@swagm91 please provide a reproducible example, your sessionInfo result in a new issue. For now, I can't reproduce anything

swagm91 commented 2 months ago

Hi @davidgohel , within as_paragraph I am using props = fp_text_default(font.size =10, font.family= ‘Calibri’, color = ‘#00CC33’). The font size and font family is coming but the color is not coming in ppt.

davidgohel commented 2 months ago

@swagm91 please provide a reproducible example, your sessionInfo result in a new issue. For now, I can't reproduce anything

filipwastberg commented 1 month ago

For future reference: if you for example want to build a Shiny app where people can download a Word report generated by Quarto that contains a flextable the better solutions is to use another fantastic package from @davidgohel officer. This will also make the report generation much faster than in Quarto.