Shiny renderUI selectInput returned NULL

2,000 views
Skip to first unread message

Eduardo Bergel

unread,
May 22, 2015, 4:20:11 PM5/22/15
to shiny-...@googlegroups.com
Sorry (newbi here), I posted this to Stack Overflow (http://stackoverflow.com/questions/30378310/shiny-renderui-selectinput-returned-null), 
but then discover this group, which I think is a better fit for this question. 


I am trying to use a reactivity model with one input affecting several outputs as describe in the shiny cheat sheet. I need to use renderUI because the choices list is rendered dynamically (not shown in the example) However, during initialization selectInput returns NULL rather than the default value. After that first NULL value the input works as expected. I am new to shiny and might be doing something wrong.

This can be resolved by catching the NULL with an if  return statement, but there might be a better solution.

UPDATE: other controls unexpectedly returned not only NULL, but also NA after initialization.

See code below. See the console output, the first input returning NULL.

Listening on http://127.0.0.1:6211
 NULL
 chr "1"
 chr "2"
 chr "1"




library(shiny)

runApp(list(

  ui = bootstrapPage(
    fluidPage( uiOutput('ui.A')   )
  ),


  server = function(input, output){

    output$ui.A = renderUI({
      selectInput("A", label = h4("input A"), 
                  choices = list(A_1=1, A_2=2), 
                  selected = 1)
    })

    A.r <- reactive({input$A })

    observe({ 

      A <- A.r()
      str(A)

    })

  }))

jrdn...@gmail.com

unread,
May 28, 2015, 2:33:45 PM5/28/15
to shiny-...@googlegroups.com
I've had a similar problem, but with inputs rendered within conditionalPanel().  Affected inputs show up as NULL when accessed in R when the application is first run but forcing a recalculation by changing any input causes the problem to go away.

http://stackoverflow.com/questions/30440859/sliders-providing-null-values-to-complex-shiny-application-at-startup-and-period

Joe Cheng

unread,
May 28, 2015, 5:31:27 PM5/28/15
to jrdn...@gmail.com, shiny-...@googlegroups.com
Eduardo,

This is expected behavior. When the client initially connects, the selectInput "A" does not exist.

However, you can add the line validate(need(input$A, message=FALSE)) to the beginning of the A.r reactive and it should solve this problem, by terminating the operation before it starts.

jrdnmdhl,

I'm not sure why the sliders would sometimes be NULL, conditionalPanel isn't supposed to have that effect. If you have a repro case it'd be super helpful. But you can use validate(need(input$xxxxx, message=FALSE)) too, in any reactive expression or output or observer where you don't want to proceed unless input$xxxxx is non-NULL.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/67af8c5e-ca92-44bc-8530-9cc5b4223619%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

jrdn...@gmail.com

unread,
May 28, 2015, 11:28:46 PM5/28/15
to shiny-...@googlegroups.com, jrdn...@gmail.com
Using validate/need I get the following:

Error in UseMethod("validate") :
  no applicable method for 'validate' applied to an object of class "NULL"

Haven't had the time to put together an example yet but will do so as soon as I get the opportunity.

jrdn...@gmail.com

unread,
May 28, 2015, 11:31:56 PM5/28/15
to shiny-...@googlegroups.com
I found my problem with validate based on a previous post.  For those with the same problem, validate is being masked by another package.  To resolve issue use shiny::validate instead of just validate.

nitmv

unread,
Jul 3, 2015, 10:13:16 AM7/3/15
to shiny-...@googlegroups.com, jrdn...@gmail.com
I have a similar structure of code. I tried using the 'validate' and 'need' but my shiny app does not respond. I tried to debug but am not able to find a solution. Please help if I am missing any functionality.

output$select_class <- renderUI({
        selectInput(inputId = 'selected_class', label = 'Select a class', choices = as.list(classes))
})

students_count_class <- reactive({
        validate(need(input$selected_class, message = F))
        StudentsInClass <- StudentsPerClass %>% filter(class == input$selected_class) # filter data table using selected input
        sum(StudentsInClass$count) # sum the students count
})

output$students_in_class <- renderValueBox({
        valueBox(students_count_class(), 'Students') # display count in a value box        
})



Reply all
Reply to author
Forward
0 new messages