shiny包中的eventReactive()函数无法正常工作

jei2mxaa  于 2023-01-06  发布在  React
关注(0)|答案(1)|浏览(133)

下面,我试图使破折号React在用户输入的变化。有日期范围值,下拉菜单和复选框。当任何默认功能被改变,应该有一个新的图表和相应的结果,由于变化。
下面的代码使用eventReactive()函数创建一个React性组件-"" 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
)

然而,当我调用下面代码中的无功分量时

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

有某种React正在进行,但现在当复选框、数据范围和下拉菜单发生变化时,会产生新的结果。
先谢了
当我试图应用带有闪亮的React成分的正态分布函数时--即一个普通的 Dataframe ,产生的是一个动态的结果,它是无React的。

laik7k3q

laik7k3q1#

我建议您尝试以下选项之一:
1.将geo_plot_tbl移出renderPlotlyReact性模块。

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

1.对数据执行un-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)
})

仅供参考

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

可替换为

coalesce(total_revenue, 0)

为了(imo)更容易阅读。

相关问题