0

Below, I am trying to make a dash reactive to changes in user inputs. There date range values, a dropdown menu and checkbox. When any of the default feature is changed, there should be a new graphs ans correspomding result due to the changes.

The below code uses the eventReactive() function to create a reactive component - "" bike_orderlines_data_filtered_tbl().

bike_orderlines_data_filtered_tbl <- eventReactive(
  eventExpr =  input$apply,
  valueExpr = {
    bike_orderlines_tbl %>%
      filter(order_date %>% between(left  = input$date_range[1], 
                                    right = input$date_range[2])) %>%
      filter(category_1 %in% input$checkbox_category_1) %>%
      filter(category_2 %in% input$picker_category_2)
    # Filtering code goes here
  },
  ignoreNULL = FALSE
)

However, When I call the the reactive componebt in the below codes

renderPlotly(expr = {
 geo_plot_tbl <- reactive({
  bike_orderlines_data_filtered_tbl() %>%
  group_by(state) %>%
  summarise(total_revenue = sum(total_price)) %>%
  ungroup() %>%
  right_join(germany_sf, by = c("state" = "VARNAME_1")) %>%
  mutate(total_revenue = ifelse(is.na(total_revenue), 0, total_revenue)) %>%
  mutate(label_text = str_glue("State: {state}
                               Revenue: {format_to_euro(total_revenue)}")) %>%
  st_as_sf()
})

plot_ly(geo_plot_tbl(),
        split = ~NAME_1,
        color = ~total_revenue,
        colors = "Blues",
        stroke = I("black"),
        hoverinfo = 'text',
        text = ~label_text,
        hoveron = "fills",
        showlegend = FALSE)
})

there is some kind of reactivity going on but now new result is produce when the checkbox, datte range and dropdown menu is change.

Thanks in advance

When I try apply the normal dplyr function with usung shiny reactive component - ie an ordinary dataframe, the is a dynamic resulkl produce which is note reactive.

Mbe45
  • 1
  • 1
  • 1
    do you need eventReactive or some other observer function instead of reactive? – Mike Jan 03 '23 at 21:16
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. It we can't run and test the code ourselves, it's much harder to help you. – MrFlick Jan 03 '23 at 21:22
  • 1
    I don't see why you have a `reactive(.)` _inside_ of your `renderPlotly(.)` I suggest you move your `geo_plot_tbl <- reactive(..)` outside of the renderplotly, then it should work as intended. (I haven't found a place where nesting observe/reactive blocks is useful, necessary, or even that it acts intuitively. I know it often eventually works, but ... I don't see why it's necessary.) – r2evans Jan 03 '23 at 21:32

1 Answers1

1

I suggest you try one of the following options:

  1. Move geo_plot_tbl out of the renderPlotly reactive block.

    geo_plot_tbl <- reactive({
      bike_orderlines_data_filtered_tbl() %>%
      group_by(state) %>%
      summarise(total_revenue = sum(total_price)) %>%
      ungroup() %>%
      right_join(germany_sf, by = c("state" = "VARNAME_1")) %>%
      mutate(total_revenue = ifelse(is.na(total_revenue), 0, total_revenue)) %>%
      mutate(label_text = str_glue("State: {state}
                                   Revenue: {format_to_euro(total_revenue)}")) %>%
      st_as_sf()
    })
    renderPlotly(expr = {
      plot_ly(geo_plot_tbl(),
              split = ~NAME_1,
              color = ~total_revenue,
              colors = "Blues",
              stroke = I("black"),
              hoverinfo = 'text',
              text = ~label_text,
              hoveron = "fills",
              showlegend = FALSE)
    })
    
  2. Un-reactive the data.

    renderPlotly(expr = {
      geo_plot_tbl <- bike_orderlines_data_filtered_tbl() %>%
        group_by(state) %>%
        summarise(total_revenue = sum(total_price)) %>%
        ungroup() %>%
        right_join(germany_sf, by = c("state" = "VARNAME_1")) %>%
        mutate(total_revenue = ifelse(is.na(total_revenue), 0, total_revenue)) %>%
        mutate(label_text = str_glue("State: {state}
                                     Revenue: {format_to_euro(total_revenue)}")) %>%
        st_as_sf()
      plot_ly(geo_plot_tbl,
              split = ~NAME_1,
              color = ~total_revenue,
              colors = "Blues",
              stroke = I("black"),
              hoverinfo = 'text',
              text = ~label_text,
              hoveron = "fills",
              showlegend = FALSE)
    })
    

FYI,

ifelse(is.na(total_revenue), 0, total_revenue)

can be replaced with

coalesce(total_revenue, 0)

for (imo) easier reading.

r2evans
  • 141,215
  • 6
  • 77
  • 149