带有 DT 包的选项卡之间的 R 闪亮构建链接 [英] R shiny build links between tabs with DT package

查看:15
本文介绍了带有 DT 包的选项卡之间的 R 闪亮构建链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标签之间创建链接的解决方案已在此处找到 标签之间的 R 闪亮构建链接 非常好,但它不适用于 DT 包(对我来说..).谁能告诉我,与没有 DT 包的解决方案相比,我在使用 DT 库的示例代码中做错了什么?

图书馆(闪亮)图书馆(DT)服务器 <- 功能(输入,输出){输出$iris_type <- DT::renderDataTable({数据表(data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>")),逃逸 = FALSE,选项 = 列表(initComplete = JS('功能(表){table.on("click.dt", "tr", function() {Shiny.onInputChange("rows", table.row( this ).index());tabs = $(".tabbable .nav.nav-tabs li a");$(tabs[1]).click();});}')))})输出$filtered_data <- DT::renderDataTable({if(is.null(input$rows)){虹膜}别的{iris[iris$Species %in% unique(iris$Species)[as.integer(input$rows)+1], ]}})}ui <-shinyUI(fluidPage(主面板(标签集面板(tabPanel("虹膜类型", DT::dataTableOutput("iris_type")),tabPanel("过滤数据", DT::dataTableOutput("filtered_data"))))))闪亮应用(用户界面 = 用户界面,服务器 = 服务器)

解决方案

你可以试试下面的代码.我更改了将选项卡切换到回调的函数(以表作为参数),并在您的 output$filtered_data 中,将 iris 替换为 datable(iris) 因为您正在使用 DT::renderDataTable

进行渲染

图书馆(闪亮)图书馆(DT)服务器 <- 功能(输入,输出){输出$iris_type <- DT::renderDataTable({数据表(data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>")),逃逸 = FALSE,回调 = JS('table.on("click.dt", "tr", function() {tabs = $(".tabbable .nav.nav-tabs li a");$(tabs[1]).click();})'))})输出$filtered_data <- DT::renderDataTable({选择 <- input$iris_type_rows_selectedif(is.null(选中)){数据表(虹膜)} 别的 {数据表(iris[iris$Species %in% unique(iris$Species)[selected], ])}})}ui <-shinyUI(fluidPage(主面板(标签集面板(tabPanel("虹膜类型", DT::dataTableOutput("iris_type")),tabPanel("过滤数据", DT::dataTableOutput("filtered_data"))))))闪亮应用(用户界面 = 用户界面,服务器 = 服务器)

请注意,这需要 DT >= 0.0.62.>

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)

解决方案

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.

这篇关于带有 DT 包的选项卡之间的 R 闪亮构建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