R shiny bundles between tabs with DT package

The solution for creating links between tabs a, R found here by brilliant links between tabs is really nice, but it does not work with the DT package (for me ..), can someone tell me what I'm doing wrong in my code example with using the DT library compared to a solution without a 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)
+4
source share
2 answers

You can try the code below. I changed the function of switching tabs to a callback (which has a table as an argument) and in yours output$filtered_data, replaced irisby datable(iris), since you are rendering withDT::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)

, DT >= 0.0.62.

+3

onclick. , ? (NicE ?)

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)
+1

Source: https://habr.com/ru/post/1584074/


All Articles