简体   繁体   English

来自 shiny package 的 eventReactive() function 无法正常工作

[英]The eventReactive() function from the shiny package is not working rightly

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().下面的代码使用 eventReactive() function 创建一个反应组件 - "" 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.当我尝试将普通的 dplyr function 与 usung shiny 反应性组件一起使用时 - 即普通的 dataframe,这是一种动态结果,它是音符反应性的。

I suggest you try one of the following options:我建议您尝试以下选项之一:

  1. Move geo_plot_tbl out of the renderPlotly reactive block.geo_plot_tbl移出renderPlotly反应块。

     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.reactive数据。

     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.为了(imo)更容易阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM