File: ar_validate.R

package info (click to toggle)
r-cran-areal 0.1.8%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,624 kB
  • sloc: sh: 13; xml: 2; makefile: 2
file content (346 lines) | stat: -rw-r--r-- 9,534 bytes parent folder | download
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
#' Validating Data for Interpolation
#'
#' @description \code{ar_validate} executes a series of logic tests for \code{sf} object status,
#'     shared coordinates between source and target data, appropriate project, and absence of
#'     variable name conflicts.
#'
#' @usage ar_validate(source, target, varList, method = "aw", verbose = FALSE)
#'
#' @param source A \code{sf} object with data to be interpolated
#' @param target A \code{sf} object that data should be interpolated to
#' @param varList A vector of variable names to be added to the \code{target} object
#' @param method The areal interpolation method validation is being performed for. This
#'     should be set to \code{"aw"}. Additional functionality will be added as the package
#'     adds new interpolation techniques.
#' @param verbose A logical scalar; if \code{TRUE}, a tibble with test results is returned
#'
#' @return If \code{verbose} is \code{FALSE}, a logical scalar is returned that is \code{TRUE}
#'     is all tests are passed and \code{FALSE} if one or more tests is failed. If \code{verbose}
#'     is \code{TRUE}, a tibble with detailed test results is returned.
#'
#' @seealso \link{c}
#'
#' @examples
#' ar_validate(source = ar_stl_asthma, target = ar_stl_wards, varList = "ASTHMA")
#'
#' ar_validate(source = ar_stl_asthma, target = ar_stl_wards, varList = "ASTHMA", verbose = TRUE)
#'
#' @importFrom glue glue
#' @importFrom sf st_crs st_is_longlat
#'
#' @export
ar_validate <- function(source, target, varList, method = "aw", verbose = FALSE){

  # check for missing parameters
  if (missing(source)) {
    stop("A sf object containing source data must be specified for the 'source' argument.")
  }

  if (missing(target)) {
    stop("A sf object containing target data must be specified for the 'target' argument.")
  }

  if (missing(varList)) {
    stop("A variable name or vector of variable names must be specified for the 'varList' argument.")
  }

  if (verbose != TRUE & verbose != FALSE){
    stop("The 'verbose' argument must be either 'TRUE' or 'FALSE'.")
  }

  if (method != "aw"){
    stop("The 'method' argument must be 'aw'.")
  }

  # store results from primary validate subfunctions
  sf_result <- ar_validate_sf(source, target)

  # execute additional tests if both are sf, otherwise set results to NA
  if (sf_result == FALSE){

    crs_result <- NA
    longlat_result <- NA
    polygon_result <- NA
    vars_exist_result <- NA
    vars_conflict_result <- NA


  } else if (sf_result == TRUE){

    # do both source and target have same CRS?
    crs_result <- ar_validate_crs(source, target)

    # are both source and target CRS values in planar?
    longlat_result1 <- ar_validate_longlat(source)
    longlat_result2 <- ar_validate_longlat(target)

    longlat_result <- all(longlat_result1, longlat_result2)

    # are both data sets polygon data?
    polygon_result1 <- ar_validate_polygon(source)
    polygon_result2 <- ar_validate_polygon(target)

    polygon_result <- all(polygon_result1, polygon_result2)

    # are there no conflicts with target variable names?
    vars_conflict_result <- ar_validate_vars_conflict(target, varList = varList)
    vars_exist_result <- ar_validate_vars_exist(source, varList = varList)

  }

  # determine if overall test is passed
  if(sf_result == "TRUE" & crs_result == "TRUE" & longlat_result == "TRUE" &
     polygon_result == "TRUE" &
     vars_exist_result == "TRUE" & vars_conflict_result == "TRUE") {

    result <- TRUE

  } else {

    result <- FALSE

  }

  # conditional code if verbose is assigned FALSE
  if(verbose == FALSE){

    out <- result

  }

  # conditional code if verbose is assigned TRUE
  else if (verbose == TRUE){

    table <- data.frame(
      test = c("sf Objects", "CRS Match", "CRS is Planar", "Polygon Geometries",
               "Variables Exist in Source", "No Variable Conflicts in Target",
               "Overall Evaluation"),
      result = c(sf_result, crs_result, longlat_result, polygon_result,
                 vars_exist_result, vars_conflict_result, result),
      stringsAsFactors = FALSE)

    out <- as_tibble(table)
  }

  # return output
  return(out)

}

