1 回答
TA贡献2039条经验 获得超7个赞
您可以使用input$navBar(您的 id in navbarPage())在 navbarPage 上收听并通过 javascript 触发更改。
observeEvent(input$navBar, {
runjs('
var elem = document.getElementsByClassName("navbar-collapse")[0]
elem.setAttribute("aria-expanded", "false");
elem.setAttribute("class", "navbar-collapse collapse");
')
})
可重现的例子:
library(shiny)
library(shinyjs)
ui <- tagList(
useShinyjs(),
navbarPage(
title=NULL, id = "navBar", collapsible = TRUE,
tabPanel(title = "Panel1", uiOutput('panel1')),
tabPanel(title = "Panel2", uiOutput('panel2')),
tabPanel(title = "Panel3", uiOutput('panel3'))
)
)
server <- function(input, output, session) {
output$panel1 <- renderUI({p("This is panel 1")})
output$panel2 <- renderUI({p("This is panel 2")})
output$panel3 <- renderUI({p("This is panel 3")})
observeEvent(input$navBar, {
runjs('
var elem = document.getElementsByClassName("navbar-collapse")[0]
elem.setAttribute("aria-expanded", "false");
elem.setAttribute("class", "navbar-collapse collapse");
')
})
}
shinyApp(ui = ui, server = server)
添加回答
举报