2
votes

The app below contains a radio button group labelled Options and a renderUI expression that renders:

  1. an input element input$data which depends on the value of the selected button and
  2. an actionButton input$add.

I would like to disable the button until the user selects a valid value for input$data. I tied observe({ toggleState(id = 'add', condition = !is.null(input$data)) }) but this fails. Printing the value of input$data to the console shows that the pickerInput initializes with two separate values NULL and "":

NULL
[1] ""

and so !is.null(input$data) returns:

[1] FALSE
[1] TRUE

Instead of just FALSE.

The app:

library(shiny)
library(shinyjs)
library(shinyWidgets)


ui <- fluidPage(shinyjs::useShinyjs(),

                prettyRadioButtons('opt', label = 'Options', choices = c('state', 'file', 'letters')),

                uiOutput('upload')

)

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

  output$upload = renderUI({

    tagList(
      switch(input$opt, 
           'state' = pickerInput('data', 'Choose a state', 
                                 choices = state.name, 
                                 options = list(title = "States..."),
                                 choicesOpt = list(subtext = seq_along(state.name))
                                 ), 

           'file' = fileInput('data', 'Select file'), 

           'letters' = pickerInput('data', 'Choose a letter', 
                                   choices = LETTERS, 
                                   options = list(title = "Letters..."),
                                   choicesOpt = list(subtext = seq_along(LETTERS))
                                   )
           ), 

      actionButton('add', 'Add')

      )

  })

  observe({

    print(input$data) # pickerInput initializes with two values
    toggleState(id = 'add', condition = !is.null(input$data))

  })


})

shinyApp(ui, server)

Also, when you switch the radio button from the default selection of state to file and then back to state, the pickerInput returns just [1] "" (not the

NULL
[1] ""

that it returns on startup). I'm not sure what's going on here and I couldn't find anything relating to this in the documentation for pickerInput so any help would be greatly appreciated.

1

1 Answers

2
votes

An empty string '' isn't a null object in R

?is.null

NULL represents the null object in R: it is a reserved word. NULL is often returned by expressions and functions whose values are undefined.

> !is.null('')
[1] TRUE

However shiny::isTruthy will solve this

> isTruthy('')
[1] FALSE