USEPA/RAQSAPI

"Error in httr::user_agent(.) : length(agent) == 1 is not TRUE" on ags_dailysummary_by_state() call

Closed this issue · 4 comments

Hi, below is the example I use and the error message I get from aqs_dailysummary_by_state(). Do you know why? (This is under R-4.0.4 with the latest CRAN version of RAQSAPI)

library(RAQSAPI)

## Variables
param <- as.character(c(
    44201, # Ozone (O3; parts per million)
    81104, # PM2.5 STP (PM2.5; micrograms/m3)
    81102, # PM10 Total 0-10um STP (PM10, micrograms/m3)
    42602, # Nitrogen dioxide (NO2) (NO2; parts per billion)
    42603, # Oxides of nitrogen (NOx) (NOX; parts per billion)
    42401, # Sulfur dioxide (SO2; parts per billion)
    42102, # Carbon dioxide (CO2; parts per million)
    42101, # Carbon monoxide (CO; parts per million)
    62101, # Outdoor Temperature (TEMPO; Degrees Fahrenheit)
    64101, # Barometric pressure (BPRES; millibars)
    62201, # Relative Humidity (RHUM; percent relative humidity)
    61101, # Wind Speed - Scalar (WS-S; Knots)
    65101 # Rain 24hr total (RAIN, inches)
))

## State
state <- 36 # New York

## Time period (only one year at a time)
start <- as.Date("2000-01-01")
end <- as.Date("2000-12-31")

## Request data
dat <- aqs_dailysummary_by_state(param, bdate = start, edate = end, stateFIPS = state)
## => Error in httr::user_agent(.) : length(agent) == 1 is not TRUE

## Traceback
## > traceback()
## 8: stop(simpleError(msg, call = if (p <- sys.parent(1L)) sys.call(p)))
## 7: stopifnot(is.character(agent), length(agent) == 1)
## 6: httr::user_agent(.)
## 5: glue("User:{user} via RAQSAPI library for R") %>% httr::user_agent()
## 4: aqs(service = service, filter = "byState", user = getOption("aqs_username"),
##        user_key = getOption("aqs_key"), variables = list(param = format.multiple.params.for.api(parameter),
##            bdate = format(bdate, format = "%Y%m%d"), edate = format(edate,
##                format = "%Y%m%d"), state = stateFIPS, cbdate = cbdate,
##            cedate = cedate))
## 3: .f(parameter = .l[[1L]][[1L]], bdate = .l[[2L]][[1L]], edate = .l[[3L]][[1L]],
##        stateFIPS = .l[[4L]][[1L]], service = .l[[5L]][[1L]], ...)
## 2: purrr::pmap(.l = params, .f = aqs_services_by_state)
## 1: aqs_dailysummary_by_state(param, bdate = start, edate = end,
##        stateFIPS = state)

I am sorry, I am unable to recreate the issue that you are having. I have tried to recreate this on both Windows and Linux using the latest version of R. Can you please give me your sessionInfo()? Are you running the latest stable version from CRAN?

Also is it possible for you to see if you can recreate this issue from another computer.

Hi, thanks for helping. That is indeed strange. Here is the sessionInfo output:

> sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-apple-darwin20.3.0 (64-bit)
Running under: macOS Big Sur 11.4

Matrix products: default
BLAS:   /usr/local/R/R-4.0.4_build/lib/libRblas.dylib
LAPACK: /usr/local/R/R-4.0.4_build/lib/libRlapack.dylib

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] RAQSAPI_2.0.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6       lubridate_1.7.8  crayon_1.3.4     dplyr_0.8.5
 [5] assertthat_0.2.1 R6_2.5.0         lifecycle_0.2.0  magrittr_2.0.1
 [9] httr_1.4.2       pillar_1.4.4     stringi_1.4.6    rlang_0.4.10
[13] generics_0.1.0   vctrs_0.3.6      ellipsis_0.3.1   tools_4.0.4
[17] stringr_1.4.0    glue_1.4.2       purrr_0.3.4      compiler_4.0.4
[21] pkgconfig_2.0.3  tidyselect_1.1.0 tibble_3.0.1

I then also updated all 'Imports' packages that RAQSAPI relies on, same behavior.
I also have an HPC Linux Server available, same behavior there, too. The sessionInfo for the Linux Server is:

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS:   /home/mhofert/soft/R/R-3.6.3_build/lib/libRblas.so
LAPACK: /home/mhofert/soft/R/R-3.6.3_build/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RAQSAPI_2.0.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6       lubridate_1.7.10 crayon_1.3.4     dplyr_1.0.6     
 [5] assertthat_0.2.0 R6_2.5.0         lifecycle_1.0.0  magrittr_2.0.1  
 [9] pillar_1.6.1     httr_1.3.1       stringi_1.2.2    rlang_0.4.10    
[13] bindrcpp_0.2.2   generics_0.1.0   tools_3.6.3      stringr_1.3.1   
[17] glue_1.4.2       purrr_0.3.4      compiler_3.6.3   pkgconfig_2.0.1 
[21] bindr_0.1.1      tidyselect_1.1.0 tibble_3.1.2   

I now did some debugging (on my local macOS; see first sessionInfo()). aqs_dailysummary_by_state() calls a non-exported aqs() function which immediately sets the user_agent variable. To this end, glue() is called and this call returns nothing, which is why the subsequent httr::user_agent() fails. I can reproduce this like this:

library(RAQSAPI)
debugonce(RAQSAPI:::aqs)
aqs_dailysummary_by_state("44201", bdate = as.Date("2000-01-01"), edate = as.Date("2000-12-31"), stateFIPS = "36")
glue("User:{user} via RAQSAPI library for R") # => empty

I don't use glue() and these type of things, but it seems like code given in curly braces is evaluated as R code. The user should be, by default, getOption("aqs_username"). But if I execute that separately, I get NULL:

Browse[2]> getOption("aqs_username")
NULL

I see... I just checked the manual and it seems that I forgot to call aqs_credentials(). It now works. It would probably be good to give a better error message if the username is NULL. Thanks & cheers, M

re-examining this post I have realized that this is a valid issue that needs to be addressed. Whenever the user calls one of the RAQSAPI functions without proper credentials the aqs function should run this code

stop("please use the aqs_credentials() function before continuing \n")

For some reason this is not being run when the user does not provide credential information. Instead the error message that you are receiving is being returned. I will look into fixing this issue with the next release.

Hopefully this issue is addressed with commit d6fea3c