validate() message not rendering in Quarto dashboard

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()
})

```\

I don't have a solution, but it seems to be a plotly issue. If you just create p (i.e. remove ggplotly(p)), and use renderPlot() and plotOutput() you will get the validation/error message.

Thanks for the response! Apparently it is an issue with htmlwidgets. An issue has been submitted here. Hopefully it will be resolved soon!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.