如何在闪亮的仪表板中创建条件renderUI [英] How to create a conditional renderUI in Shiny dashboard

查看:0
本文介绍了如何在闪亮的仪表板中创建条件renderUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过renderMenu创建有条件的侧栏菜单,因为if语句失败。"警告:If:参数长度为零时出错"。

我找到了conditional RenderUI R shinyConditional panel in Shiny dashboard,但都不是我要找的。在这种情况下,条件面板可能会起作用,但从长远来看,我需要能够在服务器端执行此操作。

    if (interactive()) {
  library(ggplot2)
  library(shiny)
  library(shinydashboard)
  library(shinipsum)

  ui <- dashboardPage(
    header = dashboardHeader(),
    dashboardSidebar(
      sidebarMenuOutput("plotDataVHA"),
      sidebarMenuOutput("tabSelector")
    ),
    dashboardBody(tabItems(
      tabItem(tabName = "facilities",
              fluidRow(box(
                uiOutput("selectedFacilityTime")
              ))),
      tabItem(tabName = "service",
              fluidRow(box(
                uiOutput("selectedFacilityYyCases")
              )))
    ))
  )

  server <- function(input, output) {
    output$renderedSelectedFacilityTime <- renderPlot({
      random_ggplot(type = "line")
    })
    output$selectedFacilityTime <- renderUI({
      plotOutput("renderedSelectedFacilityTime")
    })

    output$renderedFacilityYyCases <- renderPlot({
      random_ggplot(type = "bar")
    })
    output$selectedFacilityYyCases <- renderUI({
      plotOutput("renderedFacilityYyCases")
    })

    output$tabSelector <- renderMenu({
      sidebarMenu(id = "test",
                  menuItem(
                    text = "Chart data",
                    menuSubItem(
                      text = "Facilities",
                      tabName = "facilities",
                      selected = TRUE
                    ),
                    menuSubItem(
                      text = "Service & Specialty",
                      tabName = "service",
                      icon = NULL
                    )
                  ))
    })

    output$plotDataVHA <- renderMenu({
      if (input$test == "facilities") {
      sidebarMenu(
        menuItem(
          text = "VHA data",
          menuSubItem(
            text = "None",
            selected = TRUE,
            icon = NULL
          ),
          menuSubItem(text = "Mean", icon = NULL)
        )
      )
    }
     })
  }

  shinyApp(ui, server)
}

正常工作时,只有选择子菜单"Facilities"时,菜单"VHA Data"才可见。

推荐答案

有趣的问题。出现argument is of length zero错误的原因是通过renderMenu()在服务器端呈现两个菜单。因此,当应用程序启动时,input$test没有赋值。您可以使用req()来避免这种情况,req()将仅在input$test启动后评估input$test == "facilities"测试。

现在,为了使菜单仅在选择另一个子菜单时显示,您需要独立于renderMenu()创建菜单。最好在正常的reactive()中评估条件,然后将此反应函数作为输入传递给renderMenu()。最后,要在input$test == "facilities"FALSE时删除菜单,可以呈现一个空的html容器。

以下是更新后的代码:

  library(ggplot2)
  library(shiny)
  library(shinydashboard)
  library(shinipsum)

  ui <- dashboardPage(
    header = dashboardHeader(),
    dashboardSidebar(
      sidebarMenuOutput("plotDataVHA"),
      sidebarMenuOutput("tabSelector")
    ),
    dashboardBody(tabItems(
      tabItem(tabName = "facilities",
              fluidRow(box(
                uiOutput("selectedFacilityTime")
              ))),
      tabItem(tabName = "service",
              fluidRow(box(
                uiOutput("selectedFacilityYyCases")
              )))
    ))
  )

  server <- function(input, session, output) {
    output$renderedSelectedFacilityTime <- renderPlot({
      random_ggplot(type = "line")
    })
    output$selectedFacilityTime <- renderUI({
      plotOutput("renderedSelectedFacilityTime")
    })

    output$renderedFacilityYyCases <- renderPlot({
      random_ggplot(type = "bar")
    })
    output$selectedFacilityYyCases <- renderUI({
      plotOutput("renderedFacilityYyCases")
    })

    output$tabSelector <- renderMenu({
      sidebarMenu(id = "test",
                  menuItem(
                    text = "Chart data",
                    menuSubItem(
                      text = "Facilities",
                      tabName = "facilities",
                      selected = TRUE
                    ),
                    menuSubItem(
                      text = "Service & Specialty",
                      tabName = "service",
                      selected = FALSE,
                      icon = NULL
                    )
                  ))
    })

    make_menu <- reactive({
      cat("Current submenu selected: ", input$test, "

")

      if (req(input$test) == "facilities") {
      sidebarMenu(
        menuItem(
          text = "VHA data",
          menuSubItem(
            text = "None",
            selected = TRUE,
            icon = NULL
          ),
          menuSubItem(text = "Mean", icon = NULL)
        )
      )
      } else {
        # return an empty HTML container
        div()
      } 
    })

    output$plotDataVHA <- renderMenu({
        make_menu()
    })

  }

  shinyApp(ui, server)

这篇关于如何在闪亮的仪表板中创建条件renderUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