【问题标题】:R shiny build links between tabs with DT package带有 DT 包的选项卡之间的 R 闪亮构建链接
【发布时间】:2015-06-30 10:48:09
【问题描述】:

R shiny build links between tabs 找到的选项卡之间创建链接的解决方案非常好,但它不适用于 DT 包(对我来说......)。 谁能告诉我,与没有 DT 包的解决方案相比,我在使用 DT 库的示例代码中做错了什么?

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)

【问题讨论】:

    标签: r datatables shiny


    【解决方案1】:

    你可以试试下面的代码。我将切换选项卡的函数更改为回调(将表作为参数)并在您的output$filtered_data 中,将iris 替换为datable(iris),因为您正在使用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)
    

    请注意,这需要DT >= 0.0.62

    【讨论】:

      【解决方案2】:

      最后,我在 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-25
        • 2018-09-16
        • 1970-01-01
        • 2020-05-24
        • 2016-01-02
        • 2016-05-03
        • 1970-01-01
        相关资源
        最近更新 更多