【问题标题】:Make sidebar tab selected by default in shiny when using renderUI使用 renderUI 时,默认选中侧边栏选项卡
【发布时间】:2020-02-08 02:23:50
【问题描述】:

我是第一次使用 renderUI。当我运行应用程序时,默认情况下没有选择选项卡;在服务器外部定义 UI 时,通常默认选择第一个选项卡。

知道为什么会发生这种情况,或者如何指定在启动时默认选择第一个选项卡吗?

例子:

library(shiny)
library(shinydashboard)
library(tidyverse)
library(DT)


header <- dashboardHeader(title = "header") 

sidebar <- dashboardSidebar(uiOutput("sidebar"))

body <- dashboardBody(uiOutput("body"))

ui <- dashboardPage(title = 'Radial Networks', header, sidebar, body, skin='blue')

server <- function(input, output, session){

  output$body <- renderUI({
    dashboardBody(
       tabItems(
         tabItem(
           tabName = 'Chords', h2(fluidRow(
             box(plotOutput('plot'), type = 'html',  width = 6, height = '870px')
           )))))})

 output$sidebar <- renderUI({
   dashboardSidebar(sidebarMenu(
     menuItem("Radial Networks", tabName = "Chords", icon = icon("adjust"))))
 })

  output$plot <- renderPlot({
    ggplot(mtcars, aes(x = factor(cyl))) +
      geom_bar()
  })

}

  shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    尝试在 tabItem() 中添加一个参数:

    library(shiny)
    library(shinydashboard)
    library(tidyverse)
    library(DT)
    
    
    header <- dashboardHeader(title = "header") 
    
    sidebar <- dashboardSidebar(uiOutput("sidebar"))
    
    body <- dashboardBody(uiOutput("body"))
    
    ui <- dashboardPage(title = 'Radial Networks', header, sidebar, body, skin='blue')
    
    server <- function(input, output, session){
    
      output$body <- renderUI({
        dashboardBody(
           tabItems(
             tabItem(
               tabName = 'Chords', 
                            h2(fluidRow(box(plotOutput('plot'), 
                            type = 'html',  
                            width = 6, 
                            height = '870px')
               )))))})
    
     output$sidebar <- renderUI({
       dashboardSidebar(sidebarMenu(
                   menuItem("Radial Networks", 
                         tabName = "Chords", 
                         icon = icon("adjust"),
                         selected = 1)))
     })
    
      output$plot <- renderPlot({
        ggplot(mtcars, aes(x = factor(cyl))) +
          geom_bar()
      })
    
    }
    
      shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 这是因为我把你放错了位置。道歉。它需要在侧边栏调用中。我在上面编辑过。
    • 这仍然不起作用,尽管根据此页面:rstudio.github.io/shinydashboard/behavior.html 它应该。我也试过 ```selected = TRUE`` 这也没有效果。这令人困惑
    • 这里也有同样的问题,选中好像没有效果。
    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2019-01-02
    • 2017-05-29
    • 1970-01-01
    相关资源
    最近更新 更多