title: Highcharter plot updates only after second click - R Shiny
body: <p>This is my Code, similar like the question I already posted today. Now I have another issue I can not get my head around. When I click the <code>actionButton</code> to update the chart, the chart only updates after the second click. The <code>print</code> statement works after the first click. What is going wrong here?</p>
  
  <pre><code>library(highcharter)
  library(shiny)
  library(shinyjs)
  
  df &lt;- data.frame(
      a = floor(runif(10, min = 1, max = 10)),
      b = floor(runif(10, min = 1, max = 10))
  )
  
  
  updaterfunction &lt;- function(chartid, sendid, df, session) {
  
      message = jsonlite::toJSON(df)
      session$sendCustomMessage(sendid, message)
  
      jscode &lt;- paste0('Shiny.addCustomMessageHandler("', sendid, '", function(message) {
          var chart1 = $("', chartid, '").highcharts()
  
          var newArray1 = new Array(message.length)
          var newArray2 = new Array(message.length)
  
          for(var i in message) {
              newArray1[i] = message[i].a
              newArray2[i] = message[i].b
          }
  
          chart1.series[0].update({
              // type: "line",
              data: newArray1
          }, false)
  
          chart1.series[1].update({
          //   type: "line",
            data: newArray2
        }, false)
  
        console.log("code was run")
  
        chart1.redraw();
      })')
  
      print("execute code!")
      runjs(jscode)
  }
  
  
  
  
  # Define UI for application that draws a histogram
  ui &lt;- fluidPage(
  
      # Application title
      titlePanel("Update highcharter dynamically"),
      #includeScript("www/script.js"),
      useShinyjs(),
  
      # Sidebar with a slider input for number of bins 
      sidebarLayout(
          sidebarPanel(
              actionButton("data", "Generate Data")
          ),
  
          # Show a plot of the generated distribution
          mainPanel(
             highchartOutput("plot")
          )
      )
  )
  
  
  server &lt;- function(input, output, session) {
  
  
      observeEvent(input$data, {
  
          df1 &lt;- data.frame(
              a = floor(runif(10, min = 1, max = 10)),
              b = floor(runif(10, min = 1, max = 10))
          )
  
          updaterfunction(chartid = "#plot", sendid = "handler", df = df1, session = session)
  
      })
  
  
      output$plot &lt;- renderHighchart({
  
          highchart() %&gt;%
  
              hc_add_series(type = "bar", data = df$a) %&gt;%
              hc_add_series(type = "bar", data = df$b)
  
      })
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>