将非地理mapview对象放置到Shiny应用程序中 [英] Place non-geographic mapview object into Shiny app
问题描述
基本上,我的问题是如何将PNG或mapview对象放置到闪亮的应用程序中,以便在其上放置标记.
Basically my question is how to place either a PNG or a mapview object into a shiny app to enable placement of markers over it.
我有15个非地理平面图的mapview对象(如上面的PNG),在另一个SO用户的帮助下,使用以下代码将其转换为mapview对象
I have 15 non-geographic floorplan mapview objects like PNG above that were converted to mapview objects using the code below with the assistance of another SO user question here:
library(raster)
library(png)
library(mapview)
ohs<-data.frame(OHS_no=c(1001:1010), x=runif(10, 0, 1), y = runif(10, 0, 0.8), AGE = c(4, 15, 15, 43, 5, 50, 67, 77, 77, 28))
web_img <- "http://i.stack.imgur.com/8aSe9.png"
png <- readPNG(readBin(web_img, "raw", 1e6))
rst_blue <- raster(png[, , 1])
rst_green <- raster(png[, , 2])
rst_red <- raster(png[, , 3])
img <- brick(rst_red, rst_green, rst_blue)
m <- viewRGB(img)
abs(cbind(rnorm(40), rnorm(40)))
m@map %>% addMarkers(lng = ohs$x, lat = ohs$y)
我正在尝试创建一个Shiny应用程序以将其全部组合在一起,但是卡在renderLeaflet命令中,并且不确定如何将我的m @ map对象放置在该应用程序中.基本上,下面的功能以map对象为传单.
I am trying to create a Shiny app to put it all together but am stuck at the renderLeaflet command and am not sure how to place my m@map object within the app. Basically the below functionality with the map object as the leaflet.
library(shiny)
library(leaflet)
ohs<-data.frame(OHS_no=c(1001:1010), x=runif(10, 0, 1), y = runif(10, 0, 0.8))
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
ui <- fluidPage(
leafletOutput("mymap"),
p(),
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles("Stamen.TonerLite",
options = providerTileOptions(noWrap = TRUE)
) %>%
addMarkers(lng = ohs$x, lat = ohs$y)
})
}
shinyApp(ui, server)
推荐答案
这对我有用:
library(shiny)
library(mapview)
library(png)
library(raster)
ohs<-data.frame(OHS_no=c(1001:1010), x=runif(10, 0, 1), y = runif(10, 0, 0.8))
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
ui <- fluidPage(
leafletOutput("mymap"),
p()
)
server <- function(input, output, session) {
web_img <- "http://i.stack.imgur.com/8aSe9.png"
png <- readPNG(readBin(web_img, "raw", 1e6))
rst_blue <- raster(png[, , 1])
rst_green <- raster(png[, , 2])
rst_red <- raster(png[, , 3])
img <- brick(rst_red, rst_green, rst_blue)
m <- viewRGB(img)
output$mymap <- renderLeaflet({
m@map %>%
addMarkers(lng = ohs$x, lat = ohs$y)
})
}
shinyApp(ui, server)
只需将leaflet()
替换为m@map
(这是一个传单小部件对象).鉴于栅格未进行地理配准,因此添加提供者图块没有任何意义.
Simply replace leaflet()
with m@map
(which is a leaflet widget object). Given that the raster is not georeferenced, it does not make sense to add provider tiles.
这篇关于将非地理mapview对象放置到Shiny应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!