Gopala
Gopala

Reputation: 10483

Shiny validate not working on zero row data frame?

I have the following code....for some reason, validate does not kick in and the plot command runs with an error. What am I missing?

library(shiny)
df <- data.frame(x = 1, y = 1)

ui <- fluidPage(
  plotOutput('plot')
)

server <- function(input, output) {
  output$plot <- renderPlot({
    df <- df[-1, ]
    validate(
      need(nrow(df),
           "Not Enough Data")
    )
    plot(df$x, df$y)
  })
}

shinyApp(ui = ui, server = server)

Everything from the data frame perspective looks fine:

df <- data.frame(x = 1, y = 1)
df
  x y
1 1 1

df <- df[-1, ]
df
[1] x y
<0 rows> (or 0-length row.names)

nrow(df)
[1] 0

names(df)
[1] "x" "y"

Upvotes: 3

Views: 1208

Answers (1)

Carl
Carl

Reputation: 5779

Does this help?

shiny::need(0, "Won't say anything")
shiny::need(NULL, "Will say something")
shiny::need(FALSE, "Will say something")

Try:

need(nrow(data)>0)

Upvotes: 3

Related Questions