rshinyr-highchartercandlesticks

R Shiny App HighCharter OHLC/Candlesticks default zoom period


I am using highcharter library for a shiny app to make candlesticks chart of an XTS. Code in server.R to generate the chart is given below (sry, this code is not reproducible) By default the chart generated shows data for all period. I want the zoom level to be changed to 1 month. It is equivalent to clicking on "1m" in zoom options. How can I do that?

library(highcharter)  
output$ohlcPlot <- renderHighchart({
    if (IsValidNSESymbol(input$x1StockCode)) {
      df <- loadStockPrices()
      
      highchart(type = "stock") %>%
        hc_add_series(data = df,
                      name = "OHLC",
                      type = "candlestick") %>%
        hc_colors(color = "red")
    }
  })

Candlestick chart which defaults to Zoom level of All


Solution

  • You can add %>% hc_rangeSelector(selected = 0) to keep month value as default where 0 is the position of the zoom option.

    For eg with AAPL stock.

    library(highcharter)
    
    quantmod::getSymbols('AAPL',src = 'yahoo',from = "2013-01-01", to = "2017-12-31")
    highchart(type = "stock") %>% 
      hc_add_series(data = AAPL, 
                    name = "OHLC",
                    type = "candlestick") %>%
      hc_colors(color = "red") %>%
      hc_rangeSelector(selected = 0)
    

    enter image description here