Search code examples
rhyperlinkr-markdownkableextra

Is it possible to have kable cells with hyperlinks in r-markdown generated PDF files?


Is it possible to have for PDF outputs kable cells with hyperlinks? e.g. this doesn't work for me:

library(dplyr)
library(knitr)
library(kableExtra)

tbl <- tibble(test=c("A","B","C"),link=c("http://someurl1", "http://someurl2", "http://someurl3"))

tbl <- mutate(test = cell_spec(test, "html", link = link))

kable(tbl, "latex", escape = F, booktabs = T) %>% 
  kable_styling(bootstrap_options = c("hover", "condensed"), full_width = T)

UPDATE using escape = F is the culprit to all the actual error I was having before ... any suggestions how to fix this?

[24]
LaTeX Font Info:    External font `lmex10' loaded for size
(Font)              <10> on input line 1566.
LaTeX Font Info:    External font `lmex10' loaded for size
(Font)              <7> on input line 1566.
! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate 

l.1586 \end{tabu}

!  ==> Fatal error occurred, no output PDF file produced!

Solution

  • Here's a solution using the \\href{}{} idiom from LaTeX:

    tbl <- tibble(test=c("A","B","C"), 
                  link=c("someurl1", "someurl2", "someurl3"))
    
    tbl %>% 
      mutate(test = paste0("\\href{http://", link, "}{", test, "}")) %>%
      kable("latex", escape = F, booktabs = T) %>%
      kable_styling(bootstrap_options = c("hover", "condensed")) 
    

    There may be a pretty way to do it with kable settings but this will work.