Giorgio Bella Giorgio Bella - 1 month ago 17
R Question

R Shiny Highcharter - How to use hc_rangeSelector()

I'm plotting a financial intra-day time serie on an R-Shiny project using the highcharter package. I'm using the following code for the server part in order to get the output (note that xtsPrices() is a function that returns an xts intraday time-serie):

output$plot <- renderHighchart({

y <- xtsPrices()

highchart() %>%
hc_exporting(enabled = TRUE)%>%
hc_add_series_ohlc(y) %>%
hc_add_theme(hc_theme_538(colors = c("red", "blue", "green"),
chart = list(backgroundColor = "white") ))
})


I read in the documentation that in order to personalize zoom buttons I have to deal with the hc_rangeSelector() function, but I don't understand how to specify them in this R-Shiny environment as shown for the javascript case in Highstock API. In particular - because it's an intra-day time-serie - I would need buttons like "20min", "1h", "3h", "1D", etc.

Answer

For intra-day data you can do something like this:

hc <- highchart() %>% 
  hc_exporting(enabled = TRUE) %>%
  hc_add_series_ohlc(y, yAxis = 0, name = "Sample Data", id = "T1",smoothed=TRUE,forced=TRUE,groupPixelWidth=24) %>% 
  hc_rangeSelector( buttons = list(
    list(type = 'all', text = 'All'),
    list(type = 'hour', count = 2, text = '2h'),
    list(type = 'hour', count = 1, text = '1h'),
    list(type = 'minute', count = 30, text = '30m'),
    list(type = 'minute', count = 10, text = '10m'),
    list(type = 'minute', count = 5, text = '5m')
  )) %>% 
  hc_add_theme(hc_theme_538(colors = c("red", "blue", "green"),chart = list(backgroundColor = "white") ))
hc

enter image description here

Comments