简体   繁体   English

闪亮的renderUI selectInput返回NULL

[英]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. 我需要使用renderUI,因为选项列表是动态呈现的(示例中未显示)但是,在初始化期间,selectInput返回NULL而不是默认值。 After that first NULL value the input works as expected. 在第一个NULL值之后,输入按预期工作。 I am new to shiny and might be doing something wrong. 我是新手,可能会做错事。

UPDATE: other controls unexpectedly returned not only NULL, but also NA after initialization. 更新:其他控件意外返回不仅NULL,而且初始化后也返回NA。

See code below. 见下面的代码。 See the console output, the first input returning NULL. 查看控制台输出,第一个输入返回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)

    })

  }))

Shiny has a function called observeEvent which I almost always use instead of observer . Shiny有一个名为observeEvent的函数,我几乎总是使用它而不是observer It basically runs some code only when a reactive value changes, and by default it ignored NULL values. 它基本上仅在无功值更改时运行一些代码,并且默认情况下它忽略NULL值。 So here is the code to make your example work (all I had to do is change your observe({ line to observeEvent(Ar(), { 所以这里是让你的例子工作的代码(我所要做的就是改变你的observe({ line to observeEvent(Ar(), {

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)

    })

  }))

This might work if you use is.null() and return your "default" for input$A: 如果您使用is.null()并返回输入$ A的“默认值”,这可能会有效:

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

   input$A 

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM