0

I want to create a highchart column graph with a number of series depending on an input - n. The input (n) is an integer and I want to display the First n values as columns from a data.table (called data in my code).

This is my code:

 n <- input$Top_n
    
    # Date Range Selected:
    d1 <- as.Date(input$DateRange[1], "%Y-%mm-%dd")
    d2 <- as.Date(input$DateRange[2], "%Y-%mm-%dd")
    # d1 <- as.Date("2021-01-01")
    # d2 <- as.Date(today())
    
    
    # Data Table:
    data <- data.table(pl_all) %>%
      filter(GRUPO %in% input$PickerGrupos)
    # data <- data.table(pl_all) %>%
    #   filter(GRUPO %in% list('Domesticos','VP')) 
    
    data <- data[DATA >= d1 & DATA <= d2]
    data<- data %>% 
      group_by(FUNDO) %>%
      summarise(IN = sum(SUBSCRICOES), OUT = sum(RESGATES), NET = sum(NET))%>%
      arrange(desc(NET)) %>%
      slice(1:n)
    
    # Bar Plot:
    highchart() %>%
      hc_chart(type= "column") %>%
      hc_xAxis(categories= list("TOP 5")) %>%
      hc_add_series(name= data$FUNDO[1], data = data$NET[1], color= "#336600") %>%
      hc_add_series(name= data$FUNDO[2], data = data$NET[2], color="#990000") %>%
      hc_add_series(name= data$FUNDO[3], data = data$NET[3], color = "#006666") %>%
      hc_add_series(name= data$FUNDO[4], data = data$NET[4]) %>%
      hc_add_series(name= data$FUNDO[5], data = data$NET[5]) %>%
      hc_exporting(enabled = TRUE)

As its stands, my graph´s series are static and equal to five.

Tried to use a for cycle:

 for(i in 1:n){
      hc<- 
        hc_add_series(hc,name= data$FUNDO[i], data = data$NET[i], color= "#336600")
    }

You can use this data:

structure(list(FUNDO = 1:52, IN = c(832027943.4, 477550794.84, 
323069484.57, 185214318.25, 316335887.74, 142476573.92, 67630816.77, 
63115006.06, 44740413.27, 32909661.29, 33073221.78, 34939968.39, 
25546990.24, 22669240.51, 19500000, 23030106.11, 21316486.18, 
18924841.18, 29676794.83, 13499838.72, 11824163.88, 13228857.01, 
13556784.21, 11537128.91, 47785529.13, 19803066.97, 17909773.41, 
8034675.36, 6558370, 3755270.85, 2766415.2, 3718513.49, 3807326.99, 
1819550, 1242564.71, 3185709.61, 296241.74, 2109856.31, 1076167.9, 
5130848.18, 2364004.75, 1645003.19, 21700009.95, 1236552.92, 
900418.92, 3357288.09, 1944435.19, 742440.97, 244401865.68, 37930356.35, 
56376893.94, 108916868.79001), OUT = c(531264608.2, 204190266.1, 
79243317.98, 15545143.94, 189727969.14, 36911398, 0, 19613186.16, 
2272148.59, 804610.68, 2256049.18, 8986464.83, 2161334.36, 1474564.76, 
31835.87, 4414243.98, 3465787.42, 2195498.38, 13703006.83, 202749.14, 
80976.65, 1756626.79, 2105973.06, 261445.87, 36804651.89, 9624599.38, 
8206163.67, 606446.01, 502310.75, 479033.95, 816610.17, 2184654.08, 
2404520.72, 429681.76, 39171.3, 2882738.83, 747770.06, 2587886.52, 
1670996.38, 7128933.38, 4445475.12, 3841877.14, 24094717.08, 
4305708.18, 6865107.54, 9803609.04, 9665802.15, 14326918.86, 
274946605.12, 78086552.87, 261541233.54, 350964756.25), NET = c(300763335.2, 
273360528.74, 243826166.59, 169669174.31, 126607918.6, 105565175.92, 
67630816.77, 43501819.9, 42468264.68, 32105050.61, 30817172.6, 
25953503.56, 23385655.88, 21194675.75, 19468164.13, 18615862.13, 
17850698.76, 16729342.8, 15973788, 13297089.58, 11743187.23, 
11472230.22, 11450811.15, 11275683.04, 10980877.24, 10178467.59, 
9703609.74, 7428229.35, 6056059.25, 3276236.9, 1949805.03, 1533859.41, 
1402806.27, 1389868.24, 1203393.41, 302970.78, -451528.32, -478030.21, 
-594828.48, -1998085.2, -2081470.37, -2196873.95, -2394707.13, 
-3069155.26, -5964688.62, -6446320.95, -7721366.96, -13584477.89, 
-30544739.44, -40156196.52, -205164339.6, -242047887.45999)), row.names = c(NA, 
-52L), class = c("tbl_df", "tbl", "data.frame"))
  • 2
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a working code example and a snippet of your data or some fake data. – stefan Nov 25 '22 at 08:35
  • @stefan is the data provided ok? – Caetano Almeida e Brito Nov 25 '22 at 09:34
  • Are you using this in a shiny app? If the please add some ui and server code as well – Bertil Baron Nov 28 '22 at 16:08

0 Answers0