1

I’m trying to have two spatial plots side-by-side in shiny, and I was suggested a powerful function, sync of mapview. After figuring how to display mapview object in shiny, I tried to integrate sync in 'shiny', but got the following error: Error in slot(x, "map") : no slot of name "map" for this object of class "shiny.tag.list" . Does it mean sync does not have map object, therefore, it is not possible to integrate sync or latticeView with shiny? If so, I guess there should be workaround solutions and my ears are all open. This is a nice feature to have access from Shiny and allows to do some interesting things. Greatly appreciate any suggestions. Here is the sample reproducible code:

library(shiny)
library(mapview)
ui <- fluidPage(
   mapviewOutput("samplemap"),
   p()
)
server <- function(input, output, session) {
   output$samplemap <- renderMapview({
      m1 <- mapview(gadmCHE,zcol="ID_1")
      m2 <- mapview(gadmCHE,zcol="OBJECTID")
      sync(m1,m2)
      })
}
shinyApp(ui, server)
Community
  • 1
  • 1
SatishR
  • 230
  • 3
  • 13

1 Answers1

2

We have discussed making the return value from sync an htmlwidget. Currently, sync returns a htmltools::tagList of the leaflet maps. Inserting tags into shiny will be a little different than inserting mapview. I'll try to explain in code below.

library(mapview)

m1 <- mapview(gadmCHE,zcol="ID_1")
s1 <- sync(m1,m1)


library(shiny)

# if not dynamically adding maps
#   we can just insert as ui

shinyApp(
  ui = s1,
  server = function(input,output){}
)

# if there is a need to create the maps after UI
#   then we will need to handle differently
#   since sync gives us tags instead of an htmlwidget
shinyApp(
  ui = uiOutput("syncmap"),
  server = function(input,output){
    output$syncmap = renderUI({
      s1
    })
  }
)
timelyportfolio
  • 6,479
  • 30
  • 33
  • Apologizes for missing the discussion part regarding `sync` returned objects.
    This is useful, however, the map was being displayed only when `sync` object inserted as `ui`. I am unable to add maps dynamically, i.e., the code with `renderUI` and `uiOutput` opened a window but with no map. Not sure why? Also, I would like to know whether I can create a `sync` object in an reactive expression using `renderLeaflet` objects and later use in `renderUI` and `'uiOutput` ? Greatly appreciate your suggestions.
    – SatishR Oct 28 '16 at 14:26
  • what versions of `shiny`, `htmltools`, and `htmlwidgets` do you have? – timelyportfolio Oct 28 '16 at 15:25
  • Here it is : `shiny` _0.14.1_ `htmltools` _0.3.5_ `htmlwidgets` _0.7_ – SatishR Oct 28 '16 at 15:32
  • can you open in a browser and use F12 or CTRL+Shift+I or left-click/inspect to see if there are any errors? – timelyportfolio Oct 28 '16 at 16:05
  • Just I want to mention that it is mac, and on chrome here [is screenshot](https://www.dropbox.com/s/52kr3h17fe245cx/ErrorSyncObjectDisplay.png?dl=0) I do see at least one error, but not sure what it means. – SatishR Oct 28 '16 at 16:20
  • not sure exactly, but can you try without too much hassle to do `install.packages(leaflet)` and `install.packages(mapview)` to get CRAN versions. – timelyportfolio Oct 28 '16 at 20:19
  • I'll do that, can u confirm that it is working just fine on your end ? Thanks – SatishR Oct 28 '16 at 20:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126941/discussion-between-satishr-and-timelyportfolio). – SatishR Oct 28 '16 at 22:41