1

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?

Vickel
  • 7,879
  • 6
  • 35
  • 56
jd690764
  • 21
  • 1

2 Answers2

2

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
David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • The update works great for simple urls, but e.g.: [entrez](https://www.ncbi.nlm.nih.gov/gene?cmd=Retrieve&dopt=full_report&list_uids=6850) will give a parsing error when the flextable is included in an rpptx object by ph_at_flextable. URLencode can fix this parsing error, but now the flextable is broken, clicking the link won't open the web-page. – jd690764 Mar 03 '18 at 22:42
  • Could you provide a reproducible example? – David Gohel Mar 04 '18 at 09:52
0

`

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
jd690764
  • 21
  • 1
  • just fixed that on github. Note there is an issue with the URL and pptx/word on some config. For example, I can use "http://www.google.fr/search?source=hp&q=officer+R+package" but not yours. From MS web site, it is solved with office `version >= 2016` – David Gohel Mar 07 '18 at 00:11