How can I remove the zoom and layers buttons of mapview or leaflet? Below is a small shiny app that I found online, however, it does not remove the above mentioned buttons. The app opens but get an error when clicking the print map button. I am able to remove the buttons with JavaScript at opening but not at the time of saving the map as PNG.
library(shiny)
library(mapview)
library(leaflet)
ui <- fluidPage(
# tags$head(
# tags$style(HTML("
# /* Hide zoom control */
# .leaflet-control-zoom {
# display: none;
# }
# /* Hide layer control */
# .leaflet-control-layers {
# display: none;
# }
# "))
# ),
actionButton("print_map", "Print Map"),
leafletOutput("mymap")
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
mapview(breweries)@map
})
#Remove zoom and layers control before saving to a file.
observeEvent(input$print_map, {
mapshot(output$mymap, file = "map_zoom.png", remove_controls = c("zoomControl", "layersControl"))
})
}
shinyApp(ui, server)
#Error when click on the print map button:
Warning: Error in mapshot: requireNamespace("webshot", quietly = TRUE) is not TRUE
The steps I took to get it to work
1. Make sure webshots2 is installed
install.packages("webshots2")
2. Add this chunk of code to save the user's view of the map (which is not stated explicitly in your question but I assume is the desired behaviour). This step was taken from How to save a leaflet map in Shiny
user.created.map <- reactive({
mapview(breweries)@map %>%
setView( lng = input$mymap_center$lng
, lat = input$mymap_center$lat
, zoom = input$mymap_zoom
)
3. Switch to mapshot2 instead of mapshot
All together now it looks like this.
install.packages("webshots2")
webshot::install_phantomjs()
library(shiny)
library(mapview)
library(leaflet)
ui <- fluidPage(
tags$head(
tags$style(HTML("
/* Hide zoom control */
.leaflet-control-zoom {
display: none;
}
/* Hide layer control */
.leaflet-control-layers {
display: none;
}
"))
),
actionButton("print_map", "Print Map"),
leafletOutput("mymap")
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
mapview(breweries)@map
})
# Create a new map based off of the user's zoom level
user.created.map <- reactive({
mapview(breweries)@map %>%
setView( lng = input$mymap_center$lng
, lat = input$mymap_center$lat
, zoom = input$mymap_zoom
)
}) # end of creating user.created.map()
#Remove zoom and layers control and save the users map
observeEvent(input$print_map, {
mapshot2(user.created.map(),
file = "map_zoom.png", remove_controls = c("zoomControl", "layersControl")
)
})
}
shinyApp(ui, server)