Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapview for shiny

Tags:

r

shiny

r-mapview

I create an interactive map using mapView

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

Code:

library(sp)
library(ggplot2)
library(gstat)
library(rgdal)
library(mapview)
library(RMySQL)

con <- dbConnect(MySQL(),
                 user="root",
                 password="",
                 host="127.0.0.1",
                 dbname="rstudio")
data<-dbReadTable(con,"data")
on.exit(dbDisconnect(con))

data_test <- data
data_test$x <- data$long
data_test$y <- data$lat
coordinates(data_test) = ~x + y
x.range <- as.numeric(c(-5.99, -5.74))  
y.range <- as.numeric(c(35.57, 35.81))  
grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.002), 
                   y = seq(from = y.range[1], to = y.range[2], by = 0.002))  # expand points to grid
coordinates(grd) <- ~x + y
gridded(grd) <- TRUE

idw <- idw(formula = temp ~ 1, locations = data_test, newdata = grd)
idw.output = as.data.frame(idw)  # output is defined as a data table

names(idw.output)[1:3] <- c("long", "lat", "temp")
idw.output <- idw.output[,1:3]

coordinates(idw.output) <- ~long+lat
morocco <- readOGR("Data/morocco/TNG", "TNG")
proj4string(idw.output)<-proj4string(morocco)
tempData <- idw.output[morocco,]
proj4string(data_test)<-proj4string(morocco)
gridded(tempData) <- TRUE
m<-mapView(tempData, zcol = "temp") + data_test
m

Result:

working part

Now

I want to move to shiny, the problem is that there is no render function for mapview. I've tried to use fpView() bView() but no result.

My try with Shiny

Code:

ui.R

library(shiny)
library(shinydashboard)
library(mapview)

header <- dashboardHeader(title="Ardusky")

sidebar <- dashboardSidebar(
)

body <- dashboardBody(
  # Define UI for application
  fluidPage(
    mainPanel(
      mapview:::fpViewOutput("mapplot"),
      mapview:::plainViewOutput("test")
    ))
)

ui <- dashboardPage(header, sidebar, body, skin="black")

server.R

library(shiny)
library(mapview)
library(ggplot2)
library(sp)
library(gstat)
library(rgdal)
library(RMySQL)

shinyServer(function(input, output, session) {

  repInput <- reactive({
                    con <- dbConnect(MySQL(),
                                     user="root",
                                     password="",
                                     host="127.0.0.1",
                                     dbname="rstudio")
                    data<-dbReadTable(con,"data")
                    on.exit(dbDisconnect(con))
                    data_test <- data
                    data_test$x <- data$long
                    data_test$y <- data$lat
                    coordinates(data_test) = ~x + y
                    x.range <- as.numeric(c(-5.99, -5.74))  
                    y.range <- as.numeric(c(35.57, 35.81))  
                    grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.002), 
                                       y = seq(from = y.range[1], to = y.range[2], by = 0.002))  # expand points to grid
                    coordinates(grd) <- ~x + y
                    gridded(grd) <- TRUE
                    idw <- idw(formula = temp ~ 1, locations = data_test, newdata = grd)
                    idw.output = as.data.frame(idw)  # output is defined as a data table
                    names(idw.output)[1:3] <- c("long", "lat", "temp")
                    idw.output <- idw.output[,1:3]
                    coordinates(idw.output) <- ~long+lat
                    morocco <- readOGR("/home/bloodesu/Data/morocco/TNG", "TNG")
                    proj4string(idw.output)<-proj4string(morocco)
                    tempData <- idw.output[morocco,]
                    proj4string(data_test)<-proj4string(morocco)
                    gridded(tempData) <- TRUE
                    tempData
  })

  output$mapplot <- mapview:::renderfpView({
    mapview:::fpView(repInput(), zcol = "temp")
  })

  output$test <- mapview:::renderPlainView({
    mapview:::plainview(repInput(), zcol = "temp")
  })


})

Result

enter image description here

Conclusion

As you can see only the plainView gives some acceptable results but without leaflet support

like image 358
Serhan Avatar asked Apr 17 '16 17:04

Serhan


People also ask

Is R shiny a visualization tool?

Interactive visualizationsShiny is designed for fully interactive visualization, using JavaScript libraries like d3, Leaflet, and Google Charts.


1 Answers

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.

like image 118
TimSalabim Avatar answered Oct 18 '22 19:10

TimSalabim