条件navbarPage菜单

时间:2017-08-01 17:39:44

标签: r shiny

我有一个关于基于服务器端变量的navbarMenu条件生成的问题。我创建了一个小型演示应用来说明它。

ui = shinyUI(
    navbarPage(title = "Demo app",
               navbarMenu("Small numbers",
                          tabPanel("First small page", uiOutput("firstSmallPage"))
               ),
               navbarMenu("Big numbers",
                          tabPanel("First big page", uiOutput("firstBigPage"))
               )
    )
)

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

    rand_num = sample(1:10)[1]
    # if rand_num is higher than 5 I dont want Big number navbarMenu to appear
    print(rand_num)

    output$firstSmallPage <- renderUI({
        plotOutput("smallPlot")
    })
    output$smallPlot <- renderPlot({plot(1:10)})

    output$firstBigPage <- renderUI({
        plotOutput("bigPlot")
    })
    output$bigPlot <- renderPlot({plot(990:1000)})
})

app = shinyApp(ui=ui, server=server)

如果rand_num更高,我尝试归档的是隐藏Big Numbers标签5.我尝试在服务器端navbarMenu中包装renderUI并将其替换为{ {1}}在ui-side但是没有成功。使用超过2 uiOutput s的解决方案至关重要。提前致谢

1 个答案:

答案 0 :(得分:0)

似乎这样工作:

library(shiny)
library(shinyjs)
ui = shinyUI(
  navbarPage(
    useShinyjs(),
    title = "Demo app",
    navbarMenu("Small numbers",
               tabPanel("First small page", uiOutput("firstSmallPage"))
    ),
    navbarMenu("Big numbers", 
               tabPanel("First big page", uiOutput("firstBigPage"))
    )

  )
)

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

  rand_num = sample(1:10)[1]
  # if rand_num is higher than 5 I dont want Big number navbarMenu to appear
  print(rand_num)
  if(rand_num>5){
    hide(selector = ".navbar-nav li:nth-child(3)")
  }

  output$firstSmallPage <- renderUI({
    plotOutput("smallPlot")
  })
  output$smallPlot <- renderPlot({plot(1:10)})

  output$firstBigPage <- renderUI({
    plotOutput("bigPlot")
  })
  output$bigPlot <- renderPlot({plot(990:1000)})
})

app = shinyApp(ui=ui, server=server)
runApp(app)