mapview icon indicating copy to clipboard operation
mapview copied to clipboard

mapview browser window does not open if run with Rscript

Open adrfantini opened this issue 7 years ago • 15 comments

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?

adrfantini avatar Mar 06 '18 14:03 adrfantini

I am not sure, but maybe this could be used (either directly or for some hints): https://github.com/yeedle/mapix

tim-salabim avatar Mar 06 '18 14:03 tim-salabim

You could also try this suggestion by @timelyportfolio https://github.com/r-spatial/mapview/issues/132#issuecomment-370053553

tim-salabim avatar Mar 06 '18 14:03 tim-salabim

It happened to me last week. If calling it within a script you (seem) to need: print(mapview(something))

lbusett avatar Mar 06 '18 18:03 lbusett

@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

adrfantini avatar Mar 06 '18 19:03 adrfantini

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 avatar Mar 06 '18 20:03 lbusett

@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.

adrfantini avatar Mar 07 '18 10:03 adrfantini

what if you set the class to only the @map slot, as in

class(map@map) <- c("mapview", "suppress_viewer", "htmlwidget")

?

tim-salabim avatar Mar 07 '18 10:03 tim-salabim

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>

adrfantini avatar Mar 07 '18 10:03 adrfantini

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?

tim-salabim avatar Mar 07 '18 10:03 tim-salabim

Yep, on Linux. Reprex, a script like this:

#!/usr/bin/env Rscript
library(mapview)
map <- mapview(poppendorf)
map
print(map)
show(map)

adrfantini avatar Mar 07 '18 10:03 adrfantini

Thanks, I will try tonite

tim-salabim avatar Mar 07 '18 10:03 tim-salabim

@adrfantini did you read this?

tim-salabim avatar Mar 09 '18 06:03 tim-salabim

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.

adrfantini avatar Mar 09 '18 07:03 adrfantini

Ok, I will dig a little to see what other options there are to run Rscript in interactive mode.

tim-salabim avatar Mar 09 '18 08:03 tim-salabim

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.

luukvdmeer avatar May 05 '20 10:05 luukvdmeer