根据输入日期(R,Shiny)更改传单图上的数据

时间:2017-02-02 11:37:29

标签: r shiny leaflet

我正在尝试使用R创建一个Shiny应用程序,通过传单在地图上绘制坐标数据。数据还有日期戳,我希望能够绘制用户选择的特定日期坐标。我对R和Shiny都是全新的,所以所有的帮助都表示赞赏。以下是数据框;

的摘要
 Date    |   InitialLat | InitialLong |    NewLat   |  NewLong  |
13/05/16 |   53.477403  | -2.230932   |  51.527953  | -0.13216  |
13/05/16 |   53.490599  | -2.312568   |  53.485655  | -2.237405 |
14/05/16 |   53.371535  | -2.23148    |  53.32803   | -2.246991 |
14/05/16 |   53.371535  | -2.23148    |  53.32803   | -2.246991 |
15/05/16 |   53.371535  | -2.23148    |  53.32803   | -2.246991 |
15/05/16 |   53.371535  | -2.23148    |  53.32803   | -2.246991 |
16/05/16 |   53.478316  | -2.23270    |  53.42814   | -2.17458  |
16/05/16 |   53.48868   | -2.21839    |  53.47737   | -2.23091  |

到目前为止我的代码:

library(shiny)
library(leaflet)

cleanData <- read.csv(file="CleanedJourneyData.csv", header=TRUE, sep=",")
cleanData$X <- NULL

ui <- fluidPage(
  dateInput(inputId = "n_date", label="Select a date", value = "2016-05-13", min = "2016-05-13", max = "2016-10-24",
            format = "dd-mm-yyyy", startview = "month",
            language = "en", width = NULL),
  leafletOutput("map")

)

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

  dailyData <- reactive(cleanData[cleanData$Date == format(input$n_date, '%d/%m/%y')] )

  output$map <- renderLeaflet({  leaflet(dailyData) %>% addTiles() %>% 
      addMarkers(~InitialLong, ~InitialLat, popup = "Start")  })
}

shinyApp(ui, server)

问题出在这一行;

dailyData <- reactive(cleanData[cleanData$Date == format(input$n_date, '%d/%m/%y')] )

我得到的错误是:

Error: no applicable method for 'doResolveFormula' applied to an object of class "reactive"

我想要实现的是;

  • 用户选择日期
  • 日期格式更改为'%d /%m /%y'
  • 然后使用输入的日期搜索数据框(cleanData)以创建一个新的数据框(dailyData),其中只包含所选日期的纬度/经度坐标< / LI>
  • 将修剪后的数据框输入到传单地图中并显示

日期输入工作正常,我可以以正确的格式获取日期,但是当我尝试使用它来搜索cleanData来创建dailyData时它不起作用,我无法弄明白。我究竟做错了什么?为了记录,我能够在Shiny之外使用它 - 我在代码中手动更改了日期,并通过传单绘制了相应的坐标。

2 个答案:

答案 0 :(得分:1)

我修改了你的代码。看看这是你真正想要的。

library(shiny)
library(leaflet)

cleanData <- read.csv(file="D:/CleanedJourneyData.csv", header=TRUE, sep=",")
cleanData$X <- NULL

ui <- fluidPage(
  dateInput(inputId = "n_date", label="Select a date", value = "2016-05-13", min = "2016-05-13", max = "2016-10-24",
            format = "dd-mm-yyyy", startview = "month",
            language = "en", width = NULL),
  leafletOutput("map")

)

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

  dailyData <- reactive(cleanData[cleanData$Date == format(input$n_date, '%d/%m/%y'), ] )

  # I have implemented the change here, instead of using dailyData, I've used isolate(dailyData())
  output$map <- renderLeaflet({ leaflet(isolate(dailyData())) %>% addTiles() %>% 
                                   addMarkers(~InitialLong, ~InitialLat, popup = "Start")  })

}

shinyApp(ui, server)

希望它有所帮助!

答案 1 :(得分:0)

在精通R的同事的帮助下,我找到了问题的答案;

cleanData <- read.csv(file="/Users/CharlesPowell/R/CleanedJourneyData.csv", header=TRUE, sep=",")
cleanData$X <- NULL

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

ui <- fluidPage(
  dateInput(inputId = "n_date", label="Select a date", value = "2016-05-13", min = "2016-05-13", max = "2016-10-24",
            format = "dd-mm-yyyy", startview = "month",
            language = "en", width = NULL),
  leafletOutput("map")

)

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

  numrow <- reactive({cleanData$Date == format(input$n_date, '%d/%m/%y')})
  dailyData <- reactive({cbind(cleanData[numrow(),], Observation = seq(1:sum(numrow())))})

  z <- reactive({as.data.frame(gather(data=dailyData()[, 2:dim(dailyData())[2]], measure, val, -Observation) %>% group_by(Observation) %>%
    do(data.frame(   lat=c(.[["val"]][.[["measure"]]=="InitialLat"],
                           .[["val"]][.[["measure"]]=="NewLat"]),
                     long = c(.[["val"]][.[["measure"]]=="InitialLong"],
                              .[["val"]][.[["measure"]]=="NewLong"]))))})

  y <- reactive({points_to_line(z(), "long", "lat", "Observation")})

  output$map <- renderLeaflet({ leaflet(dailyData()) %>% addTiles() %>% 
      addMarkers(~InitialLong, ~InitialLat, popup = "Start") %>%
    addPolylines(data = y()) })

}

shinyApp(ui, server)

你可以看到我还添加了使用Kyle Walker创建的函数创建点对点线的功能。 https://rpubs.com/walkerke/points_to_line

所以现在当我更改输入的日期时,该日期的相应数据被绘制在传单地图上,其中的线条连接起点和终点。