search field in shiny navbarPage

后端 未结 3 1275
我在风中等你
我在风中等你 2021-02-11 04:58

I am trying to get a global search field into my navbarPage after some tabPanel. I am not sure if it is possible, since all my tests producing the

3条回答
  •  醉话见心
    2021-02-11 05:27

    In base Shiny you can do it using a tabPanel, if the re-rendering of current tabPanel is not to costly:

    ui <- navbarPage('test',id='test',
                     tabPanel('my app1',
                              titlePanel("Old Faithful Geyser Data1"),
                              sidebarLayout(
                                sidebarPanel(
                                  sliderInput("bins",
                                              "Number of bins:",
                                              min = 1,
                                              max = 50,
                                              value = 30)),
                                mainPanel(plotOutput("distPlot1")))),
                     tabPanel('my app2',
                              titlePanel("Old Faithful Geyser Data2"),
                              sidebarLayout(
                                sidebarPanel(
                                  sliderInput("bins",
                                              "Number of bins:",
                                              min = 1,
                                              max = 50,
                                              value = 30)),
                                mainPanel(plotOutput("distPlot2")))),
                     tabPanel( value= "search_panel",
                               textInput("search", label=NULL, value="Search"))
                    )
    
    server <- function(input, output, session) {
      observe({
        if(!is.null(input$test)){
          if(input$test=="search_panel")     # Go back to last active panel
            updateNavbarPage(session, 'test', selected = selected_panel)
          else                               # Save active panel
            selected_panel <<- input$test
          }
        })
      searchtext <- reactive({
        if(!is.null(input$search))
           if(input$search!="Search")
             return(input$search)
        return(NULL)
        })
      output$distPlot1 <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white', 
             main=ifelse(is.null(searchtext()), "Alt title 1", searchtext()))
        })
      output$distPlot2 <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white', 
             main=ifelse(is.null(searchtext()), "Alt title 2", searchtext()))
        })
    }
    
    shinyApp(ui, server)
    

提交回复
热议问题