4

Solution for creating links between tabs a have found here R shiny build links between tabs is really nice, but it's not working with DT package (for me..). Can anybody tell me, what am I doing wrong in my example code using DT library in compare to the solution without DT package?

library(shiny)
library(DT)

server <- function(input, output) {
    output$iris_type <- DT::renderDataTable({
        datatable(data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>")),
                  escape = FALSE,
                  options = list(initComplete = JS(
'function(table) {
    table.on("click.dt", "tr", function() {
    Shiny.onInputChange("rows", table.row( this ).index());
    tabs = $(".tabbable .nav.nav-tabs li a");
    $(tabs[1]).click();
    });
}')))
    })

  output$filtered_data <- DT::renderDataTable({
      if(is.null(input$rows)){
          iris
      }else{
          iris[iris$Species %in% unique(iris$Species)[as.integer(input$rows)+1], ]
      }
      })
  }

ui <- shinyUI(fluidPage(
    mainPanel(
        tabsetPanel(
            tabPanel("Iris Type", DT::dataTableOutput("iris_type")),
            tabPanel("Filtered Data", DT::dataTableOutput("filtered_data"))
        )
    )
))

shinyApp(ui = ui, server = server)
Community
  • 1
  • 1
user1991825
  • 309
  • 1
  • 5
  • 16

2 Answers2

3

You could try the code below. I changed the function switching the tabs to the callback (which has table as an argument) and in your output$filtered_data, replaced iris by datable(iris) since you are rendering with DT::renderDataTable

library(shiny)
library(DT)

server <- function(input, output) {
  output$iris_type <- DT::renderDataTable({
    datatable(data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>")),
              escape = FALSE,
              callback = JS(
                'table.on("click.dt", "tr", function() {
    tabs = $(".tabbable .nav.nav-tabs li a");
    $(tabs[1]).click();})'))
  })

  output$filtered_data <- DT::renderDataTable({
    selected <- input$iris_type_rows_selected
    if(is.null(selected)){
      datatable(iris)
    } else {
      datatable(iris[iris$Species %in% unique(iris$Species)[selected], ])
    }
  })
}

ui <- shinyUI(fluidPage(
  mainPanel(
    tabsetPanel(
      tabPanel("Iris Type", DT::dataTableOutput("iris_type")),
      tabPanel("Filtered Data", DT::dataTableOutput("filtered_data"))
    )
  )
))

shinyApp(ui = ui, server = server)

Please note this requires DT >= 0.0.62.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
NicE
  • 21,165
  • 3
  • 51
  • 68
1

In the end I used a little hack with onclick event on . Which way do you think is more clear? (NicE's or this one?)

library(shiny)
library(DT)

server <- function(input, output) {
    output$iris_type <- DT::renderDataTable({
        datatable(data.frame(Species=paste0("<a href='#filtered_data'",
                                            "alt='",unique(iris$Species),"'",                                                 
                                            "onclick=\"",
                                            "tabs = $('.tabbable .nav.nav-tabs li');",
                                            "tabs.each(function() {",
                                            "$(this).removeClass('active')",
                                            "});",
                                            "$(tabs[1]).addClass('active');",
                                            "tabsContents = $('.tabbable .tab-content .tab-pane');",
                                            "tabsContents.each(function() {",
                                            "$(this).removeClass('active')",
                                            "});",
                                            "$(tabsContents[1]).addClass('active');",
                                            "$('#filtered_data').trigger('change').trigger('shown');",
                                            "Shiny.onInputChange('species', getAttribute('alt'));",
                                            "\">",
                                            unique(iris$Species),
                                            "</a>")),
                  escape = FALSE)
    })

    output$filtered_data <- DT::renderDataTable({
        if(is.null(input$species)){
            datatable(iris)
        }else{
            datatable(iris[iris$Species %in% input$species, ])
        }
    })
    }

ui <- shinyUI(fluidPage(
    mainPanel(
        tabsetPanel(
            tabPanel("Iris Type", DT::dataTableOutput("iris_type")),
            tabPanel("Filtered Data", DT::dataTableOutput("filtered_data"))
        )
    )
))

shinyApp(ui = ui, server = server)
user1991825
  • 309
  • 1
  • 5
  • 16