mapview icon indicating copy to clipboard operation
mapview copied to clipboard

removeMouseCoordinates option

Open fawda123 opened this issue 7 years ago • 6 comments

Hi, it would be great if there was an option to remove the mouse coordinates dialog at the top of the map. I've found a solution here but it doesn't seem to work with multiple maps in a Shiny app.

This is an app that uses mapview with Shiny, works as intended:

library(shiny)
library(leaflet)
library(mapview)

pts <- data.frame(
  x = rnorm(10, mean = -93.625), 
  y = rnorm(10, mean = 42.0285)
)

mapin <- mapview(pts, xcol= 'x', ycol = 'y')@map 

# Define UI 
ui <- fluidPage(
  
  sliderInput("radius",
              "Point radius:",
              min = 1,
              max = 50,
              value = 30),
  
  leafletOutput('map1'),
             
  leafletOutput('map2')   
            
  )
)

# Define server logic
server <- function(input, output) {
  
  output$map1 <- renderLeaflet({
    mapin %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
  })
  output$map2 <- renderLeaflet({
    mapin %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
  })
  
  observe({
    
    tab1 <- leafletProxy('map1', data = pts) %>%
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = input$radius)
    
    tab2 <- leafletProxy('map2', data = pts) %>% 
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = input$radius)
    
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

This is the same app but using a function from the above link that attempts to remove the mouse coordinates. It kind of works but only one plot is rendered. Could this be solved with a hard-wired option in mapview to remove the mouse coordinates?

library(shiny)
library(leaflet)
library(mapview)

pts <- data.frame(
  x = rnorm(10, mean = -93.625), 
  y = rnorm(10, mean = 42.0285)
)

removeMouseCoordinates = function(map) {

  rc = map$jsHooks$render
  rc_lnlt = lapply(rc, grep, pattern = "lnlt")
  for (i in seq_along(map$jsHooks$render)) {
    map$jsHooks$render[[i]][rc_lnlt[[i]]] = NULL
  }
  
  return(map)
  
}

mapin <- mapview(pts, xcol= 'x', ycol = 'y')@map %>% 
  removeMouseCoordinates

# Define UI 
ui <- fluidPage(
  
  sliderInput("radius",
              "Point radius:",
              min = 1,
              max = 50,
              value = 30),
  
  leafletOutput('map1'),
             
  leafletOutput('map2')   
            
  )
)

# Define server logic
server <- function(input, output) {
  
  output$map1 <- renderLeaflet({
    mapin %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
  })
  output$map2 <- renderLeaflet({
    mapin %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
  })
  
  observe({
    
    tab1 <- leafletProxy('map1', data = pts) %>%
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = input$radius)
    
    tab2 <- leafletProxy('map2', data = pts) %>% 
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = input$radius)
    
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

fawda123 avatar Apr 04 '18 00:04 fawda123

Thanks for this issue. I am not quite sure what's going on here. I will try to find a solution to get removeMouseCoordinates to behave properly. Maybe @timelyportfolio has an idea what's happening?

In the long run, though, I would like to revamp the mapview API a bit so that most options/arguments will be set via some sort of options call. I will then take care of enabling mouseCoordinates = TRUE/FALSE.

tim-salabim avatar Apr 04 '18 18:04 tim-salabim

Sorry for the delay, this should now work as expected.

tim-salabim avatar Aug 16 '18 09:08 tim-salabim

Cheers @tim-salabim but I'm getting a new error now:

m = mapview(breweries)

returns: Error in !highlight : invalid argument type

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] leaflet_2.0.1  mapview_2.4.19

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18      compiler_3.5.1    later_0.7.3       git2r_0.23.0      base64enc_0.1-3  
 [6] class_7.3-14      tools_3.5.1       digest_0.6.15     jsonlite_1.5      viridisLite_0.3.0
[11] satellite_1.0.1   memoise_1.1.0     lattice_0.20-35   png_0.1-7         shiny_1.1.0      
[16] DBI_0.8           crosstalk_1.0.0   curl_3.2          yaml_2.1.18       spData_0.2.8.3   
[21] e1071_1.7-0       withr_2.1.2       httr_1.3.1        raster_2.6-7      htmlwidgets_1.2  
[26] devtools_1.13.6   webshot_0.5.0     stats4_3.5.1      classInt_0.2-3    grid_3.5.1       
[31] sf_0.6-3          R6_2.2.2          sp_1.3-1          udunits2_0.13     magrittr_1.5     
[36] scales_1.0.0      promises_1.0.1    htmltools_0.3.6   units_0.5-1       mime_0.5         
[41] xtable_1.8-2      colorspace_1.3-2  httpuv_1.4.5      munsell_0.5.0    

fawda123 avatar Aug 16 '18 15:08 fawda123

sorry forgot to push that one. should work now

tim-salabim avatar Aug 16 '18 16:08 tim-salabim

Works, thanks!

fawda123 avatar Aug 16 '18 18:08 fawda123

