1

I have set up a script that crops a fixed SpatialPointsDataFrame based on an area that the user defines. These are then plotted using MapView. However, if the SpatialPointsDataFrame comes back empty as no points are within the user defined area, MapView fails with the error:

Error in (function (classes, fdef, mtable): unable to find an inherited method for function ‘mapView’ for signature ‘"NULL"’

I have tried using ignoreNULL as defined in Leaflet, but it doesn't seem to work.

Example Code Below (ignoring the steps to crop the data):

##load required package    
library(mapview)

##create variable with point
breweries_A <- breweries

##create variable with no points    
breweries_B <- NULL

##create variable ready to plot as one map    
m <- mapview(breweries_A) + mapview(breweries_B)

I am hoping there is a simple bit of code I can add to get MapView to ignore an empty dataset.

Blaiso
  • 165
  • 9
  • You question is very precise, but I have an impression that a little more details would be useful to help you. The thing is, the `NULL` object, a data frame with zero rows or an empty geometry are processed with `mapview( )` differently: the first two throw an error, when the last one, e.g. `mapview(st_polygon( ))`, outputs a blank map. Probably, it makes sense to give a little more details relating to cropping of your SpatialPointsDataFrame? – Ekatef Aug 28 '19 at 13:23
  • Thanks for your reply Ekatef. I originally tried to show this, but I think my method is a bit long winded and I was struggling to do it with the Breweries dataset as the coordinates are combined in an unusual way. In short I have struggled to find a dataset to replicate the ones I am using. – Blaiso Aug 28 '19 at 14:02
  • You could do `m = mapview(breweries_A) + breweries_B` where the `NULL` object is simply being ignored. – TimSalabim Aug 29 '19 at 07:13
  • Hi Tim, the issue is that sometimes breweries_B has data if the cropping step before this doesn't remove it, hence it being included in m. – Blaiso Aug 29 '19 at 09:22
  • So, if there's data in `breweries_B` you want to add it to `m` or don't you? `mapview(breweries_A) + breweries_B` is idendical to `mapview(breweries_A) + breweries_B` except that the latter also works if second argument is `NULL`. Maybe I'm not understanding your issue completely... – TimSalabim Aug 30 '19 at 06:43
  • If there is data in breweries_b I want it to be added to m (and subsequently plotted), if there isn't I want it to just plot breweries_A and ignore breweries_B, but this is a small part of a larger script so I don't want to have to have to alter the script each time. I have simplified this to focus on the MapView function. It seems like in Leaflet it is possible to add ignoreNULL and it will ignore the error. – Blaiso Aug 30 '19 at 16:11
  • As I said, mapview(a) + mapview(b) is equivalent to mapview(a) + b. So for your problem the second should always work. – TimSalabim Aug 30 '19 at 17:34
  • mapview(a) + b won't plot b if it has data. – Blaiso Sep 01 '19 at 21:33
  • It will plot both a and b – TimSalabim Sep 02 '19 at 10:14
  • I apologise, you are quite right. Thanks very much. The only issue is, I had some formatting options on breweries_b and am unable to specify these now. – Blaiso Sep 02 '19 at 17:25
  • You're the first one to need this feature, but I might just include a method for `NULL` in mapview anyway as it should only be a few lines of code. – TimSalabim Sep 03 '19 at 05:19
  • Thanks Tim, that would be brilliant. There are probably more robust ways to do this using leaflet, but mapview is so simple to use. – Blaiso Sep 03 '19 at 13:12
  • I've just pushed an update to the github repo of mapview that simply returns `NULL` for `mapview(NULL)`. this should solve your issue I guess – TimSalabim Sep 07 '19 at 18:57
  • Works a treat, Many thanks. – Blaiso Sep 09 '19 at 12:14

0 Answers0