1

I have an interactive map containing several polygon layers. To turn on/off those layers, we can use addLayersControl() function. However, I'd like to have my own checkbox table out of the map. I found a work around to do that, but my trick makes rendering the map so slow (specially compare with what I get by using addLayersControl()). Any way I can use my own checkboxes to control the groups?

Here is my workaround for this problem:

library(shiny)
library(leaflet)
library(DT)
library(formattable)



render_dt = function(data, editable = 'cell', server = TRUE, ...) {
        renderDT(data, selection = 'none', server = server, editable = editable, ...)
}

inputTable = matrix(
        as.character(1:2), nrow = 1, ncol = 2, byrow = TRUE,
        dimnames = list(NULL, c("Quakes", "Outline"))
)

inputTable[1, ] <- sprintf('<input type="checkbox" id="fld%s" value="%s">',
                              inputTable[1, ], inputTable[1, ], inputTable[1, ])

outline <- quakes[chull(quakes$long, quakes$lat),]

ui <- fluidRow(
        leafletOutput("map"),
        DTOutput('floodShow')
)

server <- function(input, output, session) {
        output$floodShow = render_dt(inputTable, escape = FALSE, rownames = FALSE,
                                     options = list(dom = 't',
                                                    autoWidth = TRUE,
                                                    ordering = FALSE,
                                                    paging = FALSE,
                                                    drawCallback= JS(
                                                            'function(settings) {
                                                    Shiny.bindAll(this.api().table().node());}'),
                                                    columnDefs = list(list(className = "dt-center", targets = "_all"))
                                     ),
        )
        
        
        map <- leaflet(quakes) %>%
                # Base groups
                addTiles()
        
        output$map <- renderLeaflet(map)
        
        proxy <- leafletProxy("map", deferUntilFlush = FALSE)
        
        observeEvent({
                input$fld1
                input$fld2
                }, {proxy %>% {
                        if (input$fld2) {
                                addCircles(., data = quakes, ~long, ~lat, ~10^mag/5, stroke = F, group = "Quakes")
                                } else {
                                        clearGroup(., "Quakes")
                                }
                        } %>% {
                                if (input$fld1) {
                                        addPolygons(., data = outline, lng = ~long, lat = ~lat,
                                                    fill = F, weight = 2, color = "#FFFFCC", group = "Outline")
                                } else {
                                        clearGroup(., "Outline")
                                }
                        }
                })
}

shinyApp(ui = ui, server = server)

This solution is OK for small-size data, but it gets very slow when the polygons are getting larger. Using addLayersControl() obviously creates much faster response to on/off actions.

Any suggestions would be appreciated!

0

Browse other questions tagged or ask your own question.