AJG*_*JGL 6 r leaflet shiny r-leaflet
我有 R 代码,它创建了一个传单地图,其中的点由addPolylines().
library(shiny)
library(leaflet)
station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")
df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)
colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")
myMap = leaflet() %>%
setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
addTiles()%>%
addCircles(data = df,
lng = ~ longitude, lat = ~ latitude,
color = ~ colour,
radius = 2000,
stroke = TRUE,
opacity = 5,
weight = 1,
fillColor = ~ colour,
fillOpacity = 1)
for(group in levels(df$group)){
myMap = addPolylines(myMap,
lng= ~ longitude,
lat= ~ latitude,
data = df[df$group == group,],
color= ~ colour,
weight = 3)
}
myMap
Run Code Online (Sandbox Code Playgroud)
这正是我想要的,它看起来像这样:
但是,当我将其放入 R 闪亮应用程序时,地图将不会出现。用户界面代码是:
fluidPage(
theme = shinythemes::shinytheme("yeti"),
titlePanel(title = "Polyline Map"))
mainPanel("",
helpText("This is the polyline map"),
hr(),
leafletOutput("myMap", height = 400, width = 600)
)
Run Code Online (Sandbox Code Playgroud)
服务器代码是:
function(input, output, session) {
output$myMap = renderLeaflet({
leaflet() %>%
setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
addTiles()%>%
addCircles(data = df,
lng = ~ longitude, lat = ~ latitude,
color = ~ colour,
radius = 4000,
stroke = TRUE,
opacity = 5,
weight = 1,
fillColor = ~ colour,
fillOpacity = 0.5)
for(group in levels(df$group)){
myMap = addPolylines(myMap,
lng= ~ longitude,
lat= ~ latitude,
data = df[df$group==group,],
color= ~ colour,
weight = 3)
}
}
)}
Run Code Online (Sandbox Code Playgroud)
全局代码是:
library(shiny)
library(leaflet)
station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")
df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)
colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会发生这种情况以及我可以做些什么来解决它?谢谢!
我能够通过两个非常小的调整让您的代码正常工作:
myMap您在函数中引用renderLeaflet但尚未定义,所以我将第一行修改为myMap <- leaflet() %>%renderLeaflet函数中返回任何内容,因此我myMap在for-loop.工作代码如下所示,希望对您有所帮助!
library(shiny)
library(leaflet)
station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")
df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)
colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")
ui <- fluidPage(
theme = shinythemes::shinytheme("yeti"),
titlePanel(title = "Polyline Map"),
mainPanel("",
helpText("This is the polyline map"),
hr(),
leafletOutput("myMap", height = 400, width = 600)
)
)
server <- function(input, output, session) {
output$myMap = renderLeaflet({
myMap <- leaflet() %>%
setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
addTiles()%>%
addCircles(data = df,
lng = ~ longitude, lat = ~ latitude,
color = ~ colour,
radius = 4000,
stroke = TRUE,
opacity = 5,
weight = 1,
fillColor = ~ colour,
fillOpacity = 0.5)
for(group in levels(df$group)){
myMap = addPolylines(myMap,
lng= ~ longitude,
lat= ~ latitude,
data = df[df$group==group,],
color= ~ colour,
weight = 3)
}
myMap
})
}
shinyApp(ui,server)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3165 次 |
| 最近记录: |