问题
I have the shiny dashboard below and I have made the cells of the column Species
interactive in a way that if the user clicks on a word of that column ,for example 'setosa', to move to the tab Species
.This is the only way someone can move to this tab. The issue is that I do not want the tab Species
to be displayed when the user is not in this tab. A secondary solution would be to deactivate Species
'click on' ability. So if the user would accidentaly press it nothing would happen.
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(),
sidebar = dashboardSidebar(),
body = dashboardBody(tabsetPanel(
id = "myTabsetPanel",
tabPanel("Documents",
DTOutput("dt1")),
tabPanel("Species",
DTOutput("dt2"))
)),
),
server = function(input, output, session) {
output$dt1 <- renderDT(
iris,
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
output$dt2 <- renderDT(
mtcars,
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")
}
})
}
)
回答1:
UPDATE: observe the input$dt1_cells_selected
and reset the value at the end of the observeEvent
to allow same cell selection to re-trigger the tab open. You will need to use a dataTableProxy
to do this.
You can use hideTab
and showTab
to reactively hide and show the tab, but still be able to navigate to it via a data table click. More info here. I added a table output to the "Species" tab so we can tell if it has switched properly. By adding an observeEvent around the input$myTabsetPanel
we can have the "Species" tab hidden whenever input$myTabsetPanel
== Documents:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(),
sidebar = dashboardSidebar(),
body = dashboardBody(tabsetPanel(
id = "myTabsetPanel",
tabPanel("Documents",
DTOutput("dt1")),
tabPanel("Species",
DTOutput("dt2"))
))
),
server = function(input, output, session) {
observeEvent(input$myTabsetPanel, {
if(input$myTabsetPanel == "Documents"){
hideTab("myTabsetPanel", "Species")
}
})
output$dt1 <- renderDT(
iris,
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
output$dt2 <- renderDT(
mtcars,
filter = "top",
options = list(pageLength = 5),
selection = list(mode = 'single', target = 'cell')
)
myProxy = DT::dataTableProxy('dt1')
observeEvent(input$dt1_cells_selected,{
# alternative: input$dt1_cells_selected
if (req(input$dt1_cell_clicked$value) == "setosa") {
showTab("myTabsetPanel", "Species")
updateTabsetPanel(session, inputId = "myTabsetPanel", selected = "Species")
DT::selectCells(myProxy, NULL)
}
})
}
)
来源:https://stackoverflow.com/questions/61958925/hide-a-tab-in-shiny-app-when-the-user-is-not-in-this-tab-or-deactivate-it