Skip to content

Commit 514d4bb

Browse files
authored
Merge pull request #395 from ncss-tech/lint1
Linting 1
2 parents b406f96 + 8b0cffd commit 514d4bb

File tree

110 files changed

+1090
-1225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+1090
-1225
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: soilDB
22
Type: Package
33
Title: Soil Database Interface
4-
Version: 2.8.10
4+
Version: 2.8.11
55
Authors@R: c(person(given="Dylan", family="Beaudette", role = c("aut"), email = "dylan.beaudette@usda.gov"),
66
person(given="Jay", family="Skovlin", role = c("aut")),
77
person(given="Stephen", family="Roecker", role = c("aut")),

R/ISSR800.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,9 @@ ISSR800.wcs <- function(aoi, var, res = 800, quiet = FALSE) {
228228
input_class <- attr(wcs.geom, '.input_class')
229229

230230
if ((!is.null(input_class) && input_class == "raster") ||
231-
getOption('soilDB.return_Spatial', default = FALSE)) {
232-
if (requireNamespace("raster")) {
233-
r <- raster::raster(r)
234-
}
231+
getOption('soilDB.return_Spatial', default = FALSE) &&
232+
requireNamespace("raster")) {
233+
r <- raster::raster(r)
235234
}
236235

237236
# set metadata

R/KSSL_VG_model.R

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
21
# define van Genuchten model as a function
32
# this is tailored to the parameters stored in our KSSL data
43
# https://en.wikipedia.org/wiki/Water_retention_curve
54
.vg <- function(phi, theta_r, theta_s, alpha, n) {
65
theta_r + ((theta_s - theta_r) / ((1 + (alpha * phi) ^ n) ^ (1 - 1 / n)))
76
}
87

9-
10-
11-
128
## notes: Rosetta units for alpha and npar are log10(1/cm) and log10([-])
139
# VG_params: table of VG parameters from KSSL / Rosetta: alpha and npar are in log10 form
1410
# phi_min: lower limit for water potential in kPa
1511
# phi_max: upper limit for water potential in kPa
1612
# pts: number of points to include in curve
1713

18-
1914
#' @title Develop a Water Retention Curve from KSSL Data
2015
#'
2116
#' @description Water retention curve modeling via van Genuchten model and KSSL data.
@@ -56,8 +51,6 @@
5651
#'
5752
# 'van Genuchten, M.Th. (1980). "A closed-form equation for predicting the hydraulic conductivity of unsaturated soils". Soil Science Society of America Journal. 44 (5): 892-898.
5853
#'
59-
#'
60-
#'
6154
#' @export
6255
#'
6356
#' @examples
@@ -77,8 +70,8 @@
7770
KSSL_VG_model <- function(VG_params, phi_min = 10^-6, phi_max = 10^8, pts = 100) {
7871

7972
# sanity check, expected columns
80-
if( any(! c('theta_r', 'theta_s', 'alpha', 'npar') %in% names(VG_params)) ) {
81-
message('one or more required column is missing')
73+
if (!all(c('theta_r', 'theta_s', 'alpha', 'npar') %in% names(VG_params))) {
74+
message('one or more required columns is missing')
8275
return(list(VG_curve = NULL, VG_inverse_function = NULL))
8376
}
8477

@@ -87,15 +80,14 @@ KSSL_VG_model <- function(VG_params, phi_min = 10^-6, phi_max = 10^8, pts = 100)
8780

8881
# sanity check: no NA allowed
8982
# return NULL if present
90-
if(any(is.na(VG_params))) {
83+
if (anyNA(VG_params)) {
9184
message('one or more required value is NA')
9285
return(list(VG_curve = NULL, VG_inverse_function = NULL))
9386
}
9487

95-
9688
# useful range in kPa suctions
97-
phi <- 10^seq(log(phi_min, base=10), log(phi_max, base=10), length.out = pts)
98-
m <- data.frame(phi=phi)
89+
phi <- 10^seq(log(phi_min, base = 10), log(phi_max, base = 10), length.out = pts)
90+
m <- data.frame(phi = phi)
9991

10092
# Rosetta units for alpha and npar are 1/cm and [-]
10193
# convert kPa to cm of H20
@@ -121,8 +113,9 @@ KSSL_VG_model <- function(VG_params, phi_min = 10^-6, phi_max = 10^8, pts = 100)
121113
vg.inv <- splinefun(m$theta, m$phi)
122114

123115
# return curve and spline function
124-
return(list(VG_curve=m, VG_function=vg.fwd, VG_inverse_function=vg.inv))
116+
return(list(
117+
VG_curve = m,
118+
VG_function = vg.fwd,
119+
VG_inverse_function = vg.inv
120+
))
125121
}
126-
127-
128-

R/OSDquery.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ OSDquery <- function(everything = NULL, mlra='', taxonomic_class='', typical_ped
8383
## searching by sections
8484

8585
# build parameters list
86-
parameters=list(
86+
parameters <- list(
8787
json = 1,
8888
mlra = mlra,
8989
taxonomic_class = taxonomic_class,
@@ -105,7 +105,7 @@ OSDquery <- function(everything = NULL, mlra='', taxonomic_class='', typical_ped
105105
## searching entire OSD text
106106

107107
# build parameters list
108-
parameters=list(
108+
parameters <- list(
109109
json = 1,
110110
query = everything,
111111
mlra = mlra

R/ROSETTA.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ ROSETTA <- function(x, vars, v = c('1', '2', '3'), include.sd = FALSE, chunkSize
205205
}
206206

207207
# chunk
208-
x[['.chunk']] <- makeChunks(1:nrow(x), size = chunkSize)
208+
x[['.chunk']] <- makeChunks(seq_len(nrow(x)), size = chunkSize)
209209

210210
# split
211211
x <- split(x, x[['.chunk']])
@@ -233,7 +233,7 @@ ROSETTA <- function(x, vars, v = c('1', '2', '3'), include.sd = FALSE, chunkSize
233233

234234
# stack into DF
235235
res <- do.call('rbind', res)
236-
row.names(res) <- as.character(1:nrow(res))
236+
row.names(res) <- as.character(seq_len(nrow(res)))
237237

238238
# remove chunkID
239239
res[['.chunk']] <- NULL

R/SDA-spatial.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ SDA_spatialQuery <- function(geom,
259259
query_string = FALSE,
260260
as_Spatial = getOption('soilDB.return_Spatial', default = FALSE)) {
261261
if (byFeature) {
262-
res <- do.call('rbind', lapply(1:nrow(geom), function(i) {
262+
res <- do.call('rbind', lapply(seq_len(nrow(geom)), function(i) {
263263
res2 <- .SDA_spatialQuery(
264264
geom = geom[i, ],
265265
what = what,

R/SDA_query.R

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ format_SQL_in_statement <- function(x) {
3535
# and, plenty safe to perform a second time, in case this was done outside of the function call
3636
x <- unique(x)
3737
i <- paste(x, collapse = "','")
38-
i <- paste("('", i, "')", sep = '')
38+
i <- paste0("('", i, "')")
3939
return(i)
4040
}
4141

@@ -167,22 +167,20 @@ SDA_query <- function(q, dsn = NULL) {
167167
# check response content type
168168
h <- r$all_headers
169169

170-
if (!is.null(h)) {
171-
if (length(h) == 0
170+
if (!is.null(h) && (length(h) == 0
172171
|| is.null(h[[1]]$headers$`content-type`)
173172
|| !h[[1]]$headers$`content-type` %in%
174173
c("application/json; charset=utf-8", # data response
175174
"text/xml; charset=utf-8") # error response (maybe not anymore?)
176-
) {
177-
msg <- "Soil Data Access REST API is not currently available, please try again later."
178-
if (is.null(h[[1]]$headers$`content-type`)) {
179-
txt <- try(httr::content(r, as = "text", encoding = "UTF-8"), silent = TRUE)
180-
if (!inherits(txt, 'try-error')) {
181-
msg <- gsub("<[^>]*>", "", txt)
182-
}
175+
)) {
176+
msg <- "Soil Data Access REST API is not currently available, please try again later."
177+
if (is.null(h[[1]]$headers$`content-type`)) {
178+
txt <- try(httr::content(r, as = "text", encoding = "UTF-8"), silent = TRUE)
179+
if (!inherits(txt, 'try-error')) {
180+
msg <- gsub("<[^>]*>", "", txt)
183181
}
184-
r <- try(stop(msg, call. = FALSE), silent = TRUE)
185182
}
183+
r <- try(stop(msg, call. = FALSE), silent = TRUE)
186184
}
187185

188186
if (inherits(r, 'try-error')) {
@@ -209,8 +207,7 @@ SDA_query <- function(q, dsn = NULL) {
209207
}
210208

211209
## inject specific message into a try-error result
212-
request.status <- try(stop(paste0(attr(request.status, 'condition')$message, "\n",
213-
error.msg), call. = FALSE), silent = TRUE)
210+
request.status <- try(stop(attr(request.status, 'condition')$message, "\n", error.msg), silent = TRUE)
214211
}
215212

216213
# return the error object so calling function/user can handle it
@@ -223,9 +220,9 @@ SDA_query <- function(q, dsn = NULL) {
223220
# note: the data returned by SDA/JSON are all character class
224221
# we "fix" this later on
225222
r.content <- try(httr::content(r, as = 'text', encoding = 'UTF-8'), silent = TRUE)
226-
227-
if (inherits(r.content,'try-error'))
228-
return(invisible(r.content))
223+
224+
if (inherits(r.content, 'try-error'))
225+
return(invisible(r.content))
229226

230227
d <- try(jsonlite::fromJSON(r.content), silent = TRUE)
231228

@@ -291,50 +288,36 @@ SDA_query <- function(q, dsn = NULL) {
291288
dt <- strsplit(j, split = ',', fixed = TRUE)[[1]][8]
292289
# known data types in SDA and appropriate classes in R
293290
# fall-back to "character" for unknown data types
294-
switch(dt,
295-
'DataTypeName=char' = 'character',
296-
'DataTypeName=nchar' = 'character',
297-
'DataTypeName=varchar' = 'character',
298-
'DataTypeName=nvarchar' = 'character',
299-
'DataTypeName=text' = 'character',
300-
'DataTypeName=ntext' = 'character',
301-
'DataTypeName=datetime' = 'character',
302-
'DataTypeName=datetime2' = 'character',
303-
'DataTypeName=timestamp' = 'character',
304-
'DataTypeName=bit' = 'integer',
305-
'DataTypeName=int' = 'integer',
306-
'DataTypeName=bigint' = 'integer',
307-
'DataTypeName=smallint' = 'integer',
308-
'DataTypeName=tinyint' = 'integer',
309-
'DataTypeName=numeric' = 'numeric',
310-
'DataTypeName=real' = 'numeric',
311-
'DataTypeName=float' = 'numeric',
312-
'DataTypeName=decimal' = 'numeric',
313-
'character'
314-
)
291+
switch(
292+
dt,
293+
'DataTypeName=char' = 'character',
294+
'DataTypeName=nchar' = 'character',
295+
'DataTypeName=varchar' = 'character',
296+
'DataTypeName=nvarchar' = 'character',
297+
'DataTypeName=text' = 'character',
298+
'DataTypeName=ntext' = 'character',
299+
'DataTypeName=datetime' = 'character',
300+
'DataTypeName=datetime2' = 'character',
301+
'DataTypeName=timestamp' = 'character',
302+
'DataTypeName=bit' = 'integer',
303+
'DataTypeName=int' = 'integer',
304+
'DataTypeName=bigint' = 'integer',
305+
'DataTypeName=smallint' = 'integer',
306+
'DataTypeName=tinyint' = 'integer',
307+
'DataTypeName=numeric' = 'numeric',
308+
'DataTypeName=real' = 'numeric',
309+
'DataTypeName=float' = 'numeric',
310+
'DataTypeName=decimal' = 'numeric',
311+
'character'
312+
)
315313
})
316314

317315
# convert each column that isn't character
318316
idx <- which(cc != 'character')
319-
for(f in idx) {
317+
for (f in idx) {
320318
df[, f] <- as(df[, f], cc[f])
321319
}
322-
323-
324-
## strings resembling scientific notation are converted into numeric
325-
## ex: type.convert("8E2") -> 800
326-
# https://github.com/ncss-tech/soilDB/issues/190
327-
328-
# # attempt type conversion
329-
# # same result as writing to file and reading-in via read.table()
330-
# df <- type.convert(df,
331-
# na.strings = c('', 'NA'),
332-
# as.is = TRUE,
333-
# colClasses = cc
334-
# )
335-
336-
## TODO further error checking?
337-
320+
338321
return(df)
339322
}
340323

R/SSURGO_spatial_query.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ SoilWeb_spatial_query <- function(bbox=NULL, coords=NULL, what='mapunit', source
5353

5454
# process filter components
5555
if(!missing(coords)) {
56-
f <- paste('&lon=', coords[1], '&lat=', coords[2], sep='')
56+
f <- paste0('&lon=', coords[1], '&lat=', coords[2])
5757
}
5858

5959
if(!missing(bbox)) {
6060
bbox <- paste(bbox, collapse=',')
61-
f <- paste('&bbox=', bbox, sep='')
61+
f <- paste0('&bbox=', bbox)
6262
}
6363

6464
# build URL
65-
the.url <- paste('https://casoilresource.lawr.ucdavis.edu/soil_web/api/ssurgo.php?what=mapunit', f, sep='')
65+
the.url <- paste0('https://casoilresource.lawr.ucdavis.edu/soil_web/api/ssurgo.php?what=mapunit', f)
6666

6767
# attempt to load data from URL/JSON
6868
# note: this may fail when done over gov VPN

0 commit comments

Comments
 (0)