Search code examples
rshinyr-highcharter

R Shiny with Highcharter hw_grid not working


I want to use hw_grid from highcharter to display various plots in the UI. However, the plots do not show. Here is a sample code. If you run the code outside of shiny it displays the plots but shiny does not.

library(highcharter)
library(shiny)
library(shinydashboard)


ui <- dashboardPage(
  dashboardHeader(title = "hw_grid in shiny", titleWidth  = 500),
  dashboardSidebar(width = 150),

      dashboardBody(
           highchartOutput("distPlot")
        )
    )


server <- function(input, output) {

    output$distPlot <- renderHighchart({
      charts <- lapply(1:9, function(x) {
        hchart(ts(cumsum(rnorm(100))))
         })
    hw_grid(charts, rowheight = 300)
      
    })
}

shinyApp(ui = ui, server = server)

Solution

  • Since these are list of highcharter plots (and not a single highcharter plot) use renderUI and uiOutput.

    library(highcharter)
    library(shiny)
    library(shinydashboard)
    
    
    ui <- dashboardPage(
      dashboardHeader(title = "hw_grid in shiny", titleWidth  = 500),
      dashboardSidebar(width = 150),
      
      dashboardBody(
        uiOutput("distPlot")
      )
    )
    
    
    server <- function(input, output) {
      
      output$distPlot <- renderUI({
        charts <- lapply(1:9, function(x) {
          hchart(ts(cumsum(rnorm(100))))
        })
        hw_grid(charts, rowheight = 300)
        
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    enter image description here