问题
I have the shiny dashboard below in which in tab Documents
I display the first 2 lines of the iris
dataset.
When I click on any cell of the column Species
I automatically move to the View
tab.
But I need the functionality described below.
When the user clicks on the setosa
cell of the 1st row in Documents
tab the datatable in the sidebar in the View
tab only should display iris
dataset. When I click on the setosa
cell of the the 2nd row in Documents
tab the datatable in the sidebar in the View
tab only should display another dataframe, lets say mtcars
.
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(),
sidebar = dashboardSidebar(
DT::DTOutput("dt2")
),
body = dashboardBody(tabsetPanel(
id = "myTabsetPanel",
tabPanel("Documents",
DTOutput("dt1")),
tabPanel("Species")
)),
),
server = function(input, output, session) {
output$dt1 <- renderDT(
iris[1:2,],
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
observeEvent(input$dt1_cell_clicked, {
# alternative: input$dt1_cells_selected
if (req(input$dt1_cell_clicked$value) == "setosa") {
updateTabsetPanel(session, inputId = "myTabsetPanel", selected = "Species")
}
})
output$dt2<-renderDT(
if(input$myTabsetPanel=="Species"){
iris
}
else{
return(NULL)
}
)
}
)
回答1:
Something like this?
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
library(datasets)
shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(),
sidebar = dashboardSidebar(),
body = dashboardBody(tabsetPanel(
id = "myTabsetPanel",
tabPanel("Documents",
DT::DTOutput("dt1")),
tabPanel("Species",
DT::DTOutput("dt2"))
)),
),
server = function(input, output, session) {
output$dt1 <- renderDT(
iris[1:2,],
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
observeEvent(input$dt1_cell_clicked, {
# alternative: input$dt1_cells_selected
if (req(input$dt1_cell_clicked$value) == "setosa") {
updateTabsetPanel(session, inputId = "myTabsetPanel", selected = "Species")
}
})
output$dt2 <- renderDT(
if(input$dt1_cell_clicked$row == 1){
iris
}
else{
mtcars
}
)
}
)
来源:https://stackoverflow.com/questions/61911536/display-datatable-based-on-certain-cell-selection-of-another-datatable-and-in-ac