mapview
mapview copied to clipboard
mapview browser window does not open if run with Rscript
I have an R script that, at the end, calls mapview(something).
I run this using Rscript myscript.R from the (Linux) command line, however no browser window is opened, the script simply terminates without showing me the data.
Of course the script works if run manually, line by line.
Other functions, such as mapedit::editFeatures, work fine.
What am I missing?
I am not sure, but maybe this could be used (either directly or for some hints): https://github.com/yeedle/mapix
You could also try this suggestion by @timelyportfolio https://github.com/r-spatial/mapview/issues/132#issuecomment-370053553
It happened to me last week. If calling it within a script you (seem) to need: print(mapview(something))
@lbusett I did try print and show, but to no avail unfortunately. Did you use Rscript?
Thanks @tim-salabim , the first one seems to just call env "R_PROFILE_USER=$f" R --no-save -q --args $@ which runs mapview::mapview(sf::st_zm(sf::st_read(args)))... I do not know how that works out, but I'll try
Ah, right. Before that I also did set options(viewer = NULL) to get mapview to render in the browser and not in rstudio vuewer. May be that will help.
@lbusett unfortunately still nothing... but I am not using Rstudio, so that might be the difference.
@tim-salabim Setting class(map) <- c("mapview", "suppress_viewer", "htmlwidget") I get:
> class(map) <- c("mapview", "suppress_viewer", "htmlwidget")
Warning message:
In class(map) <- c("mapview", "suppress_viewer", "htmlwidget") :
Setting class(x) to multiple strings ("mapview", "suppress_viewer", ...); result will no longer be an S4 object
> map
Error: object of type 'S4' is not subsettable
If setting the class, print(map) works from the command line, but not from Rscript.
what if you set the class to only the @map slot, as in
class(map@map) <- c("mapview", "suppress_viewer", "htmlwidget")
?
class(map@map) <- c("leaflet", "suppress_viewer", "htmlwidget")
print(map)
map
Does not work. if setting class "mapview":
> class(map@map) <- c("mapview", "suppress_viewer", "htmlwidget")
Error in methods::slot(x, "map") :
no slot of name "map" for this object of class "mapview"
Calls: print ... print -> .local -> print -> mapview2leaflet -> <Anonymous>
sorry, my bad. I meant class(map@map) <- c("leaflet", "suppress_viewer", "htmlwidget")
Hm, I will need to have a closer look at this.
Can you provide a reprex? Is this on linux?
Yep, on Linux. Reprex, a script like this:
#!/usr/bin/env Rscript
library(mapview)
map <- mapview(poppendorf)
map
print(map)
show(map)
Thanks, I will try tonite
@adrfantini did you read this?
Yep, I did. Looks like a not so clean workaround to me. Also, I use the library optparse to provide command line options, and I don't think that it will be compatible with this approach.
Ok, I will dig a little to see what other options there are to run Rscript in interactive mode.
Someone got any further on this? The issue still seems to exist. (At least on my 'ancient' Ubuntu 16.04. I am not sure if anything has changed in the meantime)
Script test.R:
library(mapview)
library(sf)
mapview(breweries)
Running from terminal:
Rscript test.R
No browser window is opened.
Just as @adrfantini found, testing the options below does not help:
library(sf)
library(mapview)
map = mapview(breweries)
map
print(map)
show(map)
class(map@map) = c("leaflet", "suppress_viewer", "htmlwidget")
map
print(map)
show(map)
Of course this is not really a specific mapview issue, but rather something of the Rscript command in general. I was just wondering if one of you managed to solve it.