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?
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?
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.
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)))
}
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"
Querying a Web service API typically involves the following steps:
In the following example we will submit a request for police reported crime data from the UK Police API. The API uses both HTTP GET and POST requests and provides content in JSON data format.
The two key R packages for submitting HTTP requests to Web service APIs and parsing the content of the response are httr and jsonlite. Let’s load them into our R session. The tidyverse package is also loaded because it contains a suite of useful functions.
library(tidyverse) ; library(httr) ; library(jsonlite)
Then we’ll parse the JSON content and and convert it to a data frame.
df <- fromJSON(response, flatten = TRUE) %>%
data.frame()
%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) })