Fisseha Berhane
Fisseha Berhane

Reputation: 2653

invalidateLater based on reactive input from uiOutput in shiny results in " arguments imply differing number of rows: 0, 1"

In R Shiny, I want to allow the user to provide the value for invalidateLater() like in the sample code below but it gives "Warning: Error in data.frame: arguments imply differing number of rows: 0, 1". In the code below, even if there is error message, it does not fail. However, in my actual code it causes failure. what really is causing the error?

Note: if I directly put the numericInput() and actionButton() in ur.R, everything goes well. But, I want them to show based on some condition and hence I want to use renderUI() and 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))
            }
    })
  })

Upvotes: 0

Views: 311

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

input$alert_intervalis NULLthe first time you call it. Therefore, alert_interval() will be a numeric(0) and this causes the error in your renderPlot().

You can test if alert_interval() is "ready", by checking its length:

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

Upvotes: 1

Related Questions