Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in sfn_metrics: pattern must be a string, not NULL #23

Open
arthur-e opened this issue May 20, 2024 · 2 comments
Open

Error in sfn_metrics: pattern must be a string, not NULL #23

arthur-e opened this issue May 20, 2024 · 2 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request
Milestone

Comments

@arthur-e
Copy link

Hello,

I'm getting an unexpected error, originating in stringr, associated with use of sfn_metrics() or daylight_metrics() when the tidy = TRUE argument is used.

library(dplyr, warn.conflicts = F)
library(sapfluxnetr)

SAPFLUXNET.ROOT <- '...'
SAPFLUXNET.DIR <- paste0(SAPFLUXNET.ROOT, '/Rdata/plant')

# Build the metadata tables
sfn.metadata <- read_sfn_metadata(
  folder = SAPFLUXNET.DIR, .write_cache = file.exists(paste0(SAPFLUXNET.DIR, '/.metadata_cache.RData')))

# Filter out sites that have been managed or treated with drought/irrigation
#   treatments or CO2 enrichment
sites.of.interest <- sfn_sites_in_folder(SAPFLUXNET.DIR) %>%
  filter_sites_by_md(
    si_igbp != 'WET', # Can't use these sites
    !(st_treatment %in% c('Irrigated', 'Post Irrigation', 'drought', 'Girdling early successional',
        'ELEVATED CO2 treatments maintained at 550 ppm', 'Elevated atmospheric CO2')),
    !grepl('a very high degree of heterogeneity', st_remarks),
    !grepl('Irrigation of the experimental grove', st_remarks),
    metadata = sfn.metadata)

This works just fine:

sites.data <- read_sfn_data(sites.of.interest[1], folder = SAPFLUXNET.DIR) %>%
  daylight_metrics(period = '1 day', probs = c(0.02, 0.5, 0.98))
[1] "Crunching data for ARG_MAZ. In large datasets this could take a while"
[1] "daylight data for ARG_MAZ"

But when I add tidy = TRUE, I get an error:

# ERROR
sites.data <- read_sfn_data(sites.of.interest[1], folder = SAPFLUXNET.DIR) %>%
  daylight_metrics(period = '1 day', probs = c(0.02, 0.5, 0.98), tidy = TRUE)
[1] "Crunching data for ARG_MAZ. In large datasets this could take a while"
[1] "daylight data for ARG_MAZ"
Error in `stringr::str_detect()`:
! `pattern` must be a string, not NULL.
Run `rlang::last_trace()` to see where the error occurred.
> traceback()
11: stop(fallback)
10: signal_abort(cnd, .file)
9: rlang::abort(message, ..., call = call, use_cli_format = TRUE, 
       .frame = .frame)
8: cli::cli_abort("`pattern` must be a string, not {.obj_type_friendly {x}}.", 
       call = error_call)
7: type.default(pattern)
6: type(pattern)
5: stringr::str_detect(names(sapf_data[["sapf"]])[2], raw_codes)
4: metrics_tidyfier(., metadata, interval = "daylight")
3: sfn_metrics(sfn_data, period = period, .funs = .funs, solar = solar, 
       interval = "daylight", int_start = int_start, int_end = int_end, 
       ...) %>% {
       if (tidy) {
           metrics_tidyfier(., metadata, interval = "daylight")
       }
       else {
           .
       }
   }
2: daylight_metrics(., period = "1 day", probs = c(0.02, 0.5, 0.98), 
       tidy = TRUE)
1: read_sfn_data(sites.of.interest[1], folder = SAPFLUXNET.DIR) %>% 
       daylight_metrics(period = "1 day", probs = c(0.02, 0.5, 0.98), 
           tidy = TRUE)

My sessionInfo():

> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

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

time zone: America/Denver
tzcode source: system (glibc)

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

other attached packages:
[1] sapfluxnetr_0.1.4 dplyr_1.1.4      

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       knitr_1.46        cli_3.6.2         xfun_0.43        
 [5] rlang_1.1.3       stringi_1.8.3     pkgload_1.3.4     purrr_1.0.2      
 [9] generics_0.1.3    assertthat_0.2.1  glue_1.7.0        colorspace_2.1-0 
[13] htmltools_0.5.8.1 scales_1.3.0      fansi_1.0.6       rmarkdown_2.26   
[17] grid_4.4.0        munsell_0.5.1     evaluate_0.23     tibble_3.2.1     
[21] fastmap_1.1.1     yaml_2.3.8        lifecycle_1.0.4   stringr_1.5.1    
[25] compiler_4.4.0    timechange_0.3.0  pkgconfig_2.0.3   rstudioapi_0.16.0
[29] digest_0.6.35     R6_2.5.1          tidyselect_1.2.1  utf8_1.2.4       
[33] pillar_1.9.0      magrittr_2.0.3    withr_3.0.0       tools_4.4.0      
[37] gtable_0.3.5      lubridate_1.9.3   ggplot2_3.5.1    
@arthur-e
Copy link
Author

I see that this arises when tidy = TRUE but the metadata argument is not provided. I think that a more helpful error message could be raised, instead of letting stringr catch the fault.

@MalditoBarbudo MalditoBarbudo self-assigned this May 22, 2024
@MalditoBarbudo MalditoBarbudo added bug Something isn't working enhancement New feature or request labels May 22, 2024
@MalditoBarbudo MalditoBarbudo added this to the 0.1.5 milestone May 22, 2024
@MalditoBarbudo
Copy link
Member

@arthur-e Thanks for the report.
You are right, this error should be catched with a more informative message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants