Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

r - Shiny renderUI selectInput returned NULL

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.

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)

    })

  }))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Shiny has a function called observeEvent which I almost always use instead of observer. It basically runs some code only when a reactive value changes, and by default it ignored NULL values. So here is the code to make your example work (all I had to do is change your observe({ line to 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)

    })

  }))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...