-
Notifications
You must be signed in to change notification settings - Fork 46
/
parameter_checks.R
703 lines (538 loc) · 19 KB
/
parameter_checks.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# Check hex colours
#
# Checks for valid hexadecimal value
#
# @param df \code{data.frame}
# @param cols string of columns to check
check_hex_colours <- function(df, cols){
for(myCol in cols){
if(!all(grepl("^#(?:[0-9a-fA-F]{3}){1,2}$", df[, myCol])))
stop(paste0("Incorrect colour specified in ", myCol, ". Make sure the colours in the column are valid hexadecimal HTML colours"))
}
}
# Check opacities
#
# Checks for valid opacity values
#
# @param df \code{data.frame}
# @param cols string of columns to check
check_opacities <- function(df, cols){
for(myCol in cols){
## allow NAs through
vals <- df[, myCol][!is.na(df[, myCol])]
if(length(vals) > 0)
if(any(vals < 0) | any(vals > 1))
stop(paste0("opacity values for ", myCol, " must be between 0 and 1"))
}
}
isHexColour <- function(cols){
hexPattern <- "^#(?:[0-9a-fA-F]{3}){1,2}$|^#(?:[0-9a-fA-F]{4}){1,2}$"
all(grepl(hexPattern, cols))
}
# Logical Check
#
# Checks the argument is length 1 logical
# @param arg
logicalCheck <- function(arg){
if(!is.null(arg)){
if(!is.logical(arg) | length(arg) != 1)
stop(paste0(deparse(substitute(arg))," must be logical - TRUE or FALSE"))
}
}
loadIntervalCheck <- function(load_interval){
numericCheck(load_interval)
if(!is.null(load_interval))
if(load_interval < 0)
stop("load_interval needs to be a positive number")
}
# Numeric Check
#
# Checks the argument is lenght 1 numeric
# @param arg
numericCheck <- function(arg){
if(!is.null(arg)){
if(!is.numeric(arg) | length(arg) != 1)
stop(paste0(deparse(substitute(arg)), " must be a single numeric value"))
}
}
### url check ------------------
urlCheck <- function(url) UseMethod("urlCheck")
#' @export
urlCheck.character <- function(url) {
if(!isUrl(url)) stop("invalid url")
}
#' @export
urlCheck.url <- function(url) url
#' @export
urlCheck.default <- function(url) stopMessage(url)
# hexType <- function(cols){
# rgb <- "^#(?:[0-9a-fA-F]{3}){1,2}$"
# rgba <- "^#(?:[0-9a-fA-F]{4}){1,2}$"
# }
# Check for columns
#
# Checks for valid columns
#
# @param df \code{data.frame}
# @param cols string of columns
check_for_columns <- function(df, cols){
## check to see if the specified columns exist
if(!all(cols %in% names(df)))
stop(paste0("Could not find columns: "
, paste0(cols[!cols %in% names(df)], collapse = ", ")
, " in the data"))
}
check_address <- function(address){
if(is.character(address) & length(address) == 1){
address <- gsub(" ", "+", address)
}else{
stop("address must be a string of length 1")
}
return(address)
}
check_location <- function(loc, type){
if(is.numeric(loc) & length(loc) == 2){
loc <- paste0(loc, collapse = ",")
}else if(is.character(loc) & length(loc) == 1){
loc <- gsub(" ", "+", loc)
}else{
stop(paste0(type, " must be either a numeric vector of lat/lon coordinates, or an address string"))
}
return(loc)
}
check_multiple_locations <- function(loc, type){
loc <- sapply(1:length(loc), function(x) {
check_location(loc[[x]], type)
})
loc <- paste0(loc, collapse = "|")
}
# Latitude column
#
# calls the correct function to check for latitude column
# @param data \code{data.frame}
# @param lat string identifying the latitude column
# @param calling_function the function that called this function
latitude_column <- function(data, lat, calling_function){
if(is.null(lat)){
lat_col <- find_lat_column(names(data), calling_function)
names(data)[names(data) == lat_col[1]] <- "lat"
}else{
## check the supplied latitude column exists
check_for_columns(data, lat)
# names(data)[names(data) == lat] <- "lat"
}
return(data)
}
# Longitude column
#
# calls the correct function to check for longitude column
# @param data \code{data.frame}
# @param lon string identifying the longitude column
# @param calling_function the function that called this function
longitude_column <- function(data, lon, calling_function){
if(is.null(lon)){
lon_col <- find_lon_column(names(data), calling_function)
names(data)[names(data) == lon_col[1]] <- "lng"
}else{
check_for_columns(data, lon)
# names(data)[names(data) == lon] <- "lng"
}
return(data)
}
# Find Lat Column
#
# Tries to identify the latitude column
# @param names string of column names
# @param calling_function the function that called this function
# @param stopOnFailure logical
find_lat_column <- function(names, calling_function, stopOnFailure = TRUE) {
lats = names[grep("^(lat|lats|latitude|latitudes|stop_lat|shape_pt_lat)$", names, ignore.case = TRUE)]
if (length(lats) == 1) {
# return(list(lat = lats))
return(lats)
}
if (stopOnFailure) {
stop(paste0("Couldn't infer latitude column for ", calling_function))
}
return(NA)
# list(lat = NA)
}
# Find Lon Column
#
# Tries to identify the longitude column
# @param names string of column names
# @param calling_function the function that called this function
# @param stopOnFailure logical
find_lon_column <- function(names, calling_function, stopOnFailure = TRUE) {
lons = names[grep("^(lon|lons|lng|lngs|long|longs|longitude|longitudes|stop_lon|shape_pt_lon)$", names, ignore.case = TRUE)]
if (length(lons) == 1) {
# return(list(lon = lons))
return(lons)
}
if (stopOnFailure) {
stop(paste0("Couldn't infer longitude columns for ", calling_function))
}
#list(lon = NA)
return(NA)
}
validateAlternatives <- function(alternatives){
if(is.null(alternatives)) return(NULL)
logicalCheck(alternatives)
return(tolower(alternatives))
}
validateArrivalTime <- function(arrival_time){
if(is.null(arrival_time)) return( NULL )
checkPosix(arrival_time)
return(arrival_time)
}
validateArrivalDepartureTimes <- function(arrival_time, departure_time){
if(!is.null(arrival_time) & !is.null(departure_time)){
warning("you have supplied both an arrival_time and a departure_time - only one is allowed. The arrival_time will be ignored")
return(NULL)
}
return(arrival_time)
}
validateAvoid <- function(avoid){
if(is.null(avoid)) return(NULL)
if(!all(tolower(avoid) %in% c("tolls","highways","ferries","indoor"))){
stop("avoid can only include tolls, highways, ferries or indoor")
}else{
if(length(avoid) > 1){
avoid <- paste0(tolower(avoid), collapse = "|")
}else{
avoid <- tolower(avoid)
}
}
return(avoid)
}
validateBounds <- function(bounds){
if(is.null(bounds)) return(NULL)
if(!inherits(bounds, "list") | !all(sapply(bounds, inherits, "numeric")) | length(bounds) != 2)
stop("bounds must be a list of length 2, each item being a vector of lat/lon coordinate pairs")
if(!all(sapply(bounds, length) == 2))
stop("each element of bounds must be length 2 - a pair of lat/lon coordinates")
bounds <- paste0(lapply(bounds, function(x) paste0(x, collapse = ",")), collapse = "|")
return(bounds)
}
validateComponents <- function(components){
if(is.null(components)) return(NULL)
if(!inherits(components, "data.frame") | !sum(names(components) %in% c("component","value")) == 2)
stop("components must be a data.frame with two columns named 'component' and 'value'")
## error on misspelled components
if(!any(as.character(components$component) %in% c("route","locality","administrative_area","postal_code","country")))
stop("valid components are 'route', 'locality', 'postal_code', 'administrative_area' and 'country'")
components = paste0(apply(components, 1, function(x) paste0(x, collapse = ":")), collapse = "|")
components <- tolower(components)
return(components)
}
validateComponentsCountries <- function(components){
if(is.null(components)) return(NULL)
if(length(components) > 5){
stop("components only supports up to 5 countries")
}
if(!all(nchar(components) == 2)){
stop("components must be two characters and represent an ISO 3166-1 Alpha-2 country code")
}
if(!is.character(components)){
stop("components must be two characters and represent an ISO 3166-1 Alpha-2 country code")
}
components <- paste0("country:", components, collapse = "|")
return(components)
}
validateDepartureTime <- function(departure_time, mode){
if(is.null(departure_time)) return(NULL)
if (inherits(departure_time, "POSIXct")) {
checkPosix( departure_time )
if(mode == "driving" && ( departure_time + 60) < Sys.time() ){ ## allowing a buffer
stop("departure_time for driving mode must not be in the past")
}
return( as.integer(departure_time ) )
} else if ( inherits(departure_time, "character") ) {
if( departure_time != "now") {
stop("when using a string for departure_time you may only use 'now'")
}
}
return( departure_time )
}
validateFindInput <- function( input, inputtype ) {
## if phonenumber , "+" needs to be encoded to %2B
if( inputtype == "phonenumber") {
input <- gsub("\\+","%2B",input)
}
return( input )
}
validateLocationPoint <- function( point ) {
if(is.null(point)) return(NULL)
point <- validateGeocodeLocation( point )
return(
paste0("point:",point)
)
}
validateLocationCircle <- function( circle ) {
if(is.null(circle)) return(NULL)
if( is.null( circle[['radius']] ) || is.null( circle[['point']] ) ) {
stop("circle list must include radius and point elements")
}
return(
paste0("circle:", circle[['radius']],"@", paste0(circle[['point']], collapse = ",") )
)
}
validateLocationRectangle <- function( rectangle ) {
if(is.null(rectangle)) return(NULL)
if( is.null( rectangle[['sw']] ) || is.null( rectangle[['ne']] ) ) {
stop("rectangle list must include sw and ne elements")
}
sw <- paste0(rectangle[['sw']], collapse = ",")
ne <- paste0(rectangle[['ne']], collapse = ",")
return(
paste0("rectangle:",sw, "|", ne)
)
}
validateLocationBias <- function( point, circle, rectangle ) {
if( !is.null(point) ) {
return( point )
}
if (!is.null( circle )) {
return( circle )
}
if (!is.null( rectangle )) {
return ( rectangle )
}
return( NULL )
}
validateLanguage <- function(language){
if(is.null(language)) return(NULL)
if(!inherits(language, "character") | length(language) > 1){
stop("language must be a single string")
}
return(tolower(language))
}
validateFov <- function(fov){
if(length(fov) != 1)
stop("fov must be a single value")
if(!is.numeric(fov) | fov < 0 | fov > 120)
stop("fov must be a numeric value between 0 and 120 (inclusive)")
return(fov)
}
validateGeocodeLocation <- function(location){
if(is.null(location)) return(NULL)
## checks the location is a numeric vector (of lat/lon coordinates)
if(!is.numeric(location))
stop("location must be a vector of a pair of latitude and longitude coordinates")
if(!length(location) == 2)
stop("location must be a vector of a pair of latitude and longitude coordinates")
location <- paste0(location, collapse = ",")
return(location)
}
validateHeading <- function(heading){
if(is.null(heading)) return(NULL)
if(length(heading) != 1)
stop("heading must be a single value")
if(!is.numeric(heading) | heading < 0 | heading > 360){
stop("heading must be a numeric value between 0 and 360 (inclusive)")
}
return(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(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)){
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'")
}
return(location)
}
validateLocationType <- function(location_type){
if(is.null(location_type)) return(NULL)
if(length(setdiff(location_type, c("rooftop","range_interpolated", "geometric_center", "approximate"))) > 0)
stop("invlalid values for location_type")
if(length(location_type) > 1){
location_type <- paste0(toupper(location_type), collapse = "|")
}else{
location_type <- toupper(location_type)
}
return(location_type)
}
validateName <- function(name, search_string){
if(is.null(name)) return(NULL)
## warning if name used with search_string
if(!is.null(search_string) & !is.null(name))
warning("The 'name' argument is ignored when using a 'search_string'")
if(length(name) > 1)
name <- paste0(name, collapse = "|")
return(name)
}
validatePageToken <- function(page_token){
if(is.null(page_token)) return(NULL)
## page token is single string
if(!is.character(page_token) | length(page_token) != 1)
stop("page_token must be a string of length 1")
return(page_token)
}
validatePitch <- function(pitch){
if(length(pitch) != 1)
stop("pitch must be a single value")
if(!is.numeric(pitch) | pitch < -90 | pitch > 90){
stop("pitch must be between -90 and 90 (inclusive)")
}
return(pitch)
}
# validatePlaceInput <- function( inputtype ) {
# if ( !( intputtype == "textxquery" || inputtype == "phonenumber") ) {
# stop("inputtype must be one of textquery or phonenumber")
# }
# return( inputtype )
# }
validatePlaceType <- function(place_type){
if(is.null(place_type)) return(NULL)
if(length(place_type) > 1 | !is.character(place_type))
stop("place_type must be a string vector of length 1")
return(place_type)
}
checkPosix <- function(time){
if(!inherits(time, "POSIXct"))
stop("times must be a POSIXct object")
}
validatePriceRange <- function(price_range){
if(is.null(price_range)) return(NULL)
## price range is between 0 and 4
if(!is.numeric(price_range) | (is.numeric(price_range) & length(price_range) != 2))
stop("price_range must be a numeric vector of length 2")
if(!price_range[1] %in% 0:4 | !price_range[2] %in% 0:4)
stop("price_range must be between 0 and 4 inclusive")
return(price_range)
}
validateRadar <- function(radar){
## if radar search, must provide location, key, radius
## if radar search, one of keyword, name or type
# if(isTRUE(radar)){
# if(!is.null(search_string))
# warning("the search_string in a radar search will be ignored")
#
# if(is.null(keyword) & is.null(name) & is.null(place_type))
# stop("when using a radar search, one of keyword, name or place_type must be provided")
#
# if(is.null(location))
# stop("when using a radar search, location must be provided")
#
# if(is.null(radius))
# stop("when using a radar search, radius must be provided")
# }
#
# return(radar)
if( is.null( radar) ) {
return()
}
message("The radar argument is now deprecated")
}
validateRadius <- function(radius){
if(is.null(radius)) return(NULL)
if(length(radius) != 1 | is.list(radius))
stop("radius must be numeric vector of length 1")
if(!is.numeric(radius))
stop("radius must be numeric between 0 and 50000")
if(radius > 50000 | radius < 0)
stop("radius must be numeric between 0 and 50000")
return(radius)
}
validateRadiusRankBy <- function(rankby, radius, location){
## radius must not be included if rankby=distance
if(!is.null(rankby) & !is.null(location)){
if(!is.null(radius) & rankby == "distance"){
warning("radius is ignored when rankby == 'distance'")
radius <- NULL
}
}
return(radius)
}
validateRankBy <- function(rankby, location, search_string){
if(is.null(rankby)) return(NULL)
## rankby has correct arguments
if(!is.null(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))
warning("The 'rankby' argument is ignored when using a 'search_string'")
return(rankby)
}
validateRegion <- function(region){
if(is.null(region)) return(NULL)
if(!inherits(region, "character") | length(region) > 1)
stop("region must be a two-character string")
return(tolower(region))
}
validateResultType <- function(result_type){
if(is.null(result_type)) return(NULL)
if(!inherits(result_type, "character") )
stop("result_type must be a vector of strings")
if(length(result_type) > 1){
result_type <- paste0(tolower(result_type), collapse = "|")
}else{
result_type <- tolower(result_type)
}
return(result_type)
}
validateSize <- function(size){
if(!is.numeric(size) | length(size) != 2)
stop("size must be a numeric vector of length 2, giving the width and height (in pixles) of the image")
size <- paste0(size, collapse = "x")
return(size)
}
validateTrafficModel <- function(traffic_model){
if(is.null(traffic_model)) return(NULL)
## allow an underscore to pass
traffic_model <- match.arg(gsub("_", " ", traffic_model), choices = c("best guess", "pessimistic","optimistic"))
traffic_model <- gsub(" ", "_", traffic_model)
}
## transit_mode is only valid where mode = transit
validateTransitMode <- function(transit_mode, mode) {
if(!is.null(transit_mode) & mode != "transit") {
warning("You have specified a transit_mode, but are not using mode = 'transit'. Therefore this argument will be ignored")
return(NULL)
}else if(!is.null(transit_mode) & mode == "transit"){
transit_mode <- match.arg(transit_mode, choices = c("bus","subway","train","tram","rail"))
}
return(transit_mode)
}
## transit_routing_preference only valid where mode == transit
validateTransitRoutingPreference <- function(transit_routing_preference, mode) {
if(!is.null(transit_routing_preference) & mode != "transit"){
warning("You have specified a transit_routing_preference, but are not using mode = 'transit'. Therefore this argument will be ignored")
return(NULL)
}else if(!is.null(transit_routing_preference) & mode == "transit"){
transit_routing_preference <- match.arg(transit_routing_preference, choices = c("less_walking","fewer_transfers"))
transit_routing_preference <- paste0(transit_routing_preference, collapse = "|")
}
return(transit_routing_preference)
}
validateWaypoints <- function(waypoints, optimise_waypoints, mode){
if(is.null(waypoints)) return(NULL)
if(!mode %in% c("driving", "walking","bicycling"))
stop("waypoints are only valid for driving, walking or bicycling modes")
if(!inherits(waypoints, "list"))
stop("waypoints must be a list")
if(!all(names(waypoints) %in% c("stop", "via")))
stop("waypoint list elements must be named either 'via' or 'stop'")
## check if waypoints should be optimised, and thefore only use 'stop' as a valid waypoint
if(optimise_waypoints == TRUE){
if(any(names(waypoints) %in% c("via")))
stop("waypoints can only be optimised for stopovers. Each waypoint in the list must be named as stop")
}
return(constructWaypoints(waypoints, optimise_waypoints))
}