【问题标题】:R Shiny: Click and jump to different tab passing valuesR Shiny:单击并跳转到不同的选项卡传递值
【发布时间】:2016-08-05 08:53:19
【问题描述】:

有什么方法可以点击 dataTableOutput 中的元素,然后跳转到不同的 tabPanel?

我知道使用 escape = FALSE 可以将 url 添加到表格元素。但是如何将“跳转到不同的选项卡”添加到 dataTableOutput 元素?并传递价值?

请看一下我的可重现示例。谢谢。

library(shiny)

server <- function(input, output) {
  X = data.frame(
    ID = c(
      "<a href = 'http://www.google.com'> google </a>",
      "Click here then Jump to tab2 and pass x=2 and y=2 to tab2",
      "Click here then Jump to tab2 and pass x=3 and y=4 to tab2"
    ),
    x = c(1, 2, 3),
    y = c(10, 2, 4)
  )
  output$datatable = renderDataTable({X}, escape = FALSE,
  options = list(
    paging = FALSE,
    searching = FALSE,
    filtering = FALSE,
    ordering = FALSE
  ))
  output$text = renderText(paste("X = ", "Y = "))
}

ui <- fluidPage(tabsetPanel(
  tabPanel("tab1", dataTableOutput("datatable")),
  tabPanel("tab2", textOutput("text"))
))

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: javascript jquery r shiny


    【解决方案1】:

    幸运的是,不需要 JS 或 jQuery,因为所有这些事情都可以在 Shinyserver 端完成。

    好的,我们从哪里开始... DT 有一个内置回调功能来访问用户单击了哪些行/列/单元格。请参阅示例here。那么就没有理由将这些信息“发送”到tab2,但我们可以用这些信息做我们想做的事。就像在tab2 中适当地设置文本一样。为了更改标签,shiny 具有updateTabsetPanel 功能,可让您在没有任何超链接的情况下更改标签。

    一种变更日志:

    • 插入observeEvent 以获得功能。
    • 添加了selectedserver 属性以获取单行回调
    • 将 ID 添加到 tabsetPanel 以启用通信。
    • 去掉了谷歌链接和escape

    代码:

    library(shiny)
    library(DT)
    
    server <- function(input, output, session) {
      X = data.frame(
        ID = c("Click here then Jump to tab2 and pass x=1 and y=10 to tab2", 
               "Click here then Jump to tab2 and pass x=2 and y=2 to tab2",
               "Click here then Jump to tab2 and pass x=3 and y=4 to tab2"),
        x = c(1,2,3),
        y = c(10,2,4)    
      )
    
      output$datatable = renderDataTable({X}, selection = "single", server = FALSE,
                                         options = list(paging=FALSE,
                                                        searching=FALSE,
                                                        filtering=FALSE,
                                                        ordering=FALSE)
                                         )
      observeEvent(input$datatable_rows_selected, {
        row <- input$datatable_rows_selected 
        output$text <- renderText({paste("X =", X[row, "x"], "Y =", X[row, "y"])})
        updateTabsetPanel(session, "mainPanel", selected = "tab2")
      })  
    }
    
    ui <- fluidPage(
    
      tabsetPanel(id = "mainPanel", 
        tabPanel("tab1",dataTableOutput("datatable")),
        tabPanel("tab2",textOutput("text"))
      )
    )
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多