【问题标题】:invalidateLater based on reactive input from uiOutput in shiny results in " arguments imply differing number of rows: 0, 1"invalidateLater 基于来自 uiOutput 的反应性输入,在“参数暗示不同的行数:0、1”中的闪亮结果
【发布时间】:2017-07-19 19:39:42
【问题描述】:

在 R Shiny 中,我想允许用户像下面的示例代码一样提供 invalidateLater() 的值,但它给出“警告:data.frame 中的错误:参数暗示不同的行数:0、1” .在下面的代码中,即使有错误信息,它也不会失败。但是,在我的实际代码中,它会导致失败。真正导致错误的原因是什么?

注意:如果我直接将 numericInput() 和 actionButton() 放在 ur.R 中,一切顺利。但是,我希望它们根据某些条件显示,因此我想使用 renderUI() 和 uiOutput()

ui.R

library(shiny)

shinyUI(fluidPage(

    checkboxInput('refresh',em("Refresh"),FALSE),
    uiOutput("interval_update"),
    uiOutput("go_refresh"),
    plotOutput("plot")

 ))

server.R

 library(shiny)

 shinyServer(function(input, output) {

    output$interval_update=renderUI({
            if(input$refresh==TRUE){
                    numericInput("alert_interval", em("Alert Interval   (seconds):"),5 ,width="200px")
            }
    })

    output$go_refresh=renderUI({
            if(input$refresh==TRUE){
                    actionButton("goButton", em("Update"))
            }
    })

    alert_interval = reactive({
            input$goButton
            z=isolate(input$alert_interval)
            z=z*1000
            z
    })


    output$plot <- renderPlot({
            if(input$refresh==TRUE){
                    invalidateLater(alert_interval())
                    hist(rnorm(1000))
            }
    })
  })

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    input$alert_intervalNULL第一次调用它。因此,alert_interval() 将是 numeric(0),这会导致您的 renderPlot() 出现错误。

    您可以通过检查其长度来测试alert_interval() 是否“就绪”:

     output$plot <- renderPlot({
        if(input$refresh==TRUE & length(alert_interval())){
          ...    
        }
      })
    

    【讨论】:

    • 非常感谢!在 numericInput("alert_interval", em("Alert Interval (seconds):"),5 ,width="200px") 中,我提供了 5 秒作为默认值。为什么我第一次调用它时它是空的?
    猜你喜欢
    • 2021-11-25
    • 2015-02-04
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    相关资源
    最近更新 更多