raistlin raistlin - 2 months ago 24
R Question

R Shiny - No initial value selected in radioButtons?

From the documentation:


radioButtons(inputId, label, choices, selected = NULL, inline = FALSE,
width = NULL)

Arguments

selected The initially selected value (if not specified then defaults to the first value)


But, can you specify the
selected
argument so that the rendered radioButtons has no initial value selected? The output of this initial value could be an empty string (
""
). In the example below, neither "a" nor "b" should be selected when the app is run.

library(shiny)

runApp(
list(
ui = shinyUI(
fluidPage(
radioButtons("test","test",choices=c("a","b"),selected = NULL),
verbatimTextOutput("value")
)
), server = shinyServer(function(input, output,session) {
output$value <- renderText(input$test)
})
)
)


UPDATE

As sugested by Hackerman,
selected = character(0)
does the trick.

Answer

The problem with selected is that if not specified then defaults to the first value. But you can workaround this using the following:

selected = character(0)

Another way is providing a default option, like Nothing Selected

radioButtons("test","test",choices=c("Nothing Selected"="","a"="a","b"="b"),selected = NULL),
Comments