简体   繁体   English

如何在Shiny App中将反应式列表与反应式数据帧和R Highcharter一起使用

[英]How to use a reactive list with reactive dataframe and R Highcharter in Shiny App

I am writing up a shiny app that will produce a stacked column HighChart of Payments by Payment date, stacked by department. 我正在编写一个闪亮的应用程序,它将按部门显示按付款日期堆积的“付款的高图表”列。 In a non-reactive setting this is done by passing a data set to a list, passing the list into a add_series_list in highcharter and the stacked column chart is made. 在非反应式设置中,这是通过将数据集传递到列表,将列表传递到add_series_list中的add_series_list以及完成的堆叠柱形图来完成的。 In the reactive setting this is not producing a chart. 在反应式设置中,这不会生成图表。

This is the server side call to the chart. 这是图表的服务器端调用。 The temp data set is set from a current month data set, grouped and summarised. 临时数据集是根据当前月份的数据集进行设置,分​​组和汇总的。

A reactive data_list is created. 将创建一个反应性数据列表。 This is then set through the next bit of code. 然后通过下一段代码进行设置。 I didn't put a reactive (although tried it with reactive({}) ) due to: 由于以下原因,我没有放入反应式(尽管尝试了reactive({}) ):

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

The final bit should output the chart and is based off of: 最后一位应基于以下内容输出图表:

Highcharter stacked column groupings not using hchart() Highcharter堆叠列分组未使用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))

})

Here is a Sample data set: 这是一个示例数据集:

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
    )

UI Section: 用户界面部分:

fluidRow(
  column(12,

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

         )

  ) 

I would expect a stacked column highchart output in the UI. 我期望UI中有堆叠的列highchart输出。 I have tested a basic chart in the output to verify the UI components are setup correctly. 我已经在输出中测试了基本图表,以验证UI组件是否正确设置。

Working through this setup I discovered the answer to the basic question on this one. 通过这一设置,我发现了关于这一基本问题的答案。

In the UI area of the module, the highchart was not prefaced with the ns component for the module. 在模块的UI区域中,highchart没有以ns组件作为模块的开头。

The correct UI line should be: 正确的UI行应为:

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

You will notice the ns encapsulates the name of the highchartOutput. 您会注意到ns封装了highchartOutput的名称。 The stacking is not correct but the question of why it doesn't show up is now corrected. 堆叠是不正确的,但是现在为什么不出现堆叠的问题已得到纠正。

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

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