QBoard » Artificial Intelligence & ML » AI and ML - R » Pass Data for Querying an API in R?

Pass Data for Querying an API in R?

  • 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?

     
      November 2, 2021 2:58 PM IST
    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.

     
      November 3, 2021 2:05 PM IST
    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)))
    }​


    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.
      November 13, 2021 2:36 PM IST
    0
    1. submit the request
    2. check for any server error
    3. parse the response
    4. convert to a data frame

    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()
      November 22, 2021 12:19 PM IST
    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.
      November 22, 2021 6:18 PM IST
    0