Shiny DT table with dynamic hyperlinks

2,518 views
Skip to first unread message

Mitul Patel

unread,
Oct 13, 2017, 11:13:03 AM10/13/17
to Shiny - Web Framework for R
Hello,

I have been working on Shiny app. I like to insert hyperlink to DT datatable (FIRST column).

I have got the code and it is running fine for a small dataset. But I have a very large file (25000 rows). It takes 15-20 min.

So to save loading time, I like to insert hyperlinks to rows on current view in datatable. So instead of inserting hyperlink to whole dataset prior to loading, I just want hyperlink inserted as the datatable view changes.

Can anyone help me to achieve this?

Many thanks.


Here is my 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)

James Ferguson

unread,
Oct 14, 2017, 12:54:36 AM10/14/17
to Shiny - Web Framework for R
Mitul - It may be easier to detect a row or selion selection

eg

hyperlink <- eventReactive(input$objects_cell_clicked,{
    info  <-  input$objects_cell_clicked
    cellValue <- info$value

James Ferguson

unread,
Oct 14, 2017, 12:59:46 AM10/14/17
to Shiny - Web Framework for R
Mitul - It may be "Lazier"  to detect a row or selection
and only convert into a  url when selected - then observe any selected URL and act accordingly

 

eg

hyperlink <- eventReactive(input$objects_cell_clicked,{
    info  <-  input$objects_cell_clicked
    cellValue <- info$value
    paste0("http://search_url?",cellValue")

}

observe({ req(hyperlink()) -> link_to_search })

On Friday, 13 October 2017 17:13:03 UTC+2, Mitul Patel wrote:

Mitul Patel

unread,
Oct 16, 2017, 5:04:34 PM10/16/17
to Shiny - Web Framework for R
James, Thanks for the suggestion.  

Converting cell into hyperlink when cell clicked is the way forward....

But I am not getting it correct. When I click cell nothing happen.

what is link_to_search?

Many thanks,

James Ferguson

unread,
Oct 17, 2017, 4:56:05 AM10/17/17
to Shiny - Web Framework for R
Hi Mitul

1. Are you using the table name ?

in my pseudo code I wrote 

input$objects_cell_clicked - this assumes table name "objects"

Have a look at example here https://yihui.shinyapps.io/DT-click/


# input$tableId_cell_clicked: information about the cell being clicked of the form list(row = row_index, col = column_index, value = cell_value) 

SO for the case tableId == 'x1'
output$x1 = DT::renderDataTable({
    datatable(cars, selection = 'none')
  })

  observeEvent(input$x1_cell_clicked, {
    info = input$x1_cell_clicked
    
    if (!is.null(info$value) )  # Not null means we have a legitimate address


  })


input$objects_cell_clicked

James Ferguson

unread,
Oct 17, 2017, 5:29:18 AM10/17/17
to Shiny - Web Framework for R
Sorry acciidentally posted half answer

Hi Mitul

1. Are you using the table name ?

in my pseudo code I wrote 

input$objects_cell_clicked - this assumes table name "objects"

Have a look at example here https://yihui.shinyapps.io/DT-click/


# input$tableId_cell_clicked: information about the cell being clicked of the form list(row = row_index, col = column_index, value = cell_value) 

SO for the case tableId == 'x1'
output$x1 = DT::renderDataTable({
    datatable(cars, selection = 'none')
  })

  observeEvent(input$x1_cell_clicked, {
    info = input$x1_cell_clicked
    
    if (!is.null(info$value) ) { # Not null means we have a legitimate address
    addr = info$value
    # Here you need to convert this addr data into the search parameter and send off to google and present the results somewhere
    # So you will need to invoke a search (probably easy but I dont know api but see example for using a  url shortening AI here  https://mark.shinyapps.io/googleAuthRexample/
    # Good luck !
    }
  })

Mitul Patel

unread,
Oct 18, 2017, 11:55:52 AM10/18/17
to Shiny - Web Framework for R
Hi James,

Many thanks for the code.

Using the code I can print the correct url from addr variable. But I couldnt figure out how do i invoke the url so that it open in browser. Once the user click on cell it should convert the cell value to url and open in the browser. At the moment it just converts cell values into urls but not showing hyperlinks into DT table or opening browser.

sorry to bother you again.

Many thanks.

Mitul
Reply all
Reply to author
Forward
0 new messages