仅显示一次地图(传单和闪亮)

时间:2019-03-17 18:41:55

标签: r dictionary shiny gps leaflet

我是Shiny的新手,我拥有gps数据,并希望在地图上很好地显示它。我需要制作依赖于时间的动画-基本上,是随着时间的推移在地图上跟踪个人。我在这里面临的问题是,当我单击时间滑块以运行(动画)时,每次有新的时间点时都会重新加载地图。我希望在打开应用程序时仅加载一次地图,并且随着时间的流逝,这些点会随着所选用户ID的出现而显示。我希望这很清楚。

library(shiny)
library(leaflet)
library(lubridate)
library(shinydashboard)
library(tidyverse)

latitude=c(37.4218, 37.4063)
longitude=c(-124.0831, -124.1190)
userid=c(1704, 1704)
time=c("2017-09-15 14:40:58", "2017-09-15 14:53:35")

## reproducible data 
geo<- as.data.frame(cbind(latitude,
        longitude, 
        userid, 
        time))

## data manipulation
geo<- geo %>% 
mutate(time = ymd_hms(time))%>% 
mutate(longitude=as.numeric(longitude), 
     latitude=as.numeric(latitude), 
     userid=as.numeric(userid))


region<- "US" ##"US" / "Boston" select region of initial view

if (region=="US") {
  lngset = -93.85
  latset = 37.45
  zoomset = 4
}
if (region=="Boston") {
  lngset = ## update
  latset = ## update
  zoomset = ## update
}

mindate<-min(geo$time)
maxdate<-max(geo$time)

IDS<-unique(geo$userid)

ui=fluidPage(
   titlePanel("title"),
   leafletOutput("mymap",height = 500),
   selectInput(inputId = "userid", 
          label="user id", 
          choices = IDS),
    sliderInput("date_range", 
          "Choose Date:", 
          min = mindate, 
          max = maxdate, 
          step = 1, ## by minute 
          value = mindate,
          animate = animationOptions(loop = TRUE, interval = 1000))
)




server <- function(input,output){

 display only location for an hour for a chosen used
 data <- reactive({
   x <- geo %>% ## 
     filter(time<=input$date_range & time>=input$date_range-hours(1), 
userid==input$userid)
 })


  output$mymap <- renderLeaflet({
   df <- data()

    m <- leaflet(data = df) %>%
     setView(lng = lngset, lat = latset, zoom = zoomset) %>% ## center 
     addTiles() %>%
     addCircles(lng = ~longitude,
                lat = ~latitude,
                 popup = paste("User", df$userid, "<br>",
                              "Year:", df$time))
    m


  })



}

## deploy the app
shinyApp(ui=ui, server=server)

1 个答案:

答案 0 :(得分:0)

我会尝试的:

library(shiny)
library(leaflet)
library(lubridate)
library(shinydashboard)
library(tidyverse)

latitude=c(37.4218, 37.4063)
longitude=c(-124.0831, -124.1190)
userid=c(1704, 1704)
time=c("2017-09-15 14:40:58", "2017-09-15 14:41:03")

## reproducible data 
geo<- as.data.frame(cbind(latitude,
                          longitude, 
                          userid, 
                          time),stringsAsFactors = F)

## data manipulation
geo<- geo %>% 
   mutate(time = ymd_hms(time)) %>%
   mutate(longitude=as.numeric(longitude), 
          latitude=as.numeric(latitude), 
       userid=as.numeric(userid))


region<- "US" ##"US" / "Boston" select region of initial view

if (region=="US") {
  lngset = -93.85
  latset = 37.45
  zoomset = 4
}
olddf <- data.frame()

mindate<-min(geo$time)
maxdate<-max(geo$time)+hours(1)

IDS<-unique(geo$userid)

ui=fluidPage(
  titlePanel("title"),
  leafletOutput("mymap",height = 500),
  selectInput(inputId = "userid", 
              label="user id", 
              choices = IDS),
  sliderInput("date_range", 
              "Choose Date:", 
              min = mindate, 
              max = maxdate, 
              step = 1, ## by minute 
              value = mindate,
              animate = animationOptions(loop = TRUE, interval = 1000))
)




server <- function(input,output){

  #display only location for an hour for a chosen used
  data <- reactive({
    x <- geo %>% ## 
      filter(time<=input$date_range & time>=input$date_range-hours(1), 
             userid==input$userid)
  })

 observe({
    df <- data()
    print(df)
    leafletProxy("mymap",data = df) %>%
    #  setView(lng = lngset, lat = latset) %>% ## center 
      addTiles() %>%
      addCircles(lng = ~longitude,
                 lat = ~latitude,
                 popup = paste("User", df$userid, "<br>",
                               "Year:", df$time))
  })
  output$mymap <- renderLeaflet({
    m <- leaflet() %>%
      setView(lng = lngset, lat = latset, zoom = zoomset) %>% ## center 
      addTiles() 
    m


   # m <- leaflet() %>%
    # #  setView(lng = lngset, lat = latset, zoom = zoomset) %>% ## center 
    #  addTiles() %>%
    #    addCircles(lng = ~longitude,
    #            lat = ~latitude,
                 #               #              popup = paste("User", df$userid, "<br>",
    #                     "Year:", df$time))
# 
#  m

  })



}

## deploy the app
shinyApp(ui=ui, server=server)
  1. factor转换为numeric时要小心。观察之间的差异

geo <-as.data.frame(cbind(latitude,         经度,         用户身份,         时间))

## data manipulation
geo<- geo %>% 
mutate(time = ymd_hms(time))%>% 
mutate(longitude=as.numeric(longitude), 
     latitude=as.numeric(latitude), 
     userid=as.numeric(userid))

## reproducible data 
geo<- as.data.frame(cbind(latitude,
                          longitude, 
                          userid, 
                          time),stringsAsFactors = F)

## data manipulation
geo<- geo %>% 
   mutate(time = ymd_hms(time)) %>%
   mutate(longitude=as.numeric(longitude), 
          latitude=as.numeric(latitude), 
       userid=as.numeric(userid))
  1. 要修改实时leaflet,请使用leafletProxyhttps://rstudio.github.io/leaflet/shiny.html)。我将setView(lng = lngset, lat = latset) %>% ## center设置为注释代码,因为我们不想每次用户缩放或移动时都重新绘制地图。

最好!