shiny leaflet addControl with checkboxInput problem

I'm trying to add a checkboxInput onto my leaflet map using addControl. I'm having trouble making a reprex, since it works ok in the example below (although the right hand padding is excessive). But in my actual app I get lots of white space all around the checkbox and it's really ugly. How do I fix this? I haven't had any luck with changing my css file. I believe it uses css classes info and legend. I don't want to mess up my legend though.

library(shiny)
library(leaflet)

dlat <- 1 / 111000 * 100 # degrees per metre
n <- 10000 # number of circles
mylng <- 175.322 + (runif(n) * 2 - 1) * dlat * 6
mylat <- -37.789 + (runif(n) * 2 - 1) * dlat * 1.5
hex <- c(0:9, LETTERS[1:6])

ui <- fluidRow(
  tags$h2("Leaflet in Shiny"),
  actionButton("plotbutton", label = "Recolour Markers"),
  leafletOutput("map")
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    cat("renderLeaflet\n")
    leaflet() %>%
      addTiles() %>%
      setView(175.322, -37.789, zoom = 17) %>% 
    addControl(
      checkboxInput("linestype", strong("My checkbox"), value = FALSE), 
      position="bottomright"
    ) 
  })
  
  observeEvent(input$plotbutton, {
    cat("input$plotbutton\n")
    col <- paste0("#", paste0(sample(hex, 6, replace = TRUE), collapse = ""))
    leafletProxy("map") %>%
      clearShapes() %>% 
      addCircles(
        lng = mylng,
        lat = mylat,
        radius = 1, 
        color = col)
  })
}

shinyApp(ui = ui, server = server)

Edit: This file has some css that the author used.

https://github.com/rstudio/leaflet/blob/44df7d18a2618e0aeecb4145d765e597ec65878b/inst/htmlwidgets/lib/leafletfix/leafletfix.css

Update: I did not solve this, but I decided to add the checkbox to the overlayGroups control, which my app has, and intercept the user selection with the input$xxx_groups event: here's some code snippets for anyone interested.

 output$map <- renderLeaflet({
    leaflet() %>%
      addPolygons(
        data = boundary,
        group = catch_bound,
        fillOpacity = 0,
        opacity = 1,
        weight = 3,
        color = outlinecol
      ) %>%
      setView(
        lng = unname((boundarybox[1] + boundarybox[3]) / 2), 
        lat = unname((boundarybox[2] + boundarybox[4]) / 2), 
        zoom = 9
      ) %>%
      addProviderTiles(
        provider = "OpenStreetMap.Mapnik",
        options = providerTileOptions(minZoom = 8)
      )  %>%
      leaflet::addLayersControl(
        overlayGroups = c(catch_bound, stream_lines, low_order, feat_mask, station_markers),
        options = layersControlOptions(collapsed = FALSE)
      ) %>% 
      hideGroup(low_order)
  })

  observeEvent(input$map_groups, {
    # input$map_groups is a char vector of the names of the selected overlay groups
    showlow <- low_order %in% input$map_groups
    if (user$linestype != showlow){
      user$linestype <- showlow
      if (!showlow) {
        user$waterways <- dplyr::filter(riverlines, StreamOrde >= 3)
      } else {
        user$waterways <- riverlines
      }
    }
  })

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.