【问题标题】:selectInput reactive value in shinyDashboard sidebar of R shiny app remains NullR Shiny 应用程序的 shinyDashboard 侧边栏中的 selectInput 反应值仍然为 Null
【发布时间】:2021-01-16 16:39:34
【问题描述】:

似乎 selectInput 对象的输入值没有更新。我在侧边栏菜单中插入了一个。我正在使用闪亮的仪表板。这是我的代码。

标题和侧边栏

header <-
  dashboardHeader(
    title = "REPORT",
    tags$li(class = "dropdown",
            tags$style(
              HTML(
                "@import url('//fonts.googleapis.com/css?family=Libre+Baskerville:400,700|Open+Sans:400,700|Montserrat:400,700');"
              )
            )),
    disable = FALSE,
    titleWidth  = '200'
  )
header$children[[3]]$children[[3]] <-
  tags$h1("DATABASE",
          # align = 'left',
          style = "color:#FFFFFF; font-weight: bold; font-family: 'Open Sans','Libre Baskerville',Montserrat, serif;font-size: 23px;")

data_type_list<-c('in vivo','in vitro','pbpk') 
siderbar <- dashboardSidebar(
    width = 200,
    sidebarMenu(
        id = 'sidebar',
        style = "position: relative; overflow: visible;",
        menuItem(
            "TK Knowlegebase",
            tabName = 'tk',
            icon = icon('database'),
            badgeColor = "teal",
            #radioButtons("tk_data_type", "Select Data Type:",data_type_list)
            selectInput('tk_data_type',"Select Data Type",data_type_list, selected=1)
        )
    )
)

身体

