如何将HTML元素作为单选按钮的标签?

时间:2015-08-23 18:01:05

标签: r shiny

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

我能够为sliderInput创建一个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)

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

1 个答案:

答案 0 :(得分: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)