【问题标题】:R Shiny create menuItem after object is created/button clickedR Shiny在创建对象/单击按钮后创建menuItem
【发布时间】:2017-08-22 06:33:08
【问题描述】:

我正在尝试在创建对象或单击按钮(理想情况下是对象)时动态生成 menuItem。我尝试了多种方法,但似乎无法找到一个干净、有效的解决方案。

我有很多代码,所以下面将包括示例代码:

ui <- fluidPage(
    dashboardPage(
        dashboardHeader(title = "text"),
        dashboardSidebar(
            sidebarMenu(id = 'MenuTabs',
                        menuItem("Tab1", tabName = "tab1", selected = TRUE)
                        # menuItem("Tab1", tabName = "tab2")
            )
        ),
        dashboardBody(
            tabItems(
                tabItem("tab1",
                        actionButton("newplot", "New plot")),
                tabItem("tab2",
                        plotOutput('Plot'))
             )
        )
    )
)


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

    output$Plot <- renderPlot({
        input$newplot
        cars2 <- cars + rnorm(nrow(cars))
        plot(cars2)
    })

}


shinyApp(ui, server)

上面我有 2 个选项卡,1 个带有按钮(显示),另一个带有绘图(隐藏)。

  • 如何在单击按钮时显示带有绘图的隐藏选项卡?
  • 对于奖励积分,假设按钮创建了一个对象,我如何在创建所述对象的情况下显示隐藏的 menuItem

谢谢

【问题讨论】:

标签: r shiny shinydashboard


【解决方案1】:

我已经设法解决了。下面是通过按 show 按钮创建 menuItem 的代码。

ui <- fluidPage(
    dashboardPage(
        dashboardHeader(title = "text"),
        dashboardSidebar(
            sidebarMenu(id = 'MenuTabs',
                        menuItem("Tab1", tabName = "tab1", selected = TRUE),
                        # menuItem("Tab1", tabName = "tab2")
                        uiOutput('ui')
            )
        ),
        dashboardBody(
            tabItems(
                tabItem("tab1",
                        actionButton("newplot", "New plot"),
                        actionButton("show", "Show")),
                tabItem("tab2",
                        plotOutput('Plot'))
             )
        )
    )
)


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

    output$Plot <- renderPlot({
        input$newplot
        # Add a little noise to the cars data
        cars2 <- cars + rnorm(nrow(cars))
        plot(cars2)
    })


    output$ui <- renderUI({
        if(input$show == 0) return()
        print(input$show)
        sidebarMenu(id = 'MenuTabs',
                    menuItem("Tab1", tabName = "tab2")
        )
    })
}


shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2022-12-07
    • 1970-01-01
    • 2012-05-09
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    相关资源
    最近更新 更多