crazycapivara / openlayers

An R Interface to OpenLayers
Other
19 stars 3 forks source link

getDockerContainerName is not defined Shiny #13

Closed fliermanrw closed 5 years ago

fliermanrw commented 5 years ago

Hi,

The openlayer map does render in RStudio Viewer but does not render to Shiny dashboard. When rendering Ol to shiny dashboard, I get the following message:

Uncaught ReferenceError: getDockerContainerName is not defined
    at Object.renderValue (eval at <anonymous> (jquery.min.js:2), <anonymous>:475:17)
    at Object.renderValue (eval at <anonymous> (jquery.min.js:2), <anonymous>:822:25)
    at exports.OutputBinding.shinyBinding.renderValue (eval at <anonymous> (jquery.min.js:2), <anonymous>:516:20)
    at exports.OutputBinding.onValueChange (output_binding.js:16)
    at exports.OutputBinding.delegator.(/p/4407/anonymous function) [as onValueChange] (eval at <anonymous> (https://sub.domain.com/p/4407/shared/jquery.min.js:2:2651), <anonymous>:112:23)
    at OutputBindingAdapter.onValueChange (output_binding_adapter.js:21)
    at ShinyApp.receiveOutput (shinyapp.js:354)
    at ShinyApp.<anonymous> (shinyapp.js:566)
    at ShinyApp._sendMessagesToHandlers (shinyapp.js:551)
    at ShinyApp.dispatchMessage (shinyapp.js:537)

My code:

  draw_openlayers = function(state, ns, SP, nlMap){
    state$output$x <- renderOl(openlayers$create_map(SP, nlMap))

    out <- div(id = "jo", column(width = 12, olOutput("x", height = "800px")))
    state$output[[ns("onderzoek_ui")]] <- renderUI(
      out       
    )
  }

The "openlayers$create_map()" returns the Ol map (that works in viewer).

What is wrong?

crazycapivara commented 5 years ago

Does the shiny example work for you?

shiny-example

I just tested it and there everything works as expected, in this case you can also call getDockerContainerName() in the developer console. This function is only used when using drag-and-drop and layer switcher to give random layer names.

From the code snippet I cannot see what goes wrong. Maybe you can send the complete example.

fliermanrw commented 5 years ago

Thank you for the reply. I have tried to find the problem and it seems to occur when I try to render with renderUI (to a uiOutput). Please see the following:

library(shiny)
library(geojsonio)
library(openlayers)

view <- fluidPage(
  h1("openlayers"),
  olOutput("ol_map", height = "800px")

)

server <- function(input, output) {

  output$ol_map <- renderOl({
    ol(options = ol_options(debug = TRUE)) %>%
      add_stamen_tiles() %>%
      add_features(us_cities[1:10, ], style = circle_style()) %>%
      add_select() %>%
      add_drag_and_drop()

    # out <- div(id = "jo", column(width = 12, olOutput("ol_map", height = "800px")))
    # output$onderzoekui <- renderUI(
    #   
    #   olOutput("ol_map", height = "800px")
    # )
  })
}
shinyApp(view, server)

The code above shows correctly. This code however:

library(shiny)
library(geojsonio)
library(openlayers)

view <- fluidPage(
  h1("openlayers"),
  uiOutput("onderzoekui")
  #olOutput("ol_map", height = "800px")
  )

server <- function(input, output) {
  output$ol_map <- renderOl({
    ol(options = ol_options(debug = TRUE)) %>%
      add_stamen_tiles() %>%
      add_features(us_cities[1:10, ], style = circle_style()) %>%
      add_select() %>%
      add_drag_and_drop()

    # out <- div(id = "jo", column(width = 12, olOutput("ol_map", height = "800px")))
    output$onderzoekui <- renderUI(
      #also tried out
      olOutput("ol_map", height = "800px")
    )

  })
}
shinyApp(view, server)

So the problem seems to be that the olOutput cannot be rendered into a uiOutput.

crazycapivara commented 5 years ago

You are right, there was some strange behavior when rendering the widget via renderUI, so that the function getDockerContainerName was not available in the global namespace. I fixed the bug and merged it to master. Please install the latest version of openlayers for R (see releases) and try to run the example shiny-render-ui.R. It should work now.

fliermanrw commented 5 years ago

Awesome, thank you for fixing this bug!