1

I am trying my hands at grabbing data from an API in R.

The API I am using is the Data Science Toolkit's street2coordinates.

Basically this API returns the coordinates for street addresses. I tried using it and it works fine when I pass just one address. Like This:

library(httr)

GET("http://www.datasciencetoolkit.org/street2coordinates/2543+Graystone+Place%2c+Simi+Valley%2c+CA+93065")

I am not sure how to pass multiple addresses to this function.

I tried doing this but the output is something I don't understand. It's a weird list.

addresses <- c("4600 Vegas Dr, Las Vegas, NV 89108","3600 Vegas Dr, Las Vegas, NV 89108")
url <- GET("http://www.datasciencetoolkit.org/street2coordinates/addresses")

Long story short, I am looking for a way to pass more than one address in the GET function. Is there a way to do this?

ashwin agrawal
  • 1,603
  • 8
  • 16
Jeet
  • 188
  • 12
  • What's the output that you don't understand? In your `GET` call, you haven't included anything that would insert the list of addresses—you're just looking up the coordinates of the address "addresses", which isn't going to work – camille Oct 27 '19 at 00:35

2 Answers2

0

Looking at your code, you cannot just directly pass your addresses as a string in the GET function, as it will read it as a string, but actually you addresses is a list.

If you want to get all the coordinates then a simple loop will do:

library(httr)
addresses <- c("4600 Vegas Dr, Las Vegas, NV 89108","3600 Vegas Dr, Las Vegas, NV 89108")

for(i in (1:length(addresses))){
  print(GET(paste("http://www.datasciencetoolkit.org/street2coordinates/",addresses[i])))
}

output

Response [http://www.datasciencetoolkit.org/street2coordinates/ 4600 Vegas Dr, Las Vegas, NV 89108]
  Date: 2019-10-26 20:00
  Status: 500
  Content-Type: text/html;charset=utf-8
  Size: 61 B
{
  "error": "Empty string passed in to street2coordinates"
Response [http://www.datasciencetoolkit.org/street2coordinates/ 3600 Vegas Dr, Las Vegas, NV 89108]
  Date: 2019-10-26 20:00
  Status: 500
  Content-Type: text/html;charset=utf-8
  Size: 61 B
{
  "error": "Empty string passed in to street2coordinates"

But still the address you provided in addresses is giving error while querying the API. So please check your addresses and run the above code.

ashwin agrawal
  • 1,603
  • 8
  • 16
  • There are a couple issues here. Less important is that you don't need a for-loop, since R has built-in tools like the `apply` family for iterating over vectors. Right now you're not returning anything, just printing the HTTP response. Also, you're getting errors that point to the fact that the address you're looking up is `" "`. If your calls had been successful, you'd have 200 status codes, not 500s – camille Oct 27 '19 at 00:40
0

You need to reformat your addresses so they can be read by the API.

Looks like they were replacing the commas with %2c+, and the spaces with +.

library(httr)
library(tidyverse)

base_url <- "http://www.datasciencetoolkit.org/street2coordinates/" 
look_up <- c("4600+Vegas+Dr%2c+Las+Vegas%2c+NV+89108", "3600+Vegas+Dr%2c+Las+Vegas%2c+NV+89108", "2543+Graystone+Place%2c+Simi+Valley%2c+CA+93065")

full_url <- map2_chr(base_url, look_up, paste0)

out <- map(full_url, GET) %>% 
  map_dfr(., ~ {
  content(., as = 'parsed') %>% 
    unlist() %>% 
    enframe() %>% 
      extract(col = name, into =  "key", regex = "([^.]*$)", remove = FALSE)
    })

Hope this helps.

billash
  • 172
  • 1
  • 7