【问题标题】:`tabitem` Content Of Conditional `menuitem` Is Showing Only Once in Shiny条件`menuitem`的`tabitem`内容在Shiny中仅显示一次
【发布时间】:2021-05-04 13:20:05
【问题描述】:

我希望 shinydashboard 中的导航 menuItem 是有条件的,并根据 server.R 中评估的条件显示。

为此,我在ui.R 中的常规sidebarMenu 旁边定义了一个conditionalPanel 包含一个menuItem(我使用shinymanager 来验证用户身份):

sidebar <- dashboardSidebar(
  width=280,
  sidebarMenu(id = "sidebarmenu",
              menuItem(...),
              menuItem(...,
                       menuSubItem(...),
                       menuSubItem(...)
                       )
              ),
  conditionalPanel(condition = "output.x === 1",
                   menuItem("title", tabName="tabname")
                   )
  )

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "id",
            fluidPage(
              titlePanel("Hello World")
    )),
    tabItem(tabName="tabname",
            titlePanel("mytitle"),
            fluidPage(
              dataTableOutput(outputId = "table")
              )
    )
)

ui <- dashboardPage(
  dashboardHeader(title = "Hello App", titleWidth=280),
  sidebar,
  body
)

ui <- secure_app(ui)

server.R,我根据登录用户的登录信息切换output.x

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

  # login logic: call the server part, check_credentials returns a function to
  # authenticate users
  res_auth = secure_server(
    check_credentials = check_credentials
  )

  # Define the logon details with a reactive variable
  auth_output <- reactive({
    reactiveValuesToList(res_auth)
  })

  output$x = reactive({
    auth_output()$role
  })

  # Generate a data table from the DB to show conditionally
  conn = ...
  data = load_data(conn, ...)
  disconnect(conn)

  output$table = dt_render({data})

  # All output variables that need to be transferred to the UI should have
  # suspendWhenHidden = FALSE:
  outputOptions(output, "x", suspendWhenHidden = FALSE)

问题:条件表只显示一次,只要我愿意。这一次之后,一旦我离开它,点击条件menuItem 不会显示任何内容。 menuItem 仍然出现,这意味着 output.x === 1 被正确评估,但其内容,即随后的 tabItem,仍然隐藏。

我已经尝试将isolate 分配给output.x,甚至将其固定为1 无济于事。有线索吗?

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    由于conditionalPanel不能放在默认的sidebarMenu里面,它必须在它的独立条件sidebarMenu里面,所以在这种情况下我必须在dashboardSidebar下面定义两个sidebarMenus。下面的修改解决了这个问题:

    sidebar <- dashboardSidebar(
      width=280,
      sidebarMenu(id = "sidebarmenu",
                  menuItem(...),
                  menuItem(...,
                           menuSubItem(...),
                           menuSubItem(...)
                           )
                  ),
      sidebarMenu(id = "conditional_sidebarmenu",
          conditionalPanel(condition = "output.x === 1",
                           menuItem("title", tabName="tabname")
                          )
      )
    

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多