使用renderUI时,默认情况下使侧边栏选项卡处于闪亮状态

时间:2019-10-10 15:23:30

标签: r shiny shinydashboard

我第一次使用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)

1 个答案:

答案 0 :(得分:0)

尝试在您的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)