R Shiny将分隔符添加到单选按钮列表选项中

use*_*655 5 r radio-button shiny

我有这种单选按钮:

             radioButtons("test", "test:",
                          c("def" = "def",
                          "ghi" = "ghi",
                            "jkl" = "jkl")
Run Code Online (Sandbox Code Playgroud)

但我想添加一个分隔符tag$hr,将def与其他分隔符分开.我试着像这样制作两个列表:

radioButtons("test", "test:",
c("def" = "def"),
tag$hr ,
radioButtons("test", "test:",
c("ghi" = "ghi",
"jkl" = "jkl")
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

谢谢 !

小智 5

根据我的 Shiny 应用程序开发经验,我发现现代浏览器的“检查源”功能非常强大。它可以让您查看网站背后的 HTML/CSS/JS,然后您可以使用它来建模您的工作。Shiny 允许您将 HTML/CSS 直接插入到您的应用程序中。通过运行您提供的 radioButtons 命令,然后使用 Chrome 的开发人员功能,我能够看到构建单选按钮的确切 HTML。然后您可以直接在选项之间插入 hr。我还在头部添加了一个名为 radio 的新 hr 类,因为默认情况下 hr 周围有一些填充,并且与输入框非常相似的灰色。您可能还希望能够在应用程序的其他地方使用常规小时。

可能有一种更简单的方法可以做到这一点,更懂 HTML/CSS 的人可以与他交流。希望这可以帮助。

library(shiny)


ui <- fluidPage(
     tags$head(
          tags$style(HTML(
               "hr.radio {
                    border: 0;
                    border-top: 1px solid #8c8b8b;
                    padding-top: 1;
               }"
          ))
     ),


   titlePanel("New Radio Button Style"),


   sidebarLayout(
      sidebarPanel(HTML("
<div id='test' class='form-group shiny-input-radiogroup shiny-input-container shiny-bound-input'>
     <label class='control-label' for='test'>test</label>
<div class='shiny-options-group'>
     <div class='radio'>
          <label>
               <input type='radio' name='test' value='one' checked='checked'>
               <span>one</span>
          </label>
     </div>
     <hr class ='radio'>
     <div class='radio'>
          <label>
               <input type='radio' name='test' value='two'>
               <span>two</span>
          </label>
     </div>
     <hr class = 'radio'>
     <div class='radio'>
          <label>
               <input type='radio' name='test' value='three'>
               <span>three</span>
          </label>
     </div>
</div>
</div>")

      ),

      # You can see the input$test is still reacting with the radiobuttons
      mainPanel(
         textOutput("selection")
      )
   )
)


server <- function(input, output) {

   output$selection <- renderText({
      input$test
   })
}


shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)


jae*_*555 0

radioButtons("test1", "test:",
c("def" = "def"),selected = NULL),
tags$hr() ,
radioButtons("test2", "test:",
c("ghi" = "ghi",
"jkl" = "jkl"),selected = NULL)
Run Code Online (Sandbox Code Playgroud)

你好,你想要两个单独的单选按钮吗?这是标签$hr,而不是标签$hr。另外,您应该为每个单选框指定唯一的名称。测试1和测试2。

  • 您好,如果电台名称不同,可以一次性选择。 (2认同)