I am trying to show maximum distance covered from swimming events for each person in leaflet map with "shinyapp" based on filtering a "Name" and a "SwimType".
Here is my code:
library(shiny)
library(dplyr)
library(shinydashboard)
library(leaflet)
library(osrm)
swimd <- read.csv('swim.csv', stringsAsFactors = F)
ui <- shinyUI(dashboardPage(
dashboardHeader(title = "Training"),
dashboardSidebar("A swim session"),
dashboardBody(
fluidPage(
box("",
leafletOutput("abc", height = 430),
width = 8, height = 450, background = 'black'),
box("",
selectInput('st', label = 'Swim Type:', choices = unique(swimd$SwimStyle)),
selectInput('pn', label = 'Name:', choices = unique(swimd$Name)),
width = 4, height = 450, background = 'black')
)
)
))
server <- shinyServer(function(input, output, session){
a <- reactive({
swimd %>%
select(Name, SwimStyle, Longitude, Latitude) %>%
filter( SwimStyle %in% input$st)
})
observe({
updateSelectInput(session,
inputId='pn',
choices = c("< select Name>"="", a()$Name ))
})
output$abc <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
setView(lng=0, lat=0, zoom = 2)
})
observe({
selection <- a() %>% filter(Name %in% input$pn)
selection$res <- distHaversine(selection$Longitude, selection$Latitude, r=6378137)
#to find the difference between two sequence values
selection[ , list(Name, SwimStyle, Longitude, Latitude,res,Diff=diff(res))]
b <- selection %>% arrange(desc(Diff)) %>% top_n(2)
leafletProxy("abc") %>% clearMarkers() %>% addMarkers(lat = b$Latitude, lng = b$Longitude) %>%
addPolylines(route$lon,route$lat,
label = paste(round(route_summary[1]), 'Minutes - ', round(route_summary[2]/1000), 'Meters'),
labelOptions = labelOptions(noHide = TRUE))
flyTo(lat = b$Latitude, lng = b$Longitude, zoom = 2)
})
})
shinyApp(ui,server)
In this app when the name and swim type filtered the maximum distance of swimming should be shown in map with 'polyline'. For an example a person finished 10 mins of swimming. For each 1 min of swimming we record the distance. I would like to show addPolyline for maximum distance covered in a min for each person by selecting a name from the dropdown list as created in this app.
Can someone help me on this use-case?
Thank you.