I have the following code used to find the travel time between two locations. I am using vba to call the script which is why the command args shows up at the top but for testing purposes I am just setting the variables. This was working until today (didn't change anything) and now I keep getting this error once i run the results line: Error in rowXML[[dur]] : subscript out of bounds.
Does anyone have any idea what could be causing this or what it means?
Code:
#install and load necessary packages
#install.packages("gmapsdistance")
#install.packages("devtools")
args<-commandArgs(trailingOnly=T)
library("gmapsdistance")
library("devtools")
devtools::install_github("rodazuero/gmapsdistance")
#input variables from excel
orig <- args[1]
dest <- args[2]
filePath <- args[3]
api_key <- args[4]
orig <- "London"
dest <- "Paris"
filePath <- "C:/Users/gabby/Documents/SeniorYear/SeniorDesign/TravelTimes/Travel_Times.csv"
api_key <- "############################"
set.api.key(api_key)
#calls google maps and finds the time
results = gmapsdistance(origin = c(orig, dest), destination = c(dest, orig), mode = "driving", traffic_model = "best_guess",
key = api_key, combinations = "pairwise", shape = "wide")
#put results in a data frame
results2 <- data.frame(results)
#rename the column headings
names(results2) <- c("Origin","Destination", "Time", "X1","X2","Distance","X3","X4","Status")
#delete repeated origin/destination columns
results2$X1 <- NULL
results2$X2 <- NULL
results2$X3 <- NULL
results2$X4 <- NULL
#convert seconds to minutes
results2$Time <- results2$Time/60
#convert meters to miles
results2$Distance <- results2$Distance*0.000621371
#add extra column and input the current date/time for documentation
results2[,"Date"] <- NA
results2[1,"Date"] <- format(Sys.time(), "%a %b %d %X %Y %Z")
#write results2 to a csv file and save it in my folder
write.csv(results2, file = filePath)
I obtained an API key, reproduced your problem, and then stepped through the underlying function's source code line by line.
The error is caused by the following:
because the object
dur
contains only the following:Thus
rowXML[[dur]]
throws the error. I'm not sure where to point the finger, but very often API's change faster than the packages built around them.Nevertheless, you can still use the source code to get your result, as I did. It just takes a few more lines of code to clean up the results yourself:
Per your request in the comment, here's a bit more about what I did to get this.
First, take the arguments from the call to this function and create objects out of them (i.e. just run each argument as an individual command to create the objects). Next, load the
XML
andRcurl
libraries. Also, put your API key in an object calledkey
.After that you just take the source code of the function and run it line by line, skipping the part where the function call is defined. Along the way there are a small number of unused arguments which you can just create and set to
""
.