[Query]
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)
[Inquiry]
I am attempting to create a connection between the first tab's DataTable
output and the second tab. For instance, upon clicking on setosa
, the second tab should display the iris
dataset only containing setosa
. This action should execute the following R command: iris[iris$Species=="setosa",]
. The functionality should extend to other Species
in the iris
dataset as well.
How can I establish this link and trigger the execution of that R command with a click event?
[Recent Answer Update]
If you have a distinct layout and require specific steps, consider the following approach:
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(); }); }"
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 { ... } })