From 0e00d8d5a3a164985f0c13bed1ae7b19cc3c9d3f Mon Sep 17 00:00:00 2001 From: SymbolixAU Date: Thu, 28 Jun 2018 14:29:25 +1000 Subject: [PATCH] updated code & docs for issue #167 --- R/google_places.R | 71 ++++++++++++++++++++++++-------------------- R/parameter_checks.R | 21 ++++++++++--- man/google_places.Rd | 4 +-- 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/R/google_places.R b/R/google_places.R index 201adfed..1b1da9a2 100644 --- a/R/google_places.R +++ b/R/google_places.R @@ -28,9 +28,9 @@ #' @param radius \code{numeric} Defines the distance (in meters) within which to #' return place results. Required if only a \code{location} search is specified. #' The maximum allowed radius is 50,000 meters. Radius must not be included if -#' \code{rankby="distance"} is specified. see Details. +#' \code{rankby} is used. see Details. #' @param rankby \code{string} Specifies the order in which results are listed. -#' Possible values are \code{"prominence"}, \code{"distance"} or \code{"location"}. +#' Possible values are \code{"prominence"} or \code{"distance"}. #' If \code{rankby = distance}, then one of \code{keyword}, \code{name} or #' \code{place_type} must be specified. If a \code{search_string} is used then #' \code{rankby} is ignored. @@ -138,22 +138,23 @@ #' } #' @export #' -google_places <- function(search_string = NULL, - location = NULL, - radar = FALSE, - radius = NULL, - rankby = NULL, - keyword = NULL, - language = NULL, - name = NULL, - place_type = NULL, - price_range = NULL, - open_now = NULL, - page_token = NULL, - simplify = TRUE, - curl_proxy = NULL, - key = get_api_key("places") - ){ +google_places <- function( + search_string = NULL, + location = NULL, + radar = FALSE, + radius = NULL, + rankby = NULL, + keyword = NULL, + language = NULL, + name = NULL, + place_type = NULL, + price_range = NULL, + open_now = NULL, + page_token = NULL, + simplify = TRUE, + curl_proxy = NULL, + key = get_api_key("places") + ) { ## check if both search_string & location == NULL if(is.null(search_string) & is.null(location)) @@ -182,7 +183,7 @@ google_places <- function(search_string = NULL, ## construct the URL ## if search string is specified, use the 'textsearch' url ## if no search_string, use the 'lat/lon' url - if(isTRUE(radar)){ + if (isTRUE( radar )) { map_url <- "https://maps.googleapis.com/maps/api/place/radarsearch/json?" }else{ if(!is.null(search_string)){ @@ -193,19 +194,25 @@ google_places <- function(search_string = NULL, } } - map_url <- constructURL(map_url, c("location" = location, - "radius" = radius, - "rankby" = rankby, - "keyword" = keyword, - "language" = language, - "name" = name, - "type" = place_type, - "minprice" = price_range[1], - "maxprice" = price_range[2], - "opennow" = open_now, - "pagetoken" = page_token, - "key" = key)) + map_url <- constructURL( + map_url + , c("location" = location + , "radius" = radius + , "rankby" = rankby + , "keyword" = keyword + , "language" = language + , "name" = name + , "type" = place_type + , "minprice" = price_range[1] + , "maxprice" = price_range[2] + , "opennow" = open_now + , "pagetoken" = page_token + , "key" = key + ) + ) - return(downloadData(map_url, simplify, curl_proxy)) + return( + downloadData(map_url, simplify, curl_proxy) + ) } diff --git a/R/parameter_checks.R b/R/parameter_checks.R index 2b454912..a6331b70 100644 --- a/R/parameter_checks.R +++ b/R/parameter_checks.R @@ -371,12 +371,25 @@ validateHeading <- function(heading){ validateLocationSearch <- function(location, search_string, radius, rankby, keyword, name, place_type){ if(is.null(location)) return(NULL) + ## if using rankby="distance" + if(!is.null(rankby)) { + if(rankby == "distance") { + if(!is.null(radius)) { + stop("If using rankby, radius must not be specified") + } + } + } + + if(!is.null(radius) && !is.null(rankby)) { + stop("rankby must not be included if radius is specified") + } + ## radius must be included if using a location search - if(is.null(search_string) & !is.null(location) & is.null(radius)) + if(is.null(search_string) && is.null(radius) & is.null(rankby)) stop("you must specify a radius if only using a 'location' search") ## if rankby == distance, then one of keyword, name or place_type must be specified - if(!is.null(rankby) & !is.null(location)){ + if(!is.null(rankby)){ if(rankby == "distance" & is.null(keyword) & is.null(name) & is.null(place_type)) stop("you have specified rankby to be 'distance', so you must provide one of 'keyword','name' or 'place_type'") @@ -517,8 +530,8 @@ validateRankBy <- function(rankby, location, search_string){ ## rankby has correct arguments if(!is.null(location)) - if(!rankby %in% c("prominence","distance","location")) - stop("rankby must be one of either prominence, distance or location") + if(!rankby %in% c("prominence","distance")) + stop("rankby must be one of either prominence or distance") ## warning if rankby used with search_string if(!is.null(search_string)) diff --git a/man/google_places.Rd b/man/google_places.Rd index cf63c703..f478a263 100644 --- a/man/google_places.Rd +++ b/man/google_places.Rd @@ -29,10 +29,10 @@ A radar search must contain a \code{location} and \code{radius}, and one of \cod \item{radius}{\code{numeric} Defines the distance (in meters) within which to return place results. Required if only a \code{location} search is specified. The maximum allowed radius is 50,000 meters. Radius must not be included if -\code{rankby="distance"} is specified. see Details.} +\code{rankby} is used. see Details.} \item{rankby}{\code{string} Specifies the order in which results are listed. -Possible values are \code{"prominence"}, \code{"distance"} or \code{"location"}. +Possible values are \code{"prominence"} or \code{"distance"}. If \code{rankby = distance}, then one of \code{keyword}, \code{name} or \code{place_type} must be specified. If a \code{search_string} is used then \code{rankby} is ignored.}