In RStudio, when using flexdashboard, shiny and plotly for a visualization, how can I use tryCatch?

102 Views Asked by At

I have some visualizations plotted in a flexdashboard, but, sometimes when I choose a parameter (for instance "customer group") and it's empty (no records), the visualization will throw an error. I need something like the trycatch() function in order to show a message when the code returns an error, or at least something similar.

Following is the piece of code that corresponds to the graph that throws the error, where should my trycatch() go and with what other parameters?

### {data-width=498}
```{r}

## Serie de tiempo
timeseries88 <- reactive(selected_80() %>% 
  dplyr::group_by(Year, FECHAFACTURA, dia_año)%>%
  dplyr::summarise(VALORNETO = sum(VALORNETO)) %>%
  mutate(text = paste("Año: ", Year, 
                      "\nVenta Neta: $", round(VALORNETO/1000000, 4), " M", 
                      "\nFecha: ", format(FECHAFACTURA, format = "%d/%m/%Y"), sep = "") ,
         fechames = format(FECHAFACTURA, "%d-%m"))%>%
  arrange(FECHAFACTURA)
  )

    renderPlotly({
      
    ggplotly(ggplot(timeseries88(), aes(x = FECHAFACTURA, 
                            y = VALORNETO,
                            group = Year, 
                            text = text))+
  geom_point(size = 0.75, color = color_periodo_unico)+
  geom_line(size = 0.3, color = color_periodo_unico)+
  labs(
    x = "", y = "")+
  theme(
    axis.text.x = element_text(angle = 60, hjust = 1),
    legend.text = element_text(size = 8.5), 
    text = element_text(size = 8.5), 
    axis.ticks.x = element_blank(),
    panel.background = element_blank() ) +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6)),
  tooltip = "text")  %>%
  layout(xaxis = list(autorange = TRUE), 
         yaxis = list(autorange = TRUE),
         showlegend = F, 
           title = list(text = 'Venta de Período', x = 0.05)
  ) %>% config(displayModeBar = F)
})

```

enter image description here

0

There are 0 best solutions below