R Shiny Dashboard 在初始化时不会在 sidebarMenu 内加载渲染的 UI [英] R Shiny Dashboard does not load rendered UI inside of sidebarMenu on initialization

查看:20
本文介绍了R Shiny Dashboard 在初始化时不会在 sidebarMenu 内加载渲染的 UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户希望提供的输入方式生成动态的 UI 元素.为简单起见,我正在使用 Shiny Dashboard,但我遇到了 sidebarMenu 的问题.以前,当我将静态 UI 元素直接放入 sidebarMenu 时,我没有问题,但是当尝试将动态 UI 元素放入 sidebarMenu 时,我遇到了问题.我使用的是 R 3.3.2 和 Shiny 1.0.0 以及仪表板 0.5.3.

I'm trying to generate a UI element that is dynamic based on how the user wishes to provide their input. I'm using the Shiny Dashboard for simplicity but I have encountered an issue with the sidebarMenu. Previously when I was placing static UI elements directly into the sidebarMenu, I had no issue, however when trying to place dynamic UI elements into the sidebarMenu I have problems. I'm using R 3.3.2 and Shiny 1.0.0 and Dashboard 0.5.3.

我遇到的具体问题是,当程序第一次加载时,动态 UI 元素没有加载.代码中似乎没有任何阻碍,因为即使在卸载动态 UI 时,界面的所有功能也能正常工作.我可以通过选择导航栏中的选项卡之一或将鼠标悬停在我已实现工具提示的内容上来加载动态 UI.

The specific problem I have is that when the program first loads up, the dynamic UI elements do not load. There does not appear to be any holdup in the code, as all the features of the interface work fine even while the dynamic UI is unloaded. I am able to get the dynamic UI to load by selecting one of the tabs in the navbar, or by hovering over something that I have implemented a tooltip on.

我无法提供确切的代码,但我重新创建了一个小得多的可重现示例,该示例具有与我的较大版本相同的所有问题.

I am unable to provide the exact code but I have recreated a much smaller reproducible example that has all the same problems that my larger version does.

library("shiny")
library("shinydashboard")

header = dashboardHeader(
    title = "Dynamic UI Example"
)
sidebar = dashboardSidebar(
    sidebarMenu(
        menuItemOutput("dynamic_sidebar")
    )
)
body = dashboardBody(
    tabBox(
        tabPanel(
            strong("One")
        ),
        tabPanel(
            strong("Two")
        )
    )
)
ui = dashboardPage(header, sidebar, body)

server = shinyServer(function(input,output,session){
output$dynamic_sidebar = renderMenu({
        sidebarMenu(
            menuItem(
                "Slider or numeric problem",
                radioButtons("slider_or_numeric",
                   label = "Slider or Numeric Input",
                   choices = c("Slider", "Numeric"),
                   selected = "Slider",
                   inline = TRUE
                ),
                uiOutput("input")
            )     
        )
    })
    output$input = renderUI({
        if (input$slider_or_numeric == "Slider"){
            sliderInput("slider", 
                label = "slider",
                min = 0, max = 1,
                value = 0
            )
        } else {
            numericInput("numeric", 
                label = "numeric",
                min = 0, max = 1,
                value = 0
            )
        }
    })
})

shinyApp(ui, server)

要验证问题,加载后只需打开菜单项,您将看到单选按钮,但没有其他内容.将导航栏上的选项卡从 1 切换到 2,输入应出现在菜单中(必须在菜单打开时完成).

To verify the problem, after loading it up just open the menu item and you'll see the radio buttons but nothing else. Switch the tab on the navbar from one to two, and the input should appear in the menu (must be done while the menu is open).

我真的只是在这里抓紧了问题,我已经解决了几个小时的问题,我认为这只是与这些功能的不兼容.我真的希望有人能证明我错了,告诉我我只是做错了.我已经为我的主程序找到了替代方案,但它们与我在这里尝试完成的工作没有相同的美感.

I'm really just grasping at straws here, I have been troubleshooting this for hours and I think it's just an incompatibility with these features. I'm really hoping someone can prove me wrong and show me that I'm just doing it wrong. I've already found alternatives for my main program but they don't have the same aesthetic with what I'm trying to accomplish here.

感谢所有帮助!

推荐答案

所以我从另一个 SO 帖子中找到了解决方案:r 闪亮 - uiOutput 未在 menuItem 内呈现,我有点惭愧,我没有在我的第一组搜索工作中找到它.总结一下帖子说的更详细,我在menuItem里面渲染的UI元素被初始化为隐藏的,隐藏的项目默认是暂停的.作为帖子详细信息,您可以通过 outputOptions 函数更改此选项.这是一个更新版本,完全符合我最初的要求:

So I have found a solution from another SO post: r shiny - uiOutput not rendering inside menuItem and I'm slightly ashamed I did not manage to find this in my first set of search efforts. To sum up what the post says in more detail, the UI element I am rendering inside the menuItem is initialized as hidden, and hidden items are suspended by default. As the post details, you can change this option via the outputOptions function. Here is an updated version that works exactly as I wanted it to originally:

library("shiny")
library("shinydashboard")

header = dashboardHeader(
    title = "Dynamic UI Example"
)
sidebar = dashboardSidebar(
    sidebarMenu(
        menuItemOutput("dynamic_sidebar")
    )
)
body = dashboardBody(
    tabBox(
        tabPanel(
            strong("One")
        ),
        tabPanel(
            strong("Two")
        )
    )
)
ui = dashboardPage(header, sidebar, body)

server = shinyServer(function(input,output,session){

    output$input <- renderUI({})
    outputOptions(output, "input", suspendWhenHidden = FALSE)

output$dynamic_sidebar = renderMenu({
        sidebarMenu(
            menuItem(
                "Slider or numeric problem",
                radioButtons("slider_or_numeric",
                   label = "Slider or Numeric Input",
                   choices = c("Slider", "Numeric"),
                   selected = "Slider",
                   inline = TRUE
                ),
                uiOutput("input")
            )     
        )
    })
    output$input = renderUI({
        if (input$slider_or_numeric == "Slider"){
            sliderInput("slider", 
                label = "slider",
                min = 0, max = 1,
                value = 0
            )
        } else {
            numericInput("numeric", 
                label = "numeric",
                min = 0, max = 1,
                value = 0
            )
        }
    })
})

shinyApp(ui, server)

请注意,有必要在设置它的选项之前初始化输出对象,否则它将为空并导致错误.

Note that it is necessary to initialize the output object BEFORE setting it's options or else it would be null and cause an error.

感谢 Mike 尝试回答这个问题,希望如果其他人偶然发现这个问题,他们会发现这个解决方案很有用.

Thanks to Mike for attempting to answer the question, hopefully if someone else stumbles onto this problem they'll find this solution useful.

这篇关于R Shiny Dashboard 在初始化时不会在 sidebarMenu 内加载渲染的 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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