gpt4 book ai didi

R Shiny - 导航到选项卡项时自动隐藏侧边栏

转载 作者:行者123 更新时间:2023-12-04 10:54:37 26 4
gpt4 key购买 nike

我有一个 Shiny 应用程序 - 此处是简化示例 - 我希望侧边栏在导航到选项卡项时动态隐藏。事实上,用户将主要通过他们的手机连接到应用程序。

在帖子的帮助下Hide sidebar in default in shinydashboard ,我知道如何在您到达应用程序时默认隐藏侧边栏,但在侧边栏始终显示之后。

这是我的实际代码:

### Load librairies
library(shiny) ; library(shinydashboard) ; library(shinyjs)
library(dplyr)

### Load data
Weather <- c("cold", "rain", "snow","heat","sun")
Answer <- c("Take a coat","Take an umbrella","Take gloves","Take a swimsuit","Take solair cream")
Mydata <- data.frame( Weather, Answer, stringsAsFactors = FALSE)

remove(Weather, Answer)

### Shiny
Entete <- dashboardHeader(title = "My app")

BarreLaterale <- dashboardSidebar(
sidebarMenu(menuItem(text = "Home", tabName = "MyHome", icon = icon("home"))),
sidebarMenu(menuItem(text = "My search", tabName = "Search", icon = icon("search")))
)

Corps <- dashboardBody(

useShinyjs(),

tabItems(

tabItem(tabName = "MyHome",
fluidPage("Hello, welcome to the home page")
),

tabItem(tabName = "Search",
fluidRow(
box(title = "Weather choice", width = 6, solidHeader = TRUE, status = "danger",
selectInput(inputId = "WeatherChoice", label = NULL, choices = unique(Mydata$Weather))),
box(title = "Answer", width = 6, solidHeader = TRUE, status = "danger",
textOutput("ReturnAnswer"))
)
)

)
)

Interface <- dashboardPage(Entete, BarreLaterale, Corps, skin = "red")

### Server R
Serveur <- function(input, output, session) {

output$ReturnAnswer <- renderText({
as.character(Mydata %>% filter(Weather == input$WeatherChoice) %>% select(Answer))
})

addClass(selector = "body", class = "sidebar-collapse")

}

### Application
shinyApp(Interface, Serveur)

最佳答案

我添加了一个 id给您的 sidebarmenu (注意:你只需要一个 sidebarmenu 和多个 menuItems )和一个 observeEvent使用 id 收听所选选项卡中的更改:

### Load librairies
library(shiny) ; library(shinydashboard) ; library(shinyjs)
library(dplyr)

### Load data
Weather <- c("cold", "rain", "snow","heat","sun")
Answer <- c("Take a coat","Take an umbrella","Take gloves","Take a swimsuit","Take solair cream")
Mydata <- data.frame( Weather, Answer, stringsAsFactors = FALSE)

remove(Weather, Answer)

### Shiny
Entete <- dashboardHeader(title = "My app")

BarreLaterale <- dashboardSidebar(
sidebarMenu(id="mysidebar",
menuItem(text = "Home", tabName = "MyHome", icon = icon("home")),
menuItem(text = "My search", tabName = "Search", icon = icon("search")))
)

Corps <- dashboardBody(

useShinyjs(),

tabItems(

tabItem(tabName = "MyHome",
fluidPage("Hello, welcome to the home page")
),

tabItem(tabName = "Search",
fluidRow(
box(title = "Weather choice", width = 6, solidHeader = TRUE, status = "danger",
selectInput(inputId = "WeatherChoice", label = NULL, choices = unique(Mydata$Weather))),
box(title = "Answer", width = 6, solidHeader = TRUE, status = "danger",
textOutput("ReturnAnswer"))
)
)

)
)

Interface <- dashboardPage(Entete, BarreLaterale, Corps, skin = "red")

### Server R
Serveur <- function(input, output, session) {

output$ReturnAnswer <- renderText({
as.character(Mydata %>% filter(Weather == input$WeatherChoice) %>% select(Answer))
})

# this line is now actually obsolete.
addClass(selector = "body", class = "sidebar-collapse")

observeEvent(input$mysidebar,
{
# for desktop browsers
addClass(selector = "body", class = "sidebar-collapse")
# for mobile browsers
removeClass(selector = "body", class = "sidebar-open")
})

### Application
shinyApp(Interface, Serveur)

现在,每当您从一个选项卡切换到另一个选项卡时,侧边栏都会再次隐藏。

希望这可以帮助!

关于R Shiny - 导航到选项卡项时自动隐藏侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47830553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com