简体   繁体   中英

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. 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:

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

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. I have tested a basic chart in the output to verify the UI components are setup correctly.

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.

The correct UI line should be:

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

You will notice the ns encapsulates the name of the highchartOutput. The stacking is not correct but the question of why it doesn't show up is now corrected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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