Reopening this as I am having the same issue as @fawda123. removemousecoordinates() works on the first map, but subsequent mapview maps are not rendered. Below is a reprex quarto document (and I was having the same issue with Rmarkdowns too). Please remove the / characters at the end of code blocks, just wanted Github to render the code chunks correctly.

---
title: "Untitled"
format: html
---


```{r}
library(tidyverse)
library(mapview)
library(sf)

dc_boundary = st_read("https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Administrative_Other_Boundaries_WebMercator/MapServer/10/query?outFields=CITY_NAME&where=1%3D1&f=geojson")


removeMouseCoordinates = function(map) {

  rc = map$jsHooks$render
  rc_lnlt = lapply(rc, grep, pattern = "lnlt")
  for (i in seq_along(map$jsHooks$render)) {
    map$jsHooks$render[[i]][rc_lnlt[[i]]] = NULL
  }
  return(map)
}


mapview(dc_boundary)@map %>% removeMouseCoordinates
```

```{r}
mapview(dc_boundary)@map %>% removeMouseCoordinates
```

Session Info Below

R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS 15.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Lima
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] janitor_2.2.0           tidygeocoder_1.0.5      glue_1.8.0              leaflegend_1.2.1       
 [5] testthat_3.2.1.1        htmltools_0.5.8.1       colorspace_2.1-1        fmsb_0.7.6             
 [9] leafpop_0.1.0           gtExtras_0.5.0          gt_0.11.1               readxl_1.4.3           
[13] leaflet.providers_2.0.0 here_1.0.1              leaflet.extras_2.0.1    leaflet.extras2_1.2.2  
[17] leaflet_2.2.2           RColorBrewer_1.1-3      tidycensus_1.6.6        fontawesome_0.5.3      
[21] nngeo_0.4.8             mapview_2.11.2          arrow_18.1.0            geoarrow_0.2.1         
[25] sf_1.0-17               lubridate_1.9.3         forcats_1.0.0           stringr_1.5.1          
[29] dplyr_1.1.4             purrr_1.0.4             readr_2.1.5             tidyr_1.3.1            
[33] tibble_3.2.1            ggplot2_3.5.1           tidyverse_2.0.0         devtools_2.4.5         
[37] usethis_3.0.0          

loaded via a namespace (and not attached):
 [1] rstudioapi_0.16.0  jsonlite_1.9.1     wk_0.9.3           magrittr_2.0.3     farver_2.1.2      
 [6] fs_1.6.5           vctrs_0.6.5        memoise_2.0.1      paletteer_1.6.0    base64enc_0.1-3   
[11] terra_1.8-5        progress_1.2.3     curl_6.0.1         raster_3.6-30      cellranger_1.1.0  
[16] s2_1.1.7           sass_0.4.9         KernSmooth_2.23-24 htmlwidgets_1.6.4  cachem_1.1.0      
[21] uuid_1.2-1         mime_0.12          lifecycle_1.0.4    pkgconfig_2.0.3    R6_2.6.1          
[26] fastmap_1.2.0      snakecase_0.11.1   shiny_1.9.1        digest_0.6.37      rematch2_2.1.2    
[31] rprojroot_2.0.4    leafem_0.2.3       pkgload_1.4.0      crosstalk_1.2.1    reactR_0.6.1      
[36] timechange_0.3.0   httr_1.4.7         compiler_4.4.1     proxy_0.4-27       remotes_2.5.0     
[41] bit64_4.5.2        withr_3.0.2        brew_1.0-10        DBI_1.2.3          pkgbuild_1.4.4    
[46] rappdirs_0.3.3     sessioninfo_1.2.2  classInt_0.4-10    tools_4.4.1        units_0.8-5       
[51] reactable_0.4.4    httpuv_1.6.15      satellite_1.0.5    promises_1.3.0     grid_4.4.1        
[56] generics_0.1.3     gtable_0.3.6       tzdb_0.4.0         class_7.3-22       data.table_1.16.0 
[61] hms_1.1.3          sp_2.1-4           xml2_1.3.6         utf8_1.2.4         pillar_1.10.1     
[66] nanoarrow_0.5.0.1  later_1.3.2        lattice_0.22-6     bit_4.5.0          tidyselect_1.2.1  
[71] miniUI_0.1.1.1     knitr_1.49         svglite_2.1.3      stats4_4.4.1       xfun_0.49         
[76] brio_1.1.5         stringi_1.8.4      yaml_2.3.10        evaluate_1.0.1     codetools_0.2-20  
[81] cli_3.6.4          xtable_1.8-4       systemfonts_1.2.1  munsell_0.5.1      jquerylib_0.1.4   
[86] Rcpp_1.0.14        png_0.1-8          ellipsis_0.3.2     assertthat_0.2.1   prettyunits_1.2.0 
[91] profvis_0.4.0      urlchecker_1.0.1   tigris_2.1         scales_1.3.0       e1071_1.7-16      
[96] crayon_1.5.3       rlang_1.1.5        rvest_1.0.4       

ajjitn avatar Mar 29 '25 16:03 ajjitn