【发布时间】:2021-09-25 14:44:36
【问题描述】:
我正计划制作一个热图,显示在 R Shiny 上选定时间段内深度活动的变化。我目前遇到的问题是热图不会随着时间而改变。它一遍又一遍地显示初始情节。
这是我正在使用的数据集。它来自quake 数据集,经过一些修改。我将这个数据集命名为quakes_mod.csv
X1 lat long depth mag stations quakes_cat time
1 -20.42 181.62 562 4.8 41 High Depth 2020-12-04 05:45:32
2 -20.62 181.03 650 4.2 15 High Depth 2020-12-04 05:45:32
3 -26.00 184.10 42 5.4 43 No Depth 2020-12-04 05:45:32
4 -17.97 181.66 626 4.1 19 High Depth 2020-12-04 05:45:32
5 -20.42 181.96 649 4.0 11 High Depth 2020-12-04 05:45:32
6 -19.68 184.31 195 4.0 12 Low Depth 2020-12-04 05:45:32
7 -11.70 166.10 82 4.8 43 No Depth 2020-12-04 05:45:32
8 -28.11 181.93 194 4.4 15 Low Depth 2020-12-04 05:45:32
9 -28.74 181.74 211 4.7 35 Low Depth 2020-12-04 05:45:32
10 -17.47 179.59 622 4.3 19 High Depth 2020-12-01 08:22:42
在glimpse() 上,quakes_cat 是 factor 类型,而 time 在太平洋标准时间是 dttm
下面是我完整的 R 闪亮代码
library(shiny)
library(xts)
library(leaflet)
library(dplyr)
df<-read_csv('data/quakes_mod.csv')%>%
mutate(time=as.POSIXct(time))
ui <- bootstrapPage(
tags$style(type = "text/css", "html,
body {width:100%;height:100%}"),
leafletOutput("basemap", width= "100%", height = "100%"),
absolutePanel(
sliderInput(
"timeRange", label = "Choose Time Range:",
min = as.POSIXct("2020-12-01 00:00:00"),
max = as.POSIXct("2020-12-31 23:59:59"),
value = c(as.POSIXct("2020-12-01 00:00:00"), as.POSIXct("2020-12-04 23:59:59")),
timeFormat = "%Y-%m-%d %H:%M", timezone='PST', ticks = F, animate = T
), draggable = TRUE, top = "80%", left = "40%")
)
server <- function(input, output, session) {
#filter the traffic data based on time selected by user
filtered <- reactive({
df[df$time>=input$timeRange[1]& df$time<=input$timeRange[2],]
})
#initial static content along with leaflet the way it should be initially
output$basemap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron)
})
#updating the markers real time
observeEvent(input$timeRange,
leafletProxy("basemap", data=filtered()) %>%
clearHeatmap() %>%
addHeatmap(lng=df$long,lat=df$lat,
max=3,radius=3,blur=3,intensity=df$quakes_cat,gradient= "OrRd")
)
}
shinyApp(ui, server)
但是,在观察我的热图后,什么都没有改变。热图问题不会根据滚动条中选择的时间而改变。它只是求助于最初绘制的热图。我知道depth 中的一些值已更改,有人可以提供帮助吗?谢谢。
【问题讨论】:
标签: r time shiny leaflet heatmap