title: Getting finer control of leaflet popups in r
body: <p>I am trying to get finer control of <code>leaflet</code> popups in <code>R</code>, using the <code>leaflet</code> package. The code for a MWE is below:</p>
  
  <pre><code>library(dplyr)
  library(magrittr)
  library(leaflet)
  
  download.file(
    url = "http://biogeo.ucdavis.edu/data/gadm2.8/rds/GBR_adm1.rds", 
    destfile = "GBR_adm1.rds", 
    method = "curl"
  )
  
  shp_gbr &lt;- readRDS("GBR_adm1.rds")
  
  # get centroids for placing popups in second map
  shp_gbr_centers &lt;- 
    rgeos::gCentroid(shp_gbr, byid = TRUE) %&gt;% 
    sp::SpatialPointsDataFrame(shp_gbr@data, match.ID = FALSE)
  
  shp_gbr@data %&lt;&gt;% 
    left_join(shp_gbr_centers[1], by = 'OBJECTID', copy = TRUE) %&gt;% 
    rename(lat = y, lng = x) %&gt;% 
    select(NAME_1, lat, lng) %&gt;% 
    mutate(text = ProgGUIinR::LoremIpsum)
  
  popup &lt;- paste("&lt;b&gt;&lt;h3&gt;", shp_gbr$NAME_1, "&lt;/h3&gt;&lt;/b&gt;", shp_gbr$text)
  
  shp_gbr %&gt;% 
    leaflet() %&gt;% 
    addPolygons(popup = ~popup)
  </code></pre>
  
  <p>This gives a nice map with popups that appear on clicking within the areas of the 4 countries, but in this case, the text is too much for the popup to handle nicely:</p>
  
  <p><a href="https://i.stack.imgur.com/kinpq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kinpq.png" alt="enter image description here"></a></p>
  
  <p>What I would like is to access some of the <code>popupOptions</code> available via the <code>addPopups</code> function, in this case to make the popup wider and have a scroll bar. An example of this is below:</p>
  
  <pre><code>shp_gbr %&gt;% 
    leaflet() %&gt;% 
    addPolygons() %&gt;%
    addPopups(
      data = shp_gbr@data,
      popup = ~popup,
      options =
        popupOptions(
          maxWidth = 600,
          maxHeight = 100
        )
    )
  </code></pre>
  
  <p>However, the popups are now set to be open on launch, rather than appearing on clicking within the boundaries, and do not reopen on click once closed:</p>
  
  <p><a href="https://i.stack.imgur.com/pCEVG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pCEVG.png" alt="enter image description here"></a></p>
  
  <p>My question is how to combine these elements so that you could have, say, a scroll bar for too much text within a map such as the first example, where the popups are closed by default but open on click.</p>