Convert a column of text URLs into active hyperlinks in Shiny

Komal Rathi picture Komal Rathi · Jun 17, 2015 · Viewed 12.9k times · Source

I am creating a user interface for a pathway enrichment program. The results are shown in a table as shown below.

enter image description here

Below is a snippet showing that I am using DT::renderDataTable and DT::datatable to output the table in a tab. spia_out() is just a reactive function that runs the pathway enrichment and produces a dataframe.

spia_out <- reactive({
    ...get results in a dataframe...
  })

output$spiaout <- DT::renderDataTable({
      DT::datatable(spia_out(), extensions = ..., options = ...)
  })

Everything works fine, the pathway enrichment table is generated & printed in the corresponding UI element. My only problem is how to convert the last column (KEGGLINK) of URLs into active hyperlinks? So that people can just click on them instead of copy & pasting.

Apologies in advance for the screenshot's size. I hope you can see the last column KEGGLINK has URLs but they are not active.

Answer

jrdnmdhl picture jrdnmdhl · Jun 17, 2015

You need to do two things:

  1. Modify the last column so that the KEGGLINK is changed into a proper HTML link that looks like: <a href='url'>link text</a>.

  2. Pass DT the escape = FALSE argument so that it doesn't escape the HTML code.

The DT web page has an example of this in section 2.9: https://rstudio.github.io/DT/

A simple way to do #1 would be something like:

mydata$url <- paste0("<a href='",mydata$url,"'>",mydata$url,"</a>")