How to put HTML in radioButtons and sliderInput labels ?

545 views
Skip to first unread message

Vincke Grégoire

unread,
Nov 3, 2013, 6:30:44 AM11/3/13
to shiny-...@googlegroups.com
Hi,

I want tu put real HTML tags in labels of radio buttons and sliders in a
ui.R.

Instead of :
radioButtons("truehyp", "Model considered as true :",
list( "H1" = "h1",
"H0" = "h0")),
I have tested :
radioButtons("truehyp", "Model considered as true :",
list( HTML("H<sub>1</sub>") = "h1",
HTML("H<sub>0</sub>") = "h0")),

But it does not work, despite it works with checkboxes :
checkboxInput("alpha", HTML(" &alpha; | "), TRUE),
checkboxInput("showmu0", HTML(" &mu;<sub>0</sub> | "), TRUE),

The same problem exist with sliders :
sliderInput("mu0", HTML(" &mu;<sub>0</sub> : mean of H<sub>0</sub> :")
,min = 1,max = 100,value = 40, step=0.5),
will not render HTML put output it as raw text.

Does someone have a trick to render HTML tags in labels of radio/sliders
inputs ?

Thanks,

Gr�goire

Stéphane Laurent

unread,
Nov 3, 2013, 7:31:32 AM11/3/13
to shiny-...@googlegroups.com
Hello, 

I have just taken a look at sliderInput. We can look what happens in the html code generated by sliderInput() : 

> wrap_ui <- function(x){
   cat(as.character(x))
 }
> wrap_ui(sliderInput("mu0",  HTML("H<sub>0</sub>") 
                                       ,min = 1,max = 100,value = 40, step=0.5))
<div>
  <label class="control-label" for="mu0">H&lt;sub&gt;0&lt;/sub&gt;</label>
  <input id="mu0" type="slider" name="mu0" value="40" class="jslider" data-from="1" data-to="100" data-step="0.5" data-skin="plastic" data-round="false" data-locale="us" data-format="#,##0.#####" data-smooth="false"/>
</div>

The problem is that the label losts its html attribute because of the as.character() function at the beginning:

> sliderInput
function (inputId, label, min, max, value, step = NULL, round = FALSE, 
    format = "#,##0.#####", locale = "us", ticks = TRUE, animate = FALSE) 
{
    labelText <- as.character(label)
    ...........
    tags$div(tagList(controlLabel(inputId, labelText), slider(inputId, 
        min = min, max = max, value = value, step = step, round = round, 
        locale = locale, format = format, ticks = ticks, animate = animate)))
}
<environment: namespace:shiny>

A possible solution is to modify the controlLabel() function appearing at the end :

> shiny:::controlLabel
function (controlName, label) 
{
  tags$label(class = "control-label", `for` = controlName, 
             label)
}

Replacing label with HTML(label) should work. Since controlLabel() is internal to shiny you can proceed as follows :

controlLabel <- function (controlName, label) 
{
  tags$label(class = "control-label", `for` = controlName, 
             HTML(label))
}
assignInNamespace("controlLabel",controlLabel, ns="shiny")

And now it should work for sliderInput : 

> wrap_ui(sliderInput("mu0",  HTML("H<sub>0</sub>") 
                                       ,min = 1,max = 100,value = 40, step=0.5))
<div>
  <label class="control-label" for="mu0">H<sub>0</sub></label>
  <input id="mu0" type="slider" name="mu0" value="40" class="jslider" data-from="1" data-to="100" data-step="0.5" data-skin="plastic" data-round="false" data-locale="us" data-format="#,##0.#####" data-smooth="false"/>
</div>

Stéphane Laurent

unread,
Nov 19, 2013, 2:44:34 PM11/19/13
to shiny-...@googlegroups.com
I have just seen a similar question on stackoverflow http://stackoverflow.com/questions/20079594/r-shiny-how-can-i-format-choices-in-a-a-selectinput-drop-box

Should we report an issue for this point ? It would be better to have the possibility to put html code in widget labels.

JP

unread,
Nov 19, 2013, 10:52:28 PM11/19/13
to shiny-...@googlegroups.com
Hello,
 I posted that question - thanks for you reply, I will try it out, hopefully tomorrow. 

I think it would be great if we could just add the html code directly - how do you report an issue?

Stéphane Laurent

unread,
Nov 20, 2013, 1:30:08 AM11/20/13
to shiny-...@googlegroups.com

Joe Cheng

unread,
Nov 20, 2013, 3:56:58 PM11/20/13
to Stéphane Laurent, shiny-...@googlegroups.com
Yes, please do log an issue, thanks.


On Tue, Nov 19, 2013 at 10:30 PM, Stéphane Laurent <lauren...@yahoo.fr> wrote:

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

JP

unread,
Nov 20, 2013, 9:54:05 PM11/20/13
to shiny-...@googlegroups.com, Stéphane Laurent

Stéphane Laurent

unread,
Feb 7, 2014, 10:54:23 AM2/7/14
to shiny-...@googlegroups.com, Stéphane Laurent
According to this auto-answered question on SO it works by using div(HTML(..)) instead of HTML(...).   

Yihui Xie

unread,
Feb 26, 2014, 1:26:55 AM2/26/14
to Stéphane Laurent, shiny-discuss
Now the HTML() label also works for sliderInput() in the development
version: http://stackoverflow.com/questions/21617462/how-can-i-add-symbols-in-slider-labels

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages