Brilliant renderUI selectInput returned NULL

I am trying to use a single input reactivity model that affects multiple outputs, as described in a brilliant cheat sheet. I need to use renderUI because the select list is displayed dynamically (not shown in the example) However, during initialization, selectInput returns NULL, not the default value. After this first NULL value, the input works as expected. I'm new to shine and maybe doing something wrong.

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

See code below. See Console Output, the first input returns 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)

    })

  }))
+4
source share
2 answers

, is.null() "default" $A:

A.r <- reactive({
   if(is.null(input$A)) return (1)

   input$A 

})
+1

Shiny observeEvent, observer. NULL. , , (, , observe({ observeEvent(A.r(), {

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 })

    observeEvent(A.r(), { 

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

    })

  }))
+4

Source: https://habr.com/ru/post/1589247/


All Articles