最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - R Shiny: Click and jump to different tab passing values - Stack Overflow

programmeradmin3浏览0评论

Is there any way to click an element in a dataTableOutput and then jump to a different tabPanel?

I know using escape = FALSE could add url to the table element. But how to add "jumping to a different tab" to a dataTableOutput element? And passing values?

Please take a look at my reproducible example. Thanks.

library(shiny)

server <- function(input, output) {
  X = data.frame(
    ID = c(
      "<a href = ''> 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)

Is there any way to click an element in a dataTableOutput and then jump to a different tabPanel?

I know using escape = FALSE could add url to the table element. But how to add "jumping to a different tab" to a dataTableOutput element? And passing values?

Please take a look at my reproducible example. Thanks.

library(shiny)

server <- function(input, output) {
  X = data.frame(
    ID = c(
      "<a href = 'http://www.google.'> 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)
Share Improve this question edited Oct 31, 2017 at 19:03 user5249203 4,6681 gold badge20 silver badges51 bronze badges asked Apr 14, 2016 at 1:31 JohnJohn 1,8283 gold badges30 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Luckily, there is no need for JS, or jQuery, since all those things can be done on Shinyserver side.

Okay, where do we start... DT has an inbuild callback feature to acess which rows/columns/cells were clicked by the user. See example here. Then there is no reason to "send" this information to tab2, but we can just do with this information what we wanted to. Like setting the text in tab2 appropriately. In order to change tabs, shiny has the updateTabsetPanel function that lets you change tabs without any hyperlinks.

Kind of a changelog:

  • insertet the observeEvent for functionality.
  • added selected and server attribute to get a single row callback
  • added Id to tabsetPanel to enable munication.
  • got rid of the google link and escape.

Code:

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)
发布评论

评论列表(0)

  1. 暂无评论