Wednesday, November 20, 2013
Thursday, November 7, 2013
Driving distance using Google Distance Matrix API
library(RCurl)
library(RJSONIO)
library(plyr)
distbase <- "http://maps.googleapis.com/maps/api/distancematrix/json?&origins="
url1 <- function(origin , destination , sep = "") {
u2 <- paste(distbase, origin ,"&destinations=",
destination ,"&mode=driving&units=imperial&language=EN&sensor=false")
return(URLencode(u2))
}
drive <- function(origin, destination,verbose=FALSE) {
if(verbose) cat(origin,destinaiton,"\n")
u3 <- url1(origin,destination)
doc <- getURL(u3)
x <- fromJSON(doc,simplify = FALSE)
if(x$status=="OK") {
dist <- x$rows[[1]]$elements[[1]]$distance$text
return(c(dist))
} else {
return(c(NA))
}
}
###example
drive("new york ny", "chicago il")
#############################################################
############ ui.R
#library(shiny)
# Define UI for miles per gallon application
#shinyUI(pageWithSidebar(
# Application title
# headerPanel("Miles Per Gallon"),
# sidebarPanel(),
# mainPanel()
#))
#library(shiny)
# Define UI for application that plots random distributions
#shinyUI(pageWithSidebar(
# Application title
# headerPanel("Hello Shiny!"),
# Sidebar with a slider input for number of observations
# sidebarPanel(
# sliderInput("obs",
# "Number of observations:",
# min = 0,
# max = 1000,
# value = 500)
# ),
# Show a plot of the generated distribution
# mainPanel(
# plotOutput("distPlot")
# )
#))
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Calculate driving distances here!"),
sidebarPanel(
textInput("from_city", "From City", "") ,
textInput("to_city", "To City", "") ,
helpText("Note: Distances are calculated using Google Distance Matrix API"),
submitButton("Calculate")
),
mainPanel(
h4 ("Driving Distance is:"),
#verbatimTextOutput("text"),
#h4 ("Cities"),
#verbatimTextOutput("caption") ,
#h4("Summary Final"),
verbatimTextOutput("summary")
)
))
########################################################################################################## server.R
library(RCurl)
library(RJSONIO)
library(plyr)
library(shiny)
shinyServer(function(input,output) {
formulaText <- reactive({
paste("Driving distance between", input$from_city,"and" , input$to_city )
})
funcInput <- reactive ({driving.distance(input$from_city, input$to_city)})
output$text <- renderText({
"foo bar"
})
output$caption <- renderText({
formulaText()
})
output$summary <- renderText({
funcInput()
})
})
Sunday, October 20, 2013
Subscribe to:
Comments (Atom)