Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

javascript - R shiny build links between tabs

[Code]

library(shiny)

server <- function(input, output) {
  output$iris_type <- renderDataTable({
    data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>"))
  })
  output$filtered_data <- renderDataTable({iris})
}

ui <- shinyUI(fluidPage(
  mainPanel(
    tabsetPanel(
      tabPanel("Iris Type", dataTableOutput("iris_type")),
      tabPanel("Filtered Data", dataTableOutput("filtered_data"))
    )
  )
))

shinyApp(ui = ui, server = server)

[Question]

I am trying to link the DataTable output on first tab to the second tab. For example, when I click on setosa, the next thing showing up is second tab with iris dataset containing only setosa. It should execute this piece of code from R: iris[iris$Species=="setosa",]. It should work for other Species in iris too.

How can I build up the link and run that R command by clicking?


[Update on answer]

In case you have a different layout and needs to be specific, here is what you can do.

  1. Your DataTable callback function:

    callback =
    "function(table) {
       table.on('click.dt', 'tr', function() {
         Shiny.onInputChange('rows', table.row(this).data()[0] );
         $(".tabbable .nav.nav-tabs li a:contains('Filtered Data')").click();
       });
     }"
    
  2. Your R code:

    output$filtered_data <- renderDataTable({
      tagString <- input$rows
      rawTags <- gsub("</a>", "", gsub("<a href='#filtered_data'>", "", tagString))
    
      if (identical(tagString, character(0))) {
        iris
      } else {
        ...
      }
    })
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is easier to have the click function on the row of the first table. You can add a callback that looks for a click on the rows of the table. When a click is observed the row index is sent to a shiny reactive input:

library(shiny)

server <- function(input, output) {
  output$iris_type <- renderDataTable({
    data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>"))
  },
  callback = "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 <- 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", dataTableOutput("iris_type")),
      tabPanel("Filtered Data", dataTableOutput("filtered_data"))
    )
  )
))

shinyApp(ui = ui, server = server)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...