29

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.

userJT
  • 11,486
  • 20
  • 77
  • 88
Komal Rathi
  • 4,164
  • 13
  • 60
  • 98
  • 1
    See answer here: http://stackoverflow.com/questions/21909826/r-shiny-open-the-urls-from-rendertable-in-a-new-tab – Ken Yeoh Jun 17 '15 at 20:25
  • 3
    @KenYeoh, that answer is not entirely applicable as here the question relates to DT:renderDataTable, not shiny:renderTable and if he does what is suggested there it won't work because of DT will escape the HTML by default. – jrdnmdhl Jun 17 '15 at 20:34
  • 4
    @jrdnmdhl Exactly! It doesn't work because I am using DT::renderDataTable and not shiny::renderDataTable – Komal Rathi Jun 17 '15 at 20:38
  • Alternatively, it's also possible to keep the original dataframe intact and tell dataTable how to render a column. See section 4.4 Column Rendering in the [DT docs](https://rstudio.github.io/DT/options.html). – DeanAttali Jun 17 '15 at 23:38
  • In the link, there is no example that would do hyperlinks. Can you add the actual code. – userJT Jun 05 '17 at 18:13

1 Answers1

51

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>")
jrdnmdhl
  • 1,935
  • 18
  • 26