Hi folks,
I am making a Quarto dashboard where users can apply several filters to the dataset and view a resulting leaflet map and ggplotly figure. There are some combinations of filters that will result in all data being filtered (no rows to plot). To deal with this situation, I included
validate(need(nrow(dat()) > 0, "No data. Please adjust filters."))
at the start of each render function.
However, validate() is not giving the behavior I expect. When there is no data, the figure freezes, and no message appears. This is true for renderPlotly() and renderLeaflet(), but renderTable() displays the message properly.
I think the issue is with the quarto dashboard, since when I remove “format: dashboard” from the YAML header, the message renders fine.
I updated R, RStudio, quarto, plotly, and shiny today, but this didn’t help.
Reprex (sorry I had to add \ after each code chunk to escape the markdown formatting):
---
title: "validation() reprex"
server: shiny
format: dashboard
echo: false
---
```{r}
#| context: setup
#| include: false
library(ggplot2)
library(plotly)
library(shiny)
dat <- iris
```\
# Filters {.sidebar width=20% height=100%}
```{r}
uiOutput("width_slider")
```\
# Plot
```{r}
plotlyOutput("fig")
```\
# Table
```{r}
tableOutput("ex_table")
```\
```{r}
#| context: server
output$width_slider <- renderUI({
sliderInput(
"min_width",
label = "Select Min Width",
min = 0, max = 6, value = 0)
})
dat_filt <- reactive({
req(input$min_width)
dat_filt <- dat[which(dat$Sepal.Width > as.numeric(input$min_width)),]
return(dat_filt)
})
output$fig <- renderPlotly({
validate(
need(nrow(dat_filt()) > 0, "No data. Please adjust filters.")
)
p <- ggplot(dat_filt(), aes(Sepal.Width, Sepal.Length)) +
geom_point()
ggplotly(p)
})
output$ex_table <- renderTable({
validate(
need(nrow(dat_filt()) > 0, "No data. Please adjust filters.")
)
dat_filt()
})
```\