gpt4 book ai didi

r - 根据 Shinydashboard 中的计时器更新传单标记

转载 作者:行者123 更新时间:2023-12-04 10:49:00 25 4
gpt4 key购买 nike

我有数据框 df其中有两个变量 latlon ,现在我需要创建一个 Shinydashboard,它通过在每个 10 seconds 之后从数据框中获取下一行值来更新 map 。 .
df

 df <- data.frame("Lat" = c(12.8882, 12.890, 12.891), "Lon" = c(77.58195,77.58190,77.581958))
Ui.R
library(shiny)
library(leaflet)
shinyUI( fluidPage(
leafletOutput("map1")
)
)
server.R
library(shiny)

shinyServer(function(input, output, session) {

output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=df$lon, lat=df$lat)})
})

我唯一知道的是我可以使用 invalidateLater()调用计时器,但我不知道如何为数据帧中的行的增量读取实现它。
Expected Result
我需要一张 map 在哪里 marker每隔 10 Seconds 移动到下一个位置, 移动标记的坐标通过数据框 df给出.

最佳答案

您可以使用 reactiveVal()跟踪当前显示的标记,并使用 observe()结合 invalidateLater()leafletProxy()删除前一个标记并添加新标记。为此,我们可以给图层一个 layerId每次我们添加标记时,我们可以在绘制下一个标记时使用它再次删除标记。

下面给出了一个工作示例,我添加了一些注释来说明正在发生的事情。希望这可以帮助!

enter image description here

library(shiny)
library(leaflet)
set.seed(1)

df <- cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)

ui <- fluidPage(
leafletOutput("mymap")
)

server <- function(input, output, session) {

# Create the base map
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Stamen.TonerLite,
options = providerTileOptions(noWrap = TRUE)
) %>%
setView(lng = mean(rnorm(1000) * 2 + 13), lat = mean(rnorm(1000) + 48), zoom = 7)
})

# Initialize a reactiveVal to keep track of which point is currently selected
point_to_plot <- reactiveVal(1)

observe({
# invalidate every 2 seconds
invalidateLater(2000)
isolate({
# update point_to_plot() to next value. If next value is higher than the amount of rows
# in df, set it to 1.
point_to_plot(ifelse(point_to_plot()+1<=nrow(df),point_to_plot()+1,1))
# Use leafletProxy to remove our previous marker, and add the new one.
leafletProxy('mymap') %>%
removeMarker('my_marker') %>%
addMarkers(layerId = 'my_marker',data = df[point_to_plot(),,drop=F])
})
})

}

shinyApp(ui, server)

EDIT: Working example with your data:


library(shiny)
library(leaflet)
set.seed(1)

df <- data.frame("Lat" = c(12.8882, 12.890, 12.891), "Lon" = c(77.58195,77.58190,77.581958))

ui <- fluidPage(
leafletOutput("mymap")
)

server <- function(input, output, session) {

# Create the base map
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Stamen.TonerLite,
options = providerTileOptions(noWrap = TRUE)
) %>%
setView(lat = 12.89, lng = 77.58195, zoom = 14)
})

# Initialize a reactieVal to keep trakc of which point is currently selected
point_to_plot <- reactiveVal(1)

observe({
# invalidate every 2 seconds
invalidateLater(2000)
isolate({
# update point_to_plot() to next value. If next value is higher than the amount of rows
# in df, set it to 1.
point_to_plot(ifelse(point_to_plot()+1<=nrow(df),point_to_plot()+1,1))
# Use leafletProxy to remove our previous marker, and add the new one.
leafletProxy('mymap') %>%
removeMarker('my_marker') %>%
addMarkers(layerId = 'my_marker',data = df[point_to_plot(),,drop=F])
})
})

}

shinyApp(ui, server)

关于r - 根据 Shinydashboard 中的计时器更新传单标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50850360/

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