Shiny renderUI selectInput returned NULL

后端 未结 2 635
南笙
南笙 2020-12-02 00:17

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 rende

相关标签:
2条回答
  • 2020-12-02 00:54

    This might work if you use is.null() and return your "default" for input$A:

    A.r <- reactive({
       if(is.null(input$A)) return (1)
    
       input$A 
    
    })
    
    0 讨论(0)
  • 2020-12-02 01:00

    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)
    
        })
    
      }))
    
    0 讨论(0)
提交回复
热议问题