body <- dashboardBody(width = 870,
                      tags$head(
                          tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
                      ),
                      #theme = shinythemes::shinytheme("darkly"),
                      tabItems(
                          tabItem (
                              tabName = "tk",
                              mainPanel(
                    
                                  #in vivo ----
                      conditionalPanel(
                          condition ="input.tk_data_type== 'in vivo'",
                          tags$h2('vivo')
                                  ),
                      
                      # in vitro ----
                      conditionalPanel(
                          condition="input.tk_data_type== 'in vitro'",
                          tags$h2('vitro')
                          )
                       ,
                      #  pbpk ----
                      conditionalPanel(
                          condition="input.tk_data_type== 'pbpk'",
                              tags$h2('pbpk')              
                         )
                    
)))

server = function(input, output, session) {
observe({input$tk_data_type})
}
ui <- dashboardPage(title = 'ARC Toxkin App', skin = 'purple',
                    header, siderbar, body)
shiny::shinyApp(ui = ui, server = server)

我什至尝试在服务器部分使用observe({input$tk_data_type}),但没有成功。

我得到的是一个空白页。我希望看到的是来自 dashboardBody() 的内容出现。

感谢您的宝贵时间

更新 这是一个短期修复解决方案,感谢用户 YBS。

vitro_tabset<-tabsetPanel(
    tabPanel("Detailed",
             "This is a test"),
    tabPanel("Phys-chem",
             "This is a test"),
    tabPanel("Exploratory",
             "This is a test"),
    tabPanel("Downloads",
             "This is a test")
)
vivo_tabset<-tabsetPanel(
    tabPanel("Detailed",
             "This is a test"),
    tabPanel("Phys-chem",
             "This is a test"),
    tabPanel("Exploratory",
             "This is a test"),
    tabPanel("Downloads",
             "This is a test")
)


siderbar <- dashboardSidebar(
    sidebarMenu(
        id = 'sidebar',
        menuItem(
            "TK Knowlegebase",
            tabName = 'tk',
            icon = icon('database'),
            badgeColor = "teal",
            selected = TRUE,
            startExpanded = TRUE,
            #radioButtons("tk_data_type2", "Select Data Type:",data_type_list),
            menuSubItem('vivo', tabName = 'vivo', icon = shiny::icon("angle-double-right"), selected = NULL),
            menuSubItem('vitro', tabName = 'vitro', icon = shiny::icon("angle-double-right"), selected = TRUE),
            menuSubItem('pbpk', tabName = 'pbpk', icon = shiny::icon("angle-double-right"), selected = NULL)
        )
    )
)

body <- dashboardBody(width = 870,
                      tabItems(
                          tabItem (tabName = "vivo",
                                     vivo_tabset),
                          tabItem(tabName='vitro',
                                # in vitro ----
                                vitro_tabset),
                          #  pbpk ----
                          tabItem(tabName='pbpk')

【问题讨论】:

  • 如果有人可以帮助您,请发布一个最低限度可复制的示例。从上面的代码中,无法知道您是如何定义data_type_list。请看这里stackoverflow.com/questions/5963269/…
  • 我很抱歉。我刚刚更新了我的代码。

标签: r shiny shinydashboard reactive


【解决方案1】:

似乎tabItems()tabItem() 在您的程序中不能很好地工作,因为侧边栏菜单需要稍作调整。此外,您不需要mainPanel。试试这个

data_type_list <- c("in vivo","in vitro","pbpk")

ui <- shinydashboard::dashboardPage(title = "ARC Toxkin App",  skin = "purple",
                      shinydashboard::dashboardHeader(
                      title = "REPORT" ,
                      tags$li(class = "dropdown",
                              tags$style(
                                HTML(
                                  "@import url('//fonts.googleapis.com/css?family=Libre+Baskerville:400,700|Open+Sans:400,700|Montserrat:400,700');"
                                )
                              )),
                      disable = FALSE,
                      titleWidth  = '200'
                    ),

                    shinydashboard::dashboardSidebar(width = 220,
                                                     useShinyjs(),
                      sidebarMenu(
                        id = "tabs",
                        style = "position: relative; overflow: visible;",
                        menuItem("TK Knowlegebase", tabName="tk", icon = icon("bar-chart-o"),
                          menuSubItem("TK Knowlegebase1", tabName = "tk1", icon = icon('database')),
                          selectInput("tk_data_type", label="Select Data Type", choices = data_type_list, selected=1),
                          menuSubItem("TK Knowlegebase2", tabName = "tk2", icon = icon('database'))
                          #radioButtons("tk_data_type2", "Select Data Type:",data_type_list)
                        )
                      )
                    ),

                    shinydashboard::dashboardBody( #width = 870,
                      tags$head(
                        tags$link(rel = "stylesheet", type = "text/css", href = "custom.css"),
                      ),
                      #theme = shinythemes::shinytheme("darkly"),
                      tabItems(
                        #tabItem(tabName = "tk", headerPanel('First')),
                        tabItem(tabName = "tk1",
                                fluidRow(
                                  shinydashboard::box(title = "Graphics Package", width = 12, solidHeader = TRUE, status="info",
                                  ## vivo
                                  conditionalPanel(
                                    condition = "input.tk_data_type == 'in vivo'",
                                    tags$h2(' vivo')
                                  ),

                                  ## in vitro
                                  conditionalPanel(
                                    condition = "input.tk_data_type == 'in vitro'",
                                    tags$h2(' vitro')
                                  ),
                                  ##  pbpk
                                  conditionalPanel(
                                    condition = "input.tk_data_type == 'pbpk'",
                                    tags$h2(' pbpk')
                                  ),
                                  verbatimTextOutput("tb1"),
                                  DTOutput("tb2")
                                  
                                ))
                         ),
                         tabItem(tabName = "tk2", headerPanel('Last'), DTOutput("tb3")
                                 #verbatimTextOutput("tb4")
                         )
                    )
      )
)


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

  output$tb3 <- renderDT(mtcars)
  output$tb2 <- renderDT(iris)
  output$tb1 <- renderPrint({input$tk_data_type})
  output$tb4 <- renderPrint({input$tk_data_type2})

}

shinyApp(ui, server)

【讨论】:

  • 感谢您的回复。不幸的是,这是为了工作,我必须拥有 tabItems 和 tabItem。
  • 我绝对同意shinyDashboard在放在sidebar()中时会禁用selectInput
  • 在 selectInput 是一个长列表的情况下,menuSubItems 不是一个好的解决方案。事实是 selectInput 只是由于某种原因在侧边栏中不起作用(输入值为空)
  • 我不确定问题所在,因为它在上面的示例中确实有效。其实你可以把menuSubItem改成menuItem,还是可以的。请确认顺序。如果您使用用户提供的数据来显示选项,那么您需要在服务器端处理 renderUI 并在正文中使用 uiOutput 显示。
  • 我同意这个案子。然而,我确实有另一个应用程序,其中选择菜单有 50 个项目。在这种情况下,即使使用 renderUI 自动生成,生成 30 个子菜单也不现实。我只是希望我知道如何使 selectInput 在侧边栏中工作。
猜你喜欢
  • 2015-05-12
  • 2021-08-09
  • 2021-03-23
  • 2017-08-23
  • 1970-01-01
  • 2017-01-17
  • 2021-09-28
  • 2017-07-15
  • 2019-06-23
相关资源
最近更新 更多