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

Joh*_*ohn 1 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)
Run Code Online (Sandbox Code Playgroud)

K. *_*hde 5

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

好的,我们从哪里开始...... DT有一个内置的回调功能来访问用户点击的行/列/单元格.见这里的例子.然后没有理由"发送"这些信息tab2,但我们可以用这些信息来做我们想要的.就像tab2适当地设置文本一样.为了更改选项卡,有光泽具有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)
Run Code Online (Sandbox Code Playgroud)