Wednesday, 12 July 2017

r - How can I have HTML elements as labels for radio buttons?



I'm trying to have HTML-formatted choices on the choices of a radioButton element.



I was able to make an HTML-formatted label for a sliderInput as such:




sliderInput("bins",
HTML("Number of binsα1:"),
min = 1,
max = 50,
value = 30)


I was hoping that something like this following code block would work for radioButton




radioButtons("dist", "Distribution type:",
c(HTML("Normal μ1") = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp"))


However, the above code block throws an error. I looked a bit more into how radioButtons executes (https://github.com/rstudio/shiny/blob/8546918cbbc240e23b622d3c5c8181090deb7d62/R/input-utils.R). Eventually, a method called "generateOptions" is called to produce the text labels for the radio buttons.



inputTag <- tags$input(

type = type, name = inputId, value = value
)


If I could replace



value = value


with




value = HTML(value)


I think that might be able to solve my problem. Any ideas for how to proceed?


Answer



Hi eventually you can build your radiobuttons by hand... Like this :



## ui.R
ui <- fluidPage(

# classic radiobuttons
radioButtons(inputId = "dist2", label = "Distribution type:",
choices = list("Normal μ1 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 μ1 )
),
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)

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...