score:1

Accepted answer

Thanks to Pork Chop, the app is working. I needed to change the canvasClicked part in the new JS("function(event) { ... }") to get the app working. The sample code now looks like:

library(shiny)
library(dplyr)
library(highcharter)

ui <- shinyUI(
  fluidPage(
    column(width = 4, highchartOutput("hcontainer", height = "500px")),
    column(width = 4, highchartOutput("hcontainer2", height = "500px")),
    column(width = 4, highchartOutput("hcontainer3", height = "500px")) #added add highcharter output
  )
)

server <- function(input, output, session) { 

  df <- data.frame(year = c(rep(c(2013, 2014, 2015, 2016, 2017), 4)),
                   level_2 = c(rep(c(10, 10, 10, 10, 10, 11, 11, 11, 11, 11),2)),
                   level_3 = c(101, 101, 101, 101, 101, 111, 111, 111, 111, 111,
                               102, 102, 102, 102, 102, 112, 112, 112, 112, 112),
                   level_4 = c(c(1011, 1011, 1011, 1011, 1011, 1111, 1111, 1111, 1111, 1111,
                                 1021, 1021, 1021, 1021, 1021, 1122, 1122, 1122, 1122, 1122)), # additional level added
                   labour = c(1, 5, 10, 20, 25, 5, 10, 20, 25, 30,
                              2, 6, 11, 21, 26, 6, 11, 21, 26, 31))

  output$hcontainer <- renderHighchart({ 

    temp <- df %>% 
      group_by(year, level_2) %>% 
      summarize(Sum = sum(labour)) %>% 
      arrange(level_2) 

    hchart(temp, "line", hcaes(x = year, y = Sum, group = level_2)) %>%
      hc_plotOptions(series = list(events = list(click = canvasClickFunction)))

  })

  canvasClickFunction <- JS("function(event) {Shiny.onInputChange('canvasClicked', [this.name, event.point.category]);}")


  #second highcharter which should appear when user clicked on the serie named 10
  output$hcontainer2 <- renderHighchart({ 
    req(input$canvasClicked[[1]])
    temp2 <- df %>% 
      filter(level_2 == input$canvasClicked[[1]]) %>% # filter selected by click
      group_by(year, level_3) %>% 
      summarize(Sum = sum(labour)) %>% 
      arrange(level_3)

    hchart(temp2, "line", hcaes(x = year, y = Sum, group = level_3)) %>%
      hc_title(text = paste0("I clicked ",input$canvasClicked[[1]])) %>% 
      hc_plotOptions(series = list(events = list(click = canvasClickFunction2)))

  })

  canvasClickFunction2 <- JS("function(event) {Shiny.onInputChange('canvasClicked2', [this.name, event.point.category]);}")

    output$hcontainer3 <- renderHighchart({ 
    req(input$canvasClicked2[[1]])

        temp3 <- df %>%
          filter(level_3 == input$canvasClicked2[[1]]) %>% # filter selected by click
          group_by(year, level_4) %>%
          summarize(Sum = sum(labour)) %>%
          arrange(level_4)

        hchart(temp3, "line", hcaes(x = year, y = Sum, group = level_4)) %>% 
          hc_title(text = paste0("I clicked ",input$canvasClicked2[[1]])) #%>%
    })

} 


shinyApp(ui = ui, server = server)

I will accept the answer for reference once i'm able to (after two days).


Related Query

More Query from same tag