R Shiny:单击并跳转到不同的选项卡传递值

时间:2016-04-14 01:31:03

标签: javascript jquery r shiny

有没有办法点击dataTableOutput中的元素然后跳转到另一个tabPanel?

我知道使用 escape = FALSE 可以将url添加到table元素中。但是如何添加"跳转到另一个标签"到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)

1 个答案:

答案 0 :(得分:5)

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

好的,我们从哪里开始...... DT有一个内部回调功能来访问用户点击哪些行/列/单元格。请参见示例here。然后没有理由将此信息“发送”给tab2,但我们可以将这些信息用于我们想要的内容。就像在tab2中正确设置文本一样。为了更改选项卡,有光泽具有updateTabsetPanel功能,可让您更改选项卡而无需任何超链接。

改变日志的种类:

  • observeEvent插入功能。
  • 添加了selectedserver属性以获得单行回调
  • 将ID添加到tabsetPanel以启用通信。
  • 摆脱了Google链接和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)