当用户不在此选项卡中时,在闪亮的应用程序中隐藏该选项卡。或停用它 [英] Hide a tab in shiny app when the user is not in this tab. Or deactivate it
问题描述
我下面有一个发光的仪表板,并且使 Species
列的单元格具有交互性,例如,如果用户单击该列的某个单词, 'setosa',移动到选项卡 Species
。这是有人可以移动到此选项卡的唯一方法。问题是当用户不在此选项卡中时,我不希望显示选项卡 Species
。第二种解决方案是停用 Species
的点击功能。因此,如果用户不小心按了它,什么也不会发生。
库(发光)
库(发光板)
库(shinydashboardPlus)
库(DT)
ShinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(),
sidebar = dashboardSidebar( ),
body = dashboardBody(tabsetPanel(
id = myTabsetPanel,
tabPanel( Documents,
DTOutput( dt1)),
tabPanel( Species,
DTOutput( dt2))
)),
),
服务器=函数(输入,输出,会话){
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),
选择= list(mode ='single',target ='cell')
)
watchEvent(input $ dt1_cell_clicked,{
#可选:input $ dt1_cells_selected
if(req(input $ dt1_cell_clicked $ value)== setosa){
updateTabsetPanel(session,inputId = myTabsetPanel,selected = Species)
}
})
}
)
更新:观察 input $ dt1_cells_selected
并重置 observeEvent
末尾的值,以允许选择相同的单元格来重新触发打开的选项卡。您将需要使用 dataTableProxy
来做到这一点。
您可以使用 hideTab
和 showTab
来隐藏和显示选项卡,但仍可以通过单击数据表来导航至该选项卡。
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")
}
})
}
)
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)
}
})
}
)
这篇关于当用户不在此选项卡中时,在闪亮的应用程序中隐藏该选项卡。或停用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!