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.
selected = character(0)instead ofselected = NULL