Created
July 27, 2016 21:33
-
-
Save slopp/ae828419edb645b763de25aeea58bdec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sparklyr) | |
library(shiny) | |
library(leaflet) | |
library(readr) | |
library(ggplot2) | |
spark <- 0 | |
if(spark){ | |
Sys.setenv(SPARK_HOME = "/home/sean/.cache/spark/spark-1.6.2-bin-hadoop2.6") | |
sc <- spark_connect(master = "local", version = "1.6.2") | |
spark_read_csv(sc, "nyc_taxi_sample", path = "../../nathan/sol-eng-nyc-taxi-data/csv/trips/nyc_taxi_trips_2015-11.csv") | |
nyc_data <- tbl(sc, "nyc_taxi_sample") | |
m <- nyc_data %>% | |
filter(fare_amount > 0, | |
pickup_latitude > 0, | |
pickup_longitude < 0, | |
dropoff_latitude > 0, | |
dropoff_longitude < 0) %>% | |
ml_linear_regression(fare_amount ~ pickup_latitude + pickup_longitude + dropoff_latitude + dropoff_longitude) | |
}else { | |
nyc_data <- read_csv("testdata.csv") | |
m <- lm(data=nyc_data, fare_amount ~ pickup_latitude + pickup_longitude + dropoff_latitude + dropoff_longitude) | |
} | |
# Define UI for application that draws a histogram | |
ui <- fluidPage( | |
fluidRow( | |
leafletOutput("map"), | |
p("Click on Location First and Desired Destination Second"), | |
actionButton("go", "Predict Cost"), | |
textOutput("result") | |
) | |
) | |
server <- function(input, output) { | |
new_points <- data.frame(lat=c(), lng=c()) | |
output$map <- renderLeaflet({ | |
leaflet() %>% | |
fitBounds(lng1 = -73.97, lng2 = -73.85, lat1 = 40.68, lat2 = 40.77) %>% # NYC | |
addProviderTiles("Stamen.Toner") | |
}) | |
observeEvent(input$map_click, { | |
proxy <- leafletProxy("map") | |
data <- input$map_click | |
proxy %>% | |
addMarkers(lng = data$lng, lat = data$lat) | |
new_points <<- rbind(new_points, data) | |
}) | |
prediction <- eventReactive(input$go, { | |
new_data <- data.frame(pickup_latitude = new_points$lat[1], | |
pickup_longitude = new_points$lng[1], | |
dropoff_latitude = new_points$lat[2], | |
dropoff_longitude = new_points$lng[2]) | |
print(new_data) | |
predict(m, new_data) | |
}) | |
output$result <- renderText({ | |
num_trips <- nyc_data %>% count() | |
result <- paste("Your estimated trip fare is: $", round(prediction(),2), " based on", num_trips, "trips.") | |
result | |
}) | |
observeEvent(input$go, { | |
proxy <- leafletProxy("map") | |
proxy %>% | |
addCircles(lng = nyc_data$pickup_longitude, lat = nyc_data$pickup_latitude, col = "orange") %>% | |
addCircles(lng = nyc_data$dropoff_longitude, lat = nyc_data$dropoff_latitude, col = "green") | |
}) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment