title: Add opacity slider in R leaflet
body: <p>How can I add an slider in a R leaflet app, which controls the opacity of a specific layer? For this application, I don't want to use shiny (suggested here: <a href="https://stackoverflow.com/questions/37682619/adding-sliders-in-r-leaflet-app">adding sliders in R leaflet app</a>), since it has to be exported to a stand-alone html page.</p>
  
  <p>In the following example, I have two CartoDB layers for which I want to control the opacity for one of them, say the basemap layer.</p>
  
  <p><a href="https://stackoverflow.com/questions/31254277/leaflet-js-set-opacity-to-layergroup-with-slider">Leaflet.js - Set Opacity to LayerGroup with Slider</a> contains useful information how to add such a slider. Also, I found out that the htmlwidgets::onRender function can be used to add JavaScript code to an htmlwidget.</p>
  
  <p>So I tried to following code, which doesn't work. The slider is visible, but is doesn't do anything. Moreover, the map pans when dragging the slider.</p>
  
  <pre><code>library(leaflet)
  leaflet() %&gt;% 
    addProviderTiles(provider = "CartoDB.PositronNoLabels", group="Basemap", layerId = 123) %&gt;% 
    addTiles("http://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png", group="Labels") %&gt;% 
    addLayersControl(baseGroups="Basemap", overlayGroups = "Labels") %&gt;% 
    addControl(html="&lt;input id=\"slide\" type=\"range\" min=\"0\" max=\"1\" step=\"0.1\" value=\"1\" onchange=\"updateOpacity(this.value)\"&gt;") %&gt;%
    onRender("
       function updateOpacity(value) {
           this.getLayer(123).opacity(value);
       }
    ")
  </code></pre>
  
  <p>Anyone who can help me out? Thanks in advance!</p>