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.
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),
Assuming that you don't want an initial value because it would be observed, there seem to be 3 solutions:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With