Hugo Silva
Hugo Silva

Reputation: 199

How to use html in radio button choices r shiny

I'm trying to update a radiobutton list on the server side with HTML code but I'm not succeeding.
I'm using this example for you to understand what I'm trying to do. I refer again that has to be on the server side because the elements on my list will be related to other inputs made by the user.

Can someone help to figure out how it can be made? thanks

## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(
radioButtons("rb", "Choose one:",
             choiceNames = list("icon", "html", "text"),
             choiceValues = c(1,2,3)),
 textOutput("txt")
) 


server <- function(input, output,session) {


a<-HTML("<p style='color:red;'>option2</p>")
list1=as.list(c("option1",a,"option3"))

  updateRadioButtons(session, "rb", choiceNames = list1, choiceValues = c(1,2,3))



output$txt <- renderText({
  paste("You chose", input$rb)
})

}

shinyApp(ui, server)
}

Upvotes: 1

Views: 634

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84619

There are some problems in your code:

choiceValues must be an atomic vector, not a list

ui <- fluidPage(
  radioButtons("rb", "Choose one:",
               choiceNames = list("icon", "html", "text"),
               choiceValues = c(1,2,3)),
  textOutput("txt")
)

if you use updateXXX you have to set the argument session to the server function:

server <- function(input, output, session) {

in updateRadioButtons you have to set both choiceNames and choiceValues:

  if(TRUE){  
    list=list(icon("calendar"),
              HTML("<p style='color:red;'>Red Text</p>"),
              "Normal text"
    )
    updateRadioButtons(session, "rb", choiceNames = list, choiceValues = c(1,2,3))
  }

And the icon does not work.

Upvotes: 3

Related Questions