闪亮的地图视图
mapview for shiny
我使用 mapView
创建了一个交互式地图
mapview()
函数可以很好地处理我的网格化数据 (SpatialPixelsDataFrame
):
代码:
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
结果:
现在
我想移动到shiny,问题是mapview没有渲染功能。
我试过使用 fpView() bView() 但没有结果。
我对 Shiny 的尝试
代码:
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")
})
})
结果
结论
如您所见,只有 plainView 提供了一些可接受的结果,但没有传单支持
mapview 和 shiny 并不是天生一对。然而,mapview 是基于 leaflet 所以我们可以利用 shiny 来自 leaflet 的支持。诀窍是使用 mapview 设置地图对象,然后在 [=13] 中调用 @map
槽(leaflet 部分) =]
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
})
})
这是否解决了您的问题?
更新:
我刚刚在 github 的开发版本中添加了 mapviewOutput
和 renderMapview
。这意味着我们现在可以跳过对 mapview 对象的 @map
槽的显式调用。所以像 output$mapplot <- renderMapview(m)
这样的东西现在应该可以工作了。
开发版mapview可以用devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")
安装
更新(2018/04/01):
renderMapview
和 mapviewOutput
目前不起作用!因此,调用 renderLeaflet({ m@map })
是目前使用 mapview with shiny 的方式。
我使用 mapView
mapview()
函数可以很好地处理我的网格化数据 (SpatialPixelsDataFrame
):
代码:
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
结果:
现在
我想移动到shiny,问题是mapview没有渲染功能。 我试过使用 fpView() bView() 但没有结果。
我对 Shiny 的尝试
代码:
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")
})
})
结果
结论
如您所见,只有 plainView 提供了一些可接受的结果,但没有传单支持
mapview 和 shiny 并不是天生一对。然而,mapview 是基于 leaflet 所以我们可以利用 shiny 来自 leaflet 的支持。诀窍是使用 mapview 设置地图对象,然后在 [=13] 中调用 @map
槽(leaflet 部分) =]
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
})
})
这是否解决了您的问题?
更新:
我刚刚在 github 的开发版本中添加了 mapviewOutput
和 renderMapview
。这意味着我们现在可以跳过对 mapview 对象的 @map
槽的显式调用。所以像 output$mapplot <- renderMapview(m)
这样的东西现在应该可以工作了。
开发版mapview可以用devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")
更新(2018/04/01):
renderMapview
和 mapviewOutput
目前不起作用!因此,调用 renderLeaflet({ m@map })
是目前使用 mapview with shiny 的方式。