gpt4 book ai didi

r - 为什么 addPolylines 在 R Shiny 传单 map 上的工作方式不同?

转载 作者:行者123 更新时间:2023-12-02 01:50:30 25 4
gpt4 key购买 nike

我有 R 代码,它创建一个传单 map ,其中的点通过 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

这正是我想要的,它看起来像这样:

enter image description here

但是,当我将其放入 R Shiny 应用程序中时, map 将不会出现。用户界面代码是:

fluidPage(

theme = shinythemes::shinytheme("yeti"),

titlePanel(title = "Polyline Map"))

mainPanel("",
helpText("This is the polyline map"),
hr(),
leafletOutput("myMap", height = 400, width = 600)

)

服务器代码是:

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)
}
}

)}

全局代码是:

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在你的renderLeaflet函数但尚未定义,所以我将第一行修改为 myMap <- leaflet() %>%
  • 您不会从 renderLeaflet 返回任何内容函数,所以我添加了语句 myMap for-loop之后.

工作代码如下所示,希望对您有所帮助!

<小时/>

enter image description here

<小时/>
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)

关于r - 为什么 addPolylines 在 R Shiny 传单 map 上的工作方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50101354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com