# Lite Version of Validation for aw_preview_weights
#
# @description \code{aw_validate_preview} is designed to be called by
#     \code{aw_preview_weights} before the weights are calculated. It
#     lacks the variable validation functionality of \code{ar_validate}.
#
# @param source A \code{sf} object with data to be interpolated
# @param target A \code{sf} object that data should be interpolated to
#
# @return If \code{verbose} is \code{FALSE}, a logical scalar is returned that is \code{TRUE}
#     is all tests are passed and \code{FALSE} if one or more tests is failed. If \code{verbose}
#     is \code{TRUE}, a tibble with detailed test results is returned.
#
aw_validate_preview <- function(source, target){

  # store results from primary validate subfunctions
  sf_result <- ar_validate_sf(source, target)

  # execute additional tests if both are sf, otherwise set results to NA
  if (sf_result == FALSE){

    crs_result <- NA
    longlat_result <- NA
    polygon_result <- NA

  } else if (sf_result == TRUE){

    # do both source and target have same CRS?
    crs_result <- ar_validate_crs(source, target)

    # are both source and target CRS values in planar?
    longlat_result1 <- ar_validate_longlat(source)
    longlat_result2 <- ar_validate_longlat(target)

    longlat_result <- all(longlat_result1, longlat_result2)

    # are both data sets polygon data?
    polygon_result1 <- ar_validate_polygon(source)
    polygon_result2 <- ar_validate_polygon(target)

    polygon_result <- all(polygon_result1, polygon_result2)

  }

  # determine if overall test is passed
  if(sf_result == "TRUE" & crs_result == "TRUE" & longlat_result == "TRUE" &
     polygon_result == "TRUE") {

    out <- TRUE

  } else {

    out <- FALSE

  }

  # return output
  return(out)

}

# Testing for sf object status for source and target data
#
# @description \code{ar_validate_sf} conducts a logic test for shared coordinate
#     coordinate systems, which are a requirement for interpolation.
#
# @param source A \code{sf} object with data to be interpolated
# @param target A \code{sf} object that data should be interpolated to
#
# @return A logical scalar; if \code{TRUE}, the test is passed.
#
ar_validate_sf <- function(source, target){

  # identify sf object in class
  source_sf <- "sf" %in% class(source)
  target_sf <- "sf" %in% class(target)

  if(source_sf == TRUE & target_sf == TRUE){

    # if both objects are sf
    out <- TRUE

  } else if(source_sf == FALSE | target_sf == FALSE){

    # if one or both are not sf
    out <- FALSE

  }

  # return result output
  return(out)

}

# Testing for shared coordinates for source and target data
#
# @description \code{awrvalidate_crs} conducts a logic test for shared coordinate
#     coordinate systems, which are a requirement for interpolation.
#
# @param source A \code{sf} object with data to be interpolated
# @param target A \code{sf} object that data should be interpolated to
#
# @return A logical scalar; if \code{TRUE}, the test is passed.
#
ar_validate_crs <- function(source, target){

  if(sf::st_crs(source) == sf::st_crs(target)) {

    # if both objects share crs
    out <- TRUE

  } else if(sf::st_crs(source) != sf::st_crs(target)) {

    # if objects have different crs
    out <- FALSE
  }

  # return result output
  return(out)

}

# Testing for type of coordinates
#
# @description \code{ar_validate_longlat} conducts a logic test for
#     whether or not the data are in planar format.
#
# @param .data A sf object
#
# @return A logical scalar; if \code{TRUE}, the test is passed
#
ar_validate_longlat <- function(.data){

  result <- sf::st_is_longlat(.data)

  if (result == TRUE){

    # if object is in lat long
    out <- FALSE

  } else if (result == FALSE){

    # if object is in planar
    out <- TRUE

  }

  # return result output
  return(out)

}

# Testing for Variable Conflicts in Target
#
# @description \code{ar_validate_vars_conflict} conducts a logic test for
#     whether or not any of the variables to be created in the target
#     data already exist as named columns.
#
# @param .data A sf object
# @param varList A vector of variables to be created
#
# @return A logical scalar; if \code{TRUE}, the test is passed
#
ar_validate_vars_conflict <- function(.data, varList){

  # create logical vector
  resultVector <- varList %in% colnames(.data)
  result <- any(resultVector)

  if (result == TRUE){

    # if at least one variable name is in target
    out <- FALSE

  } else if (result == FALSE){

    # if no existing variable names are in target
    out <- TRUE

  }

  # return result output
  return(out)

}

# Testing for Variables Existing in Source
#
# @description \code{ar_validate_vars_exist} conducts a logic test for
#     whether or not all variables exist in the source data.
#
# @param .data A sf object
# @param varList A vector of variables assumed to exist.
#
# @return A logical scalar; if \code{TRUE}, the test is passed
#
ar_validate_vars_exist <- function(.data, varList){

  # create logical vector
  resultVector <- varList %in% colnames(.data)
  out <- all(resultVector)

  # return result output
  return(out)

}

# Testing Geometry
ar_validate_polygon <- function(.data){

  # create logical vector
  out <- any(sf::st_geometry_type(.data) %in% c("POLYGON", "MULTIPOLYGON"))

  # return result output
  return(out)

}