@@ -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
0 commit comments