I've written a function that will pull geospatial data from the Google Maps API and store the 1) name 2) coordinates in a dataframe using the googleway function 'google_places'.
Google_places uses a "next page token" for a total of 3 API calls for one full search.
When pulled out of the function, this code runs perfectly and returns a 60 row dataframe.
However, when I run this function using the appropriate arguments, it returns a dataframe of only 40 results.
I have been explicit in my code to run all three calls necessary, not just two.
I am not sure why this code works outside the function, but not inside the function.
Does anyone know what is happening here?
Again, this is using the googleway google_places function. https://rdrr.io/cran/googleway/man/google_places.html
Thank you! This is the code:
i. First set the conditions used in the search
search_string <- "Urgent care center"
key <- key #my API key
radius <- 50000
location <- L1
#L1 is a numeric vector of coordinates [1] 39 -105
search <- google_places(search_string, key, location, radius)
ii. Function to initialize the search by creating the dataframe from one search call (there are three total search calls).
thin_df <- function(search){
a <- search$results$name
b <- search$results$geometry$location$lat
c <- search$results$geometry$location$lng
thin_df <- data.frame(a, b, c)
return(thin_df)}
iii. In this function a 'central df' combines the three 'thin df' call results, creating a full search for the first coordinate pair using the arguments previously defined and the thin_df function.
full_search <- function(search_string,
key, location, radius){
call_1 <- google_places(search_string,
key,
location,
radius)
thin1 <- thin_df(call_1)
call_2 <- google_places(search_string = search_string,
page_token = call_1$next_page_token,
key = key,
location = location,
radius = radius)
thin2 <- thin_df(call_2)
full_df <- rbind(thin1, thin2)
call_3 <- google_places(search_string,
page_token = call_2$next_page_token,
key,
location,
radius)
thin3 <- thin_df(call_3)
central_df <- rbind(full_df, thin3)
return(central_df)
}
central_df <- full_search(("Urgent care center", key, L1, 50000)