rdtflexdashboard

Add hyperlinks to cells (and/or rows) of DT table in flexdashboard


I have a large table which I would like to present in interactive (filterable and sortable) form within a flexdashboard. I have achieved this using the DT package. One of the columns contains URLs, which currently are not 'clickable', so users will have to copy and paste into their browser. Is there a way to make these URLs into hyperlinks?

I have tried adding html tags in this format:

<a href=\"https://www.website-address.co.uk/something/or/other">Link text</a>

But the tags themselves display in the table, with no hyperlink.

Another related question - as these URLs are taking up a large proportion of the width of the table, is it possible to make an entire row 'clickable', directing users to the URL stored in 'column B' of the table, without displaying 'column B'?

The other answers I have found apply to using DT in JavaScript or shiny, and seem not to match my code.

Thank you :)

Example code:

df <- data.frame(fruit = c("apple", "pear", "banana"), 
             colour = c("red", "green", "yellow"), 
             website = c("<a href=\"https://en.wikipedia.org/wiki/Apple#/media/File:Red_Apple.jpg\">Apple</a>", "<a href=\"https://dictionary.cambridge.org/dictionary/english/pear\">Pear</a>", "<a href=\"https://en.wikipedia.org/wiki/Banana#/media/File:Bananas.jpg\">Banana</a>"))

datatable(df)

Solution

  • You can use escape=FAlSE argument in datatable() function.

    Here is an example:

    df <- data.frame(textLink=paste0('<a href=\"https://www.website-address.co.uk/something/or/other">Link text</a>'))
    
    datatable(df, escape = FALSE)