0

I want to insert a hyperlink to DT table in shiny.

To save the loading time, I want to insert hyperlinks to current view (input$table_rows_current).

I have tired with observing but I don't know how to specify where to insert hyperlink and how?

Any help much appreciated.

Here is the sample code:

library(shiny)

createLink <- function(val) {
   sprintf('<a href="https://www.google.com/#q=%s" target="_blank" >%s</a>',val,val)  
}

ui <- fluidPage(  
    titlePanel("Table with Links!"),
    sidebarLayout(
    sidebarPanel(
        h4("Click the link in the table to see
            a google search for the car.")
          ),
    mainPanel(
       dataTableOutput('table1')
            )
          )
        )

server <- function(input, output) {

     output$table1 <- renderDataTable({
        dt <- datatable(mtcars, escape=FALSE, selection = 'none') %>% formatStyle(0, cursor = 'pointer')
      })


     observe({
        List <- input$table1_rows_current
        List <- createLink(List)
         return(List)
     })

}

shinyApp(ui, server)
Prradep
  • 5,506
  • 5
  • 43
  • 84
alan pter
  • 41
  • 3
  • 11
  • Possible duplicate of [Clickable links in Shiny Datatable](https://stackoverflow.com/questions/28117556/clickable-links-in-shiny-datatable) – Pork Chop Oct 12 '17 at 16:34
  • I have different issue.... It is just an example. I have very big data matrix and it takes 15-20 min to load if I use this code......So to save time, I just want to add hyperlinks to current view in the table. – alan pter Oct 12 '17 at 21:24

1 Answers1

0

The function createLink is not working because you missed one val:

Corrected:

createLink <- function(val,val) {
   sprintf('<a href="https://www.google.com/#q=%s' target="_blank" >%s</a>',val,val)  
}

Then you can use createLink() as below:

input$table1_rows_current = createLink(input$table1_rows_current,
                                       input$table1_rows_current)

It will show you embedded link column.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
Della
  • 51
  • 2