R shiny ligaments 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 output of a DataTable in the first tab to the second tab. For example, when I click on setosa , the next indication is the second tab with the iris dataset containing only setosa . He should execute this part of the code from R: iris[iris$Species=="setosa",] . It should work for other Species in iris .

How can I create a link and run this R command by pressing?


[Refresh in response]

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

  • 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(); }); }" 
  • 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 { ... } }) 
+6
source share
1 answer

It is easier to have a click function in 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 the brilliant 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) 
+2
source

Source: https://habr.com/ru/post/979140/


All Articles