jd690764
jd690764

Reputation: 21

How to include hyperlinks in flextable?

I am trying to make a powerpoint file with officer that includes tables with hyperlinks in their cells, but I can't find a way how to do it.

E.g. a row in a 2 column table on a slide could include 'Ensembl' in the 1st column, the 2nd column would say 'ENSG00000165025' and clicking on it would open up the browser at 'uswest.ensembl.org/Homo_sapiens/Gene/Summary?g=ENSG00000165025'. Some values in the 2nd column could be just plain text.

Is this possible to achieve?

Upvotes: 1

Views: 1292

Answers (2)

jd690764
jd690764

Reputation: 21

`

dat <- data.frame(
    col = "entrez", 
    href = "https://www.ncbi.nlm.nih.gov/gene?cmd=Retrieve&dopt=full_report&list_uids=6850", 
    stringsAsFactors = FALSE)

ft <- flextable(dat)
ft <- display(
    ft, col_key = "col", pattern = "# {{mylink}}",
    formatters = list(mylink ~ hyperlink_text(href, col) )
)
ft # works fine

doc <- read_pptx() %>% 
    add_slide(layout = 'Title and Content', 'Office Theme') %>% 
    ph_with_flextable(ft) # error

Error in doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html, : EntityRef: expecting ';' [23]

repeat with:

dat <- data.frame(
    col = "entrez", href = URLencode("https://www.ncbi.nlm.nih.gov/gene?cmd=Retrieve&dopt=full_report&list_uids=6850", reserved = TRUE),
    stringsAsFactors = FALSE)

ft <- flextable(dat)
ft <- display(
    ft, col_key = "col", pattern = "# {{mylink}}",
    formatters = list(mylink ~ hyperlink_text(href, col) )
)
ft # clicking the link in rstudio fails

doc <- read_pptx() %>% 
    add_slide(layout = 'Title and Content', 'Office Theme') %>% 
    ph_with_flextable(ft) # fine, no error message, but error message when opening pp file

Upvotes: 0

David Gohel
David Gohel

Reputation: 10675

With the new version on github, you will be able to include hyperlinks as demo below:

library(flextable)
dat <- data.frame(
  col = "CRAN website", href = "https://cran.r-project.org",
  stringsAsFactors = FALSE)

ft <- flextable(dat)
ft <- display(
  ft, col_key = "col", pattern = "# {{mylink}}",
  formatters = list(mylink ~ hyperlinked_text(href, col) )
)
ft

Upvotes: 2

Related Questions