闪亮的小部件没有出现在侧边栏面板中

Shiny widget not appearing in sidebarPanel

我正在 Shiny 应用程序中使用 shinydashboard::sidebarSearchForm,并且想在它下面放置其他小部件。但是,每当我这样做时,它们都会被推出 sidebarPanel 并进入侧边栏下方的空白区域。在下面的代码中,materialSwitchsidebarPanel 中,但呈现在侧边栏的 外部 中。但是,如果您将 materialSwitch 放在 sidebarPanelsidebarSearchForm 的上方,那么所有小部件都会正确包含在边栏中。我如何确保所有这些小部件都保留在侧边栏中?我希望 materialSwitch 位于侧边栏的底部。谢谢!


library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui <- tagList(
  navbarPage(
    theme = "simplex",
    "App",
    tabPanel("Do something",
             sidebarPanel(
               
               sliderInput("slider", "Select Num:"
                           ,min   = 1
                           ,max   = 10
                           ,sep   = ""
                           ,value = c(1, 10)),
               
               pickerInput(
                 inputId  = "picker",
                 label    = "Select:",
                 selected = "a",
                 choices  = letters,
                 options  = list(
                   `actions-box` = TRUE,
                   size = 10,
                   `selected-text-format` = "count > 3"
                 ),
                 multiple = TRUE
               ),

               h5(HTML("<b>Search</b>")),
               sidebarSearchForm(textId   = "searchText", 
                                 buttonId = "searchButton",
                                 label    = "Include These"
               ),
               
               h5(HTML("<b>Additional Options:</b>")),
               materialSwitch(inputId = "add",
                              label   = "Add?:?", 
                              value   = FALSE, 
                              status  = "primary")
             ),
             
             mainPanel(
               tabsetPanel(
                 tabPanel("Tab",
                          "text"
                 )
               )
             )
    )
  )
)

server <- function(input, output){
}

此行为的原因

问题是由于 sidebarPanelsidebarSearchForm 都创建了 <form> 标签和 <form>tags must not include other <form> tags。例如,在 chrome 中,这种无效的 HTML 已修复,我的猜测是此修复会将元素移出侧边栏。

顺便说一句:sidebarSearchForm 用于 dashboardSidebar(不会创建 <form> 标签。


解决方法

我不是HTML专家,所以我不知道sidebarSearchForm是否需要自己坐<form>(我以前也没有用过这个元素) .

假设没有必要在自己的 <form> 中拥有它,您可以像这样调整 sidebarSearchForm

mySidebarSearchForm <- function (textId, buttonId, label = "Search...", icon = shiny::icon("search")) {
   div(class = "sidebar-form", div(class = "input-group", 
        tags$input(id = textId, type = "text", class = "form-control", 
            placeholder = label, style = "margin: 5px;"), 
        span(class = "input-group-btn", tags$button(id = buttonId, 
            type = "button", class = "btn btn-flat action-button", 
            icon))))
}

(这基本上是原始 sidebarSearchForm 的副本,用 <div> 替换了 <form>

然后 UI 会按应有的方式呈现:

ui <- tagList(
  navbarPage(
    theme = "simplex",
    "App",
    tabPanel("Do something",
             sidebarPanel(
               mySidebarSearchForm(textId   = "searchText", 
                                 buttonId = "searchButton",
                                 label    = "Include These"
               ),
               actionButton("go", "go")

             ), 
             mainPanel(
               tabsetPanel(
                 tabPanel("Tab",
                          "text"
                 )
               )
             )
    )
  )
)

shinyApp(ui, server = function(...) {})