【问题标题】:How to use a reactive list with reactive dataframe and R Highcharter in Shiny App如何在 Shiny App 中使用带有反应数据框和 R Highcharter 的反应列表
【发布时间】:2019-05-31 11:31:53
【问题描述】:

我正在编写一个闪亮的应用程序,它将按付款日期生成一个堆叠列 HighChart of Payments,按部门堆叠。在非反应性设置中,这是通过将数据集传递给列表来完成的,将列表传递给 highcharter 中的add_series_list 并制作堆叠柱形图。在响应式设置中,这不会生成图表。

这是对图表的服务器端调用。临时数据集是根据当前月份数据集设置的,经过分组和汇总。

创建了一个响应式 data_list。然后通过下一位代码设置。由于:

https://shiny.rstudio.com/reference/shiny/1.0.5/reactiveValues.html

最后一位应该输出图表并且基于:

Highcharter stacked column groupings not using hchart()

output$dailyRevenueChart = renderHighchart({

tempData <- reactive({
  currentMonthdata() %>%
    group_by(PaymentDate, LCO_Indicator) %>%
    summarise(GrossAmount = sum(GrossAmount))
})

data_list <- reactiveValues()

data_list <- 
  tempData() %>% 
    group_by(LCO_Indicator) %>% 
    do(data = list_parse2(.[, c('PaymentDate', 'GrossAmount')])) %>% 
    rename(name = LCO_Indicator) %>% 
    mutate(type = 'column') %>% 
    list_parse()


highchart() %>%
  hc_xAxis(categories = data$PaymentDate) %>%
  hc_add_series_list(isolate(data_list))%>%
  #hc_add_series_list(data_lst2)%>%
  hc_plotOptions(column = list(
    dataLabels = list(enabled = TRUE),
    stacking = "normal",
    enableMouseTracking = TRUE))

})

这是一个示例数据集:

currentMonthData <- tibble::tribble(
   ~PaymentDate,     ~LCO_Indicator, ~TransActionCount, ~Slaes,      ~SalesFreight, ~SalesTax, ~ServiceLabor, ~ServiceMaterials,   ~GrossAmount,
  "2019-01-01",       "Credit",                4L,            -189,             0,      -3.6,             0,                 0,   -192.6,
  "2019-01-01",    "Equipment",                9L,           12286,             0,    250.66,             0,                 0, 12536.66,
  "2019-01-01",   "Equipment",                2L,             9.9,             0,         0,             0,                 0,      9.9,
  "2019-01-02",   "Supply",                2L,             658,             0,     39.48,             0,                 0,   697.48,
  "2019-01-02", "Supply",              190L,               0,             0,         0,       9523.62,            2287.9, 12269.38,
  "2019-01-02",       "Equipment",               76L,        26682.18,              5,   1274.05,             0,                 0, 24639.73
    )

用户界面部分:

fluidRow(
  column(12,

         tabsetPanel(type = "tabs",
                     tabPanel("Daily Revenue",     highchartOutput("dailyRevenueChart"))

         )

  ) 

我希望 UI 中有一个堆叠列高图输出。我已经在输出中测试了一个基本图表,以验证 UI 组件设置是否正确。

【问题讨论】:

  • currentMonthdata() 如您在示例中显示的那样不是响应式的,但是您正在响应式调用它(()),您是要这样做吗?
  • currentMonthdata()在实际代码中是反应性的,但我希望有一个可以测试的数据代码 sn-p。它是在代码中创建的:currentMonthData &lt;- reactive({ data() %&gt;% filter(monthGroup == currentMonthGroup) }) currentMonthData 在模块的服务器组件中正确显示的其他地方使用。
  • @Jabberwockey 如果您在 StackOverflow 上没有获得有关您的问题的帮助,我建议您在此处查找:shiny.rstudio.com/help

标签: r highcharts shiny shiny-reactivity


【解决方案1】:

通过这个设置,我发现了这个基本问题的答案。

在模块的 UI 区域,highchart 没有以模块的 ns 组件开头。

正确的 UI 行应该是:

tabPanel("Daily Revenue", highchartOutput(ns("dailyRevenueChart")))

您会注意到 ns 封装了 highchartOutput 的名称。堆叠不正确,但现在更正了为什么它不显示的问题。

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 2018-02-09
    • 2015-08-07
    • 2013-06-29
    • 2021-03-02
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多