SymbolixAU / googleway

R Package for accessing and plotting Google Maps
http://symbolixau.github.io/googleway/
Other
232 stars 46 forks source link

add_polygons z_index parameter requires global scope #182

Closed avsdev-cw closed 6 years ago

avsdev-cw commented 6 years ago

I believe there is a bug when using a scoped object to pass a value into z_index for add_polygons.

It appears to bubble up from createMapObject as shown by the trace below:

Listening on http://127.0.0.1:4561 [1] 5 [1] TRUE Warning: Error in eval: object 'zindex' not found 95: eval 94: eval 91: FUN 90: lapply 89: createMapObject 88: add_polygons 87: function_list[[k]] 85: freduce 84: _fseq 83: eval 82: eval 80: %>% 79: func [~/example/UI/mapWidget.R#269] 78: origRenderFunc 77: output$map 1: runApp

The relevant code sections are:

# line 41
server <- function(input, output) {
  layersList <- data.frame(
    id = c("layer_1", "layer_2", "layer_3", "user_layer"),
    name = c("REDACT_1", "REDACT_2", "REDACT_3", "REDACT_4"),
    zindex = c(10, 5, 6, 99)
  )

# skipped -----

# line 261
  output$map <- renderGoogle_map({

    map <- google_map(key = global$google_api_key, location = c(52.936374, -3.060485), zoom = 6)

    print(layersList["layer_2",]$zindex)
    print(is.numeric(layersList["layer_2",]$zindex))
    zindex <- layersList["layer_2",]$zindex
    map <- map %>% add_polygons(polyData,
                                lat = "lat", lon = "long",
                                id = "id", pathId = "piece",
                                fill_colour = "Colour", stroke_colour = "Stroke",
                                layer_id = layersList["layer_2",]$id,
                                z_index = zindex)

I have tried a few different methods of passing a value into the z_index (the only thing that works is passing in a hard coded number or a globally scoped object).

Originally I tried passing in a value the exact same way as I am passing in a value for layer_id (which works for layer_id but not for z_index).

avsdev-cw commented 6 years ago

Forgot to mention:

platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          3                           
minor          5.1                         
year           2018                        
month          07                          
day            02                          
svn rev        74947                       
language       R                           
version.string R version 3.5.1 (2018-07-02)
nickname       Feather Spray  

Googleway version: 2.7.1

SymbolixAU commented 6 years ago

This line

 layersList["layer_2",]$zindex

givs NA

Do you mean

layersList[ layersList$id == "layer_2", ]$zindex

?

avsdev-cw commented 6 years ago

Sorry yes, that was a mistake on my part trying to redact some of the sensitive info out of it. A shorter test is to simply spin up a shiny app ad do something like:

(not tested, I don't have R installed on this machine)

library(shiny)
library(googleway)

ui <- fluidPage(
 google_mapOutput('map')
)

server <- function(input, output) {
  output$map <- renderGoogle_map({
    zIndex <- 5
    google_map(key = "your_api_key") %>% 
      add_polygons(data = melbourne, polyline = "polyline", z_index = zIndex)
  })
}
SymbolixAU commented 6 years ago

Thanks - yes I can reproduce the error.

SymbolixAU commented 6 years ago

Are you only seeing this issue inside a shiny environment?

avsdev-cw commented 6 years ago

Nope, the following should trigger the issue:

library(googleway)

testFunc <- function(){
  zIndex <- 5
  google_map(key = "your_api_key") %>% 
    add_polygons(data = melbourne, polyline = "polyline", z_index = zIndex)
}

testFunc()
SymbolixAU commented 6 years ago

yes. On its own, outside a function/scoped environment I see it works

  zIndex <- 5
  google_map(key = "your_api_key") %>% 
    add_polygons(data = melbourne, polyline = "polyline", z_index = zIndex)
avsdev-cw commented 6 years ago

Yes, there's a disparity between z_index and layer_id in that layer_id works scoped, but z_index does not

SymbolixAU commented 6 years ago

yeah - they're used differently inside the add_*() functions. I need to figure out where the z_index is getting lost in the system.

SymbolixAU commented 6 years ago

I've just pushed a change to the master branch which I think solves the problem. Would you mind testing it?

devtools::install_github("SymbolixAU/googleway")

At the moment this fix is only for add_polygon()

avsdev-cw commented 6 years ago

On the snippets it seems to work. I won't be able to make the modifications to the main code body and test it properly until office hours tomorrow GMT (in about 10 hours from now)

avsdev-cw commented 6 years ago

That modification fixes the issues I was having - Thanks!