本文介绍了根据输入的日期(R,闪亮)更改传单图上的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R创建一个Shiny应用程序,该应用程序通过传单在地图上绘制坐标数据.该数据还带有日期戳,我希望能够绘制出用户选择的特定日期的坐标.我是R和Shiny的新手,因此感谢所有帮助.这是数据帧;

I am attempting to create a Shiny app using R that plots coordinate data on a map via leaflet. The data also has date stamps, and I want to be able to plot a specific days' coordinates as chosen by the user. I'm brand new to both R and Shiny so all help is appreciated. Here is a snippet of the dataframe;

 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  |

到目前为止,我的代码:

My code so far:

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),该数据仅包含所选日期的经纬度坐标
  • 修剪后的数据框被输入到传单地图中并显示
  • the user selects a date
  • the format of the date is changed to '%d/%m/%y'
  • the inputted date is then used to search the dataframe (cleanData) to create a new dataframe (dailyData) of just the lat/long coordinates from that selected date
  • the trimmed down dataframe is inputted into the leaflet map and displayed

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

The date input works fine, I can get the date in the right format, but when I try and use this to search cleanData to create the dailyData it doesn't work, and I cannot figure it out. What am I doing wrong? For the record I was able to get this to work outside of Shiny - I manually changed the date in the code and the corresponding coordinates were plotted through the leaflet.

推荐答案

我已经修改了您的代码.请看看这是否是您真正想要的.

I have modified your code. Just have a look if this is what you really want.

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)

希望有帮助!

这篇关于根据输入的日期(R,闪亮)更改传单图上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:16