10

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.

4
  • Ok, when you run that code, what happens?
    – Hackerman
    Sep 16, 2016 at 16:17
  • When I run it, the "a" button (the first value) is already selected. I would like no button to be selected. Sep 16, 2016 at 16:19
  • 1
    You can use selected = character(0) instead of selected = NULL
    – Hackerman
    Sep 16, 2016 at 16:22
  • that works perfectly, thank you. Sep 16, 2016 at 16:26

2 Answers 2

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

17

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),
5
  • 1
    The selected = character(0) alternative is simple but it does seem like a hack. I worry that it might have secondary effects (although I can't think of any now). Sep 16, 2016 at 16:41
  • The closes effect which I could think of is, once the user makes the selection then there is no way by which the user can go back to the initial stage i.e when no radio button was selected. To counter this may be add an extra radio button saying 'None selected' Dec 20, 2017 at 12:33
  • Note that this does not work with awesomeRadio from shinyWidgets (although it does work with radioButtons as stated)
    – nico
    Jun 5, 2020 at 9:29
  • 1
    @nico Yes, this is an almost 4 years old answer...I've been a little bit far away from R development during those years :)
    – Hackerman
    Jun 5, 2020 at 17:14
  • Oh yes, but your answer was still useful!
    – nico
    Jun 6, 2020 at 7:43
1

Assuming that you don't want an initial value because it would be observed, there seem to be 3 solutions:

  • use an extra option, e.g. called 'none' see radioButtons help
  • use character(0) (see radioButtons help)
  • use an extra actionButton ond observe that button in stead.

If you do not want to blur your user interface with an extra radioButtons choice or actionButton, use character(0).

However when using 'character(0)' you may have to resolve a side effect as well. The problem is that 'character(0)' will not reset the input parameter to NULL, so you can't use the same option twice (which may or may not be desirable).This is shown in the following sample program.

server <- function(input, output) {
output$uiRadioButtons <- renderUI({ radioButtons (inputId='actionId', label='action:', choices = c ('a', 'b', 'c'), selected=character(0)) })
n  <- 0
observe({
    actionId <- input$actionId
    n <<- n+1
    if (!is.null(actionId)) {
        if (actionId=='a')  output$action  <- renderText (paste (n, "action A"))
        if (actionId=='b')  output$action  <- renderText (paste (n, "action B"))
        if (actionId=='c')  output$action  <- renderText (paste (n, "action C"))
        output$uiRadioButtons <- renderUI({ radioButtons (inputId='actionId', label='action:', choices = c ('a', 'b', 'c'), selected=character(0)) })
    } else                  output$action  <- renderText ("actionId equals NULL")
}) } 
ui <- fluidPage (
    sidebarLayout(
        sidebarPanel ( uiOutput('uiRadioButtons')),
        mainPanel    (uiOutput('action'))
    ) ) 
shinyApp (ui = ui, server = server)

This can be resolved (though it may be too slow for remote applications) by using and observing a dummy radioButtons as follows.

server <- function(input, output) {
showActions <- function() {
    output$uiRadioButtons <- renderUI ({ radioButtons (inputId='actionId', label='action:', choices = c ('a', 'b', 'c'), selected=character(0)) })
}
showActions()
n  <- 0
observe({
    actionId <- input$actionId
    n <<- n+1
    if (!is.null(actionId)) {
        if (actionId=='dummy')  showActions ()
        else {
            if (actionId=='a')  output$action  <- renderText (paste (n, "action A"))
            if (actionId=='b')  output$action  <- renderText (paste (n, "action B"))
            if (actionId=='c')  output$action  <- renderText (paste (n, "action C"))
            output$uiRadioButtons <- renderUI({ radioButtons (inputId='actionId', label='action:', choices = 'dummy') })
        }
    } else                  output$action  <- renderText ("actionId equals NULL")

})
}
ui <- fluidPage (
    sidebarLayout(
        sidebarPanel (
            # radioButtons (inputId='objectId', label='selct object:', choices = c ('o1', 'o2', 'o3'), inline = TRUE),
            uiOutput('uiRadioButtons')
        ),
        mainPanel    (uiOutput('action'))
    )
)
shinyApp (ui = ui, server = server)

It looks ugly, but it works and I would pleased, if some could show me a better solution, e.g. a way to reset the input variable to NULL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.