mapview for shiny

后端 未结 1 944
日久生厌
日久生厌 2021-01-01 20:04

I create an interactive map using mapView

the mapview() function works fine with my gridded data (SpatialPixelsDataFrame):

相关标签:
1条回答
  • 2021-01-01 21:10

    mapview and shiny are not naturally made for each other. However, mapview is based on leaflet so we can leverage shiny support from leaflet. The trick is to set up your map object using mapview and then calling the @map slot (the leaflet part) inside renderLeaflet()

    ui.R

    library(shiny)
    library(shinydashboard)
    library(mapview)
    
    header <- dashboardHeader(title="Ardusky")
    
    sidebar <- dashboardSidebar(
    )
    
    body <- dashboardBody(
      # Define UI for application
      fluidPage(
        mainPanel(
          leafletOutput("mapplot"),
          mapview:::plainViewOutput("test")
        ))
    )
    
    ui <- dashboardPage(header, sidebar, body, skin="black")
    

    server.ui

    library(shiny)
    library(mapview)
    library(sp)
    
    shinyServer(function(input, output, session) {
    
      data(meuse)
      coordinates(meuse) <- ~x+y
      proj4string(meuse) <- CRS("+init=epsg:28992")
    
      data(meuse.grid)
      coordinates(meuse.grid) <- ~x+y
      proj4string(meuse.grid) <- CRS("+init=epsg:28992")
      gridded(meuse.grid) <- TRUE
    
      m <- mapview(meuse.grid, zcol = "dist") + meuse
    
      output$mapplot <- renderLeaflet({
        m@map
      })
    
    })
    

    Does this solve your problem?


    UPDATE: I have just added mapviewOutput and renderMapview to the development version on github. This means that we can now skip the explicit call to the @map slot of the mapview object. So something like output$mapplot <- renderMapview(m) should work now.

    The development version of mapview can be installed with devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")

    UPDATE (2018/04/01): renderMapview and mapviewOutput currently do not work! Thus, calling renderLeaflet({ m@map }) is currently the way to use mapview with shiny.

    0 讨论(0)
提交回复
热议问题