【问题标题】:How can I have HTML elements as labels for radio buttons?如何将 HTML 元素作为单选按钮的标签?
【发布时间】:2015-11-17 03:15:23
【问题描述】:

我正在尝试对 radioButton 元素的选择进行 HTML 格式的选择。

我能够为滑块输入制作 HTML 格式的标签:

sliderInput("bins",
HTML("Number of <u>bins</u>&alpha;<sub>1</sub>:"),
min = 1,
max = 50,
value = 30)

我希望像下面这样的代码块适用于 radioButton

radioButtons("dist", "Distribution type:",
c(HTML("Normal &mu;<sub>1</sub>") = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp"))

但是,上面的代码块会引发错误。我更深入地研究了 radioButtons 的执行方式 (https://github.com/rstudio/shiny/blob/8546918cbbc240e23b622d3c5c8181090deb7d62/R/input-utils.R)。最终,调用名为“generateOptions”的方法来为单选按钮生成文本标签。

inputTag <- tags$input(
type = type, name = inputId, value = value
)

如果我可以替换

value = value

value = HTML(value)

我认为这可能能够解决我的问题。关于如何进行的任何想法?

【问题讨论】:

标签: r shiny


【解决方案1】:

您好,最终您可以手动构建单选按钮...像这样:

## ui.R
ui <- fluidPage(
  # classic radiobuttons
  radioButtons(inputId = "dist2", label = "Distribution type:",
               choices = list("Normal &mu;<sub>1</sub" = "rnorm", "Uniform" = "runif")),
  br(),
  # custom radiobuttoms
  tags$div(
    id="dist", class="form-group shiny-input-radiogroup shiny-input-container",
    tags$label(class="control-label", `for`="dist", "Distribution type:"),
    tags$div(class="shiny-options-group",
             tags$div(class="radio",
                      tags$label(
                        tags$input(type="radio", name="dist", value="rnorm", checked="checked",
                                   tags$span(HTML("Normal &mu;<sub>1</sub")))
                      )
             ),
             tags$div(class="radio",
                      tags$label(
                        tags$input(type="radio", name="dist", value="runif",
                                   tags$span(HTML("Uniform")))
                      )
             )
    )
  ),
  verbatimTextOutput(outputId = "test")
)
## server.R
server <- function(input, output) {
  output$test <- renderPrint({
    print("id = dist2")
    print(input$dist2)
    print("")
    print("id = dist")
    print(input$dist)
  })
}
# launch app
shinyApp(ui = ui, server = server)

【讨论】:

  • 哇!谢谢你。这比我预期的要优雅得多。
猜你喜欢
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 2012-10-27
相关资源
最近更新 更多