R Shiny使用DT包在选项卡之间建立链接

4
我在这里找到了一个创建选项卡之间链接的解决方案,链接如下: 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)
2个回答

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

1
最终我使用了一个小技巧,在 . 上使用 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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接