search field in shiny navbarPage

后端 未结 3 1271
我在风中等你
我在风中等你 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:19

    You can do this by manipulating the navbar HTML a little bit. Valter is correct - you can achieve this by constructing the menu entirely in HTML instead of using Shiny. But there's an easier way: you can build the navbar in regular Shiny, and then use htmltools to slightly modify it. Here's one quick solution that I think is the cleanest out of the current proposed solutions:

    library(shiny)
    
    navbarPageWithInputs <- function(..., inputs) {
      navbar <- navbarPage(...)
      form <- tags$form(class = "navbar-form", inputs)
      navbar[[3]][[1]]$children[[1]] <- htmltools::tagAppendChild(
        navbar[[3]][[1]]$children[[1]], form)
      navbar
    }
    
    ui <- navbarPageWithInputs(
      "Test app",
      tabPanel("tab1", "tab 1", textOutput("out")),
      tabPanel("tab2", "tab 2"),
      inputs = textInput("search", NULL, placeholder = "Search")
    )
    
    server <- function(input, output, session) {
      output$out <- renderText(input$search)
    }
    
    shinyApp(ui = ui, server = server)
    

    Basically I created a navbarPageWithInputs() function that accepts all the same parameters as navbarPage(), and also an inputs parameter. All this function does is call the regular navbarPage(), and then adds the given inputs to the HTML.

提交回复
热议问题