Skip to content

mapview not rendering on vscode and mapviewOptions(fgb = TRUE) #485

Open
@duccioa

Description

@duccioa

Hello!
I am in VSCode 1.89.1. This script opens a Viewer on the side but it is empty.

library(mapview)
#> GDAL version >= 3.1.0 | setting mapviewOptions(fgb = TRUE)
mapview(breweries)
#> Browsing http://127.0.0.1:5046

The same address opened in Firefox or Chrome still shows an empty page. If I inspect the html code, it is indeed empty.

With fgb = FALSE it works as expected in the VSCode viewer.

library(mapview)
#> GDAL version >= 3.1.0 | setting mapviewOptions(fgb = TRUE)
mapviewOptions(fgb = FALSE)
mapview(breweries)

I was experimenting with the print method and I tried to grey out the part about if (ide == "vscode") and in this case it works also with mapviewOptions(fgb = TRUE), but not with mapviewOptions(viewer.suppress = TRUE).

printMapview = function (x) {

  ## normal htmlwidget printing for notebooks etc.
  ## set options fgb & georaster to FALSE!!
  if (!isTRUE(mapviewGetOption("fgb")) & !isTRUE(mapviewGetOption("georaster"))) {
    print(mapview2leaflet(x))
    # invisible(x)
    return(invisible())
  }

  ## convert to leaflet object
  x = mapview2leaflet(x)
  viewer = getOption("viewer")
  ide = get_ide()
  if (mapviewGetOption("viewer.suppress")) {
    viewer = NULL
  }
  if (!is.null(viewer)) {
    viewerFunc = function(url) {
      paneHeight = x$sizingPolicy$viewer$paneHeight
      if (identical(paneHeight, "maximize")) {
        paneHeight = -1
      }
      # I REMOVED THIS PART >>>>>
      # if (ide == "vscode") {
      #   # VSCode's viewer can't ignore cross-origin requests. Need to serve the
      #   # map so assests can be read, e.g. .fgb files.
      #   server <- servr::httd(
      #       dir = get_url_dir(url),
      #       verbose = FALSE,
      #       browser = FALSE
      #     )
      #   url <- server$url
      # }
      # <<<<<<<<<<<<<<<<<<<<<<<<
      viewer(url, height = paneHeight)
    }
  } else {
    viewerFunc = function(url) {
      dir = get_url_dir(url)
      switch(ide,
        "rstudio" = if (mapviewGetOption("viewer.suppress")) {
          fl = file.path(dir, "index.html")
          utils::browseURL(fl)
          } else {
            servr::httd(
              dir = dir,
              verbose = FALSE
            )
          },
          "vscode" = servr::httd(
            dir = dir,
            verbose = FALSE
          ),
          # default
          servr::httd(
            dir = dir,
            verbose = FALSE
          )
          )
    }
  }
  htmltools::html_print(
    htmltools::as.tags(x, standalone = TRUE)
    , viewer = if (interactive()) viewerFunc
  )
  invisible(x)
}

My session info:

R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Pop!_OS 22.04 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] mapview_2.11.2.9000 s4c_2.5.4           here_1.0.1          dplyr_1.1.4         magrittr_2.0.3      sf_1.0-14           data.table_1.14.10  devtools_2.4.5      usethis_2.2.2      

loaded via a namespace (and not attached):
 [1] pkgload_1.3.3           jsonlite_1.8.8          shiny_1.8.0             lgr_0.4.4               sp_2.1-2                stats4_4.1.2            renv_1.0.5              remotes_2.4.2.1         sessioninfo_1.2.2       pillar_1.9.0           
[11] lattice_0.21-8          glue_1.6.2              uuid_1.1-1              digest_0.6.33           RColorBrewer_1.1-3      promises_1.2.1          leaflet.providers_2.0.0 colorspace_2.1-0        htmltools_0.5.7         httpuv_1.6.13          
[21] pkgconfig_2.0.3         raster_3.6-26           servr_0.27              purrr_1.0.2             xtable_1.8-4            scales_1.3.0            brew_1.0-8              svglite_2.1.3           terra_1.7-55            satellite_1.0.4        
[31] later_1.3.2             tibble_3.2.1            proxy_0.4-27            todor_0.1.2             generics_0.1.3          ellipsis_0.3.2          cachem_1.0.8            withr_2.5.2             lazyeval_0.2.2          cli_3.6.2              
[41] crayon_1.5.2            mime_0.12               memoise_2.0.1           fs_1.6.3                fansi_1.0.6             class_7.3-22            pkgbuild_1.4.3          profvis_0.3.8           tools_4.1.2             lifecycle_1.0.4        
[51] stringr_1.5.1           munsell_0.5.0           jquerylib_0.1.4         rex_1.2.1               compiler_4.1.2          e1071_1.7-14            systemfonts_1.0.5       rlang_1.1.2             classInt_0.4-10         units_0.8-5            
[61] grid_4.1.2              leafpop_0.1.0           rstudioapi_0.15.0       htmlwidgets_1.6.4       crosstalk_1.2.1         miniUI_0.1.1.1          leafem_0.2.3            base64enc_0.1-3         codetools_0.2-19        DBI_1.1.3              
[71] R6_2.5.1                fastmap_1.1.1           utf8_1.2.4              rprojroot_2.0.4         KernSmooth_2.23-21      desc_1.4.3              stringi_1.8.3           Rcpp_1.0.11             vctrs_0.6.5             png_0.1-8              
[81] xfun_0.41               leaflet_2.2.1           tidyselect_1.2.0        urlchecker_1.0.1  

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions