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

Parameterize key query fields in riem_measures() #48

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ee51f9e
Parameterize 'data' field in riem_measures()
JElchison Dec 16, 2024
e95c3ec
Add comment linking field documentation
JElchison Dec 16, 2024
2f6a39c
Parameterize 'elev' field in riem_measures()
JElchison Dec 16, 2024
695875d
Parameterize 'latlon' field in riem_measures()
JElchison Dec 16, 2024
ca212f2
Parameterize 'report_type' field in riem_measures(); Skip HFMETAR by …
JElchison Dec 16, 2024
2a5f27e
Choose a safer default date_start
JElchison Dec 16, 2024
3c358b4
Speficy nometa = no, since parsing counts on column names
JElchison Dec 16, 2024
60d8c28
Update self-documentation for riem_measures()
JElchison Dec 16, 2024
d5a7f4c
Update test fixtures files since default endpoint query changed
JElchison Dec 16, 2024
8737611
Document
JElchison Dec 16, 2024
1c8a136
Preserve order of riem_measures() positional arguments; Remove defaul…
JElchison Dec 17, 2024
a75b5b9
Specify the now-required station srgument in test-measures.R
JElchison Dec 17, 2024
3b082f0
Correct copy/paste error
JElchison Dec 17, 2024
12aeea5
Specify the now-required date_start argument in test-measures.R
JElchison Dec 17, 2024
540fb46
Add call to rlang::check_dots_empty() in riem_measures()
JElchison Dec 17, 2024
fe048f4
Add better validation for new riem_measures() args
JElchison Dec 17, 2024
b14e790
Specify the now-required station srgument in test-measures.R
JElchison Dec 17, 2024
3b8bc96
Correct order of args in documentation
JElchison Dec 17, 2024
ae205a3
refactor: use `rlang::is_character()`
maelle Dec 19, 2024
2f0c7f9
refactor: make report_type's input more readable
maelle Dec 19, 2024
b3c5dd4
test: add test for new parameters
maelle Dec 19, 2024
115a373
Address lintr warnings
JElchison Dec 19, 2024
045b229
Add new tests; Address lintr warnings
JElchison Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Authors@R: c(
comment = "The research leading to these results has received funding from the European Research Council under the ERC Grant Agreement number 336167– the CHAI Project"),
person("rOpenSci", role = "fnd",
comment = "https://ropensci.org/"),
person("Daryl", "Herzmann", , "[email protected]", role = "ctb")
person("Daryl", "Herzmann", , "[email protected]", role = "ctb"),
person("Jonathan", "Elchison", email = "[email protected]", role = c("aut"),
comment = c(ORCID = "0009-0004-0787-3426"))
)
Description: Allows to get weather data from Automated Surface Observing
System (ASOS) stations (airports) in the whole world thanks to the
Expand Down Expand Up @@ -40,4 +42,4 @@ Suggests:
xts
Config/testthat/edition: 3
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ importFrom(jsonlite,read_json)
importFrom(magrittr,"%>%")
importFrom(rlang,"!!!")
importFrom(rlang,"%||%")
importFrom(rlang,`%||%`)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line duplicative of the previous? (See comment below.) I'm uncertain about the nuance of backticks vs. double-quotes.

importFrom(utils,read.table)
53 changes: 47 additions & 6 deletions R/measures.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#' Get weather data from one station
#'
#' @importFrom rlang `%||%`
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing here and here, should these backticks be removed? I'm uncertain about the nuance of backticks vs. double-quotes.

#'
#' @param station station ID, see riem_stations()
#' @param date_start date of start of the desired data, e.g. "2000-01-01"
#' @param date_start date of start of the desired data, e.g. "2016-01-01"
#' @inheritParams rlang::args_dots_empty
#' @param date_end date of end of the desired data, e.g. "2016-04-22"
#' @param data The data columns to return. The available options are: all, tmpf, dwpf, relh, drct, sknt, p01i, alti, mslp, vsby, gust, skyc1, skyc2, skyc3, skyc4, skyl1, skyl2, skyl3, skyl4, wxcodes, ice_accretion_1hr, ice_accretion_3hr, ice_accretion_6hr, peak_wind_gust, peak_wind_drct, peak_wind_time, feel, metar, snowdepth # nolint: line_length_linter
#' @param elev If TRUE, the elevation (m) of the station will be included in the output. # nolint: line_length_linter
#' @param latlon If TRUE, the latitude and longitude of the station will be included in the output. # nolint: line_length_linter
#' @param report_type The report type to query. The available options are "hfmetar" (skipped by default), "routine", "specials". # nolint: line_length_linter
#'
#' @return a data.frame (tibble tibble) with measures,
#' the number of columns can vary from station to station,
Expand Down Expand Up @@ -63,28 +69,63 @@
#' )
#' }
riem_measures <- function(
station = "VOHY",
date_start = "2014-01-01",
maelle marked this conversation as resolved.
Show resolved Hide resolved
date_end = as.character(Sys.Date())) {
station,
date_start,
...,
date_end = as.character(Sys.Date()),
data = "all",
elev = FALSE,
latlon = TRUE,
report_type = NULL) {
if (!rlang::is_character(station, n = 1L)) {
cli::cli_abort("{.arg station} must be a string.")
}
date_start <- format_and_check_date(date_start, "date_start")
rlang::check_dots_empty()
date_end <- format_and_check_date(date_end, "date_end")
if (date_end < date_start) {
cli::cli_abort("{.arg date_end} must be bigger than {.arg date_start}.")
}
if (!rlang::is_character(data, n = 1L)) {
cli::cli_abort("{.arg data} must be a string.")
}
if (!is.logical(elev)) {
cli::cli_abort("{.arg elev} must be a logical (TRUE/FALSE)") # nolint: nonportable_path_linter
}
if (!is.logical(latlon)) {
cli::cli_abort("{.arg latlon} must be a logical (TRUE/FALSE)") # nolint: nonportable_path_linter
}

report_type <- report_type %||% c("routine", "specials")
report_type <- tolower(report_type) # not case-sensitive
report_type <- rlang::arg_match(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your use of a map to abstract away the literals!

One side effect of rlang::arg_match(multiple = TRUE), however, is that the error message can be misleading in terms of the number required:

! `report_type` must be one of "hfmetar", "routine", or "specials", not "3,4".

It's the one of that throws me for a mild loop. I wonder if we could reduce any misleading by making line 12 a more clear comment, perhaps:

#' @param report_type A vector of strings, representing report types to query. The available options are "hfmetar", "routine", "specials". Default value is c("routine", "specials"). # nolint: line_length_linter

report_type,
values = c("hfmetar", "routine", "specials"),
multiple = TRUE
)
report_type <- purrr::map_int(
report_type,
\(x) switch(x, hfmetar = 1L, routine = 3L, specials = 4L) # nolint: unnecessary_lambda_linter
)
report_type <- paste(report_type, collapse = ",")

resp <- perform_riem_request(
path = "cgi-bin/request/asos.py/", # nolint: nonportable_path_linter
# query fields per https://mesonet.agron.iastate.edu/cgi-bin/request/asos.py?help # nolint: line_length_linter
query = list(
station = station,
data = "all",
data = data,
elev = ifelse(elev, "yes", "no"),
latlon = ifelse(latlon, "yes", "no"),
year1 = lubridate::year(date_start),
month1 = lubridate::month(date_start),
day1 = lubridate::day(date_start),
year2 = lubridate::year(date_end),
month2 = lubridate::month(date_end),
day2 = lubridate::day(date_end),
report_type = report_type,
format = "tdf",
latlon = "yes",
nometa = "no",
tz = "UTC"
)
)
Expand Down
23 changes: 19 additions & 4 deletions man/riem_measures.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 47 additions & 3 deletions tests/testthat/_snaps/measures.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
# riem_measures checks dates
# riem_measures fails if required 'station' param is absent

argument "station" is missing, with no default

# riem_measures fails if required 'date_start' param is absent

argument "date_start" is missing, with no default

# riem_measures validates 'station' param

`station` must be a string.

---

HTTP 422 Unprocessable Entity.

# riem_measures validates dates

x Invalid `date_start`: somethingelse.
i Correct format is YYYY-MM-DD.

---

x Invalid `date_end`: somethingelse.
x Invalid `date_start`: 2015 31 01.
i Correct format is YYYY-MM-DD.

---

x Invalid `date_start`: 2015 31 01.
x Invalid `date_end`: somethingelse.
i Correct format is YYYY-MM-DD.

---
Expand All @@ -22,3 +38,31 @@

`date_end` must be bigger than `date_start`.

# riem_measures validates 'elev' param

`elev` must be a logical (TRUE/FALSE)

---

`elev` must be a logical (TRUE/FALSE)

# riem_measures validates 'latlon' param

`latlon` must be a logical (TRUE/FALSE)

---

`latlon` must be a logical (TRUE/FALSE)

# riem_measures validates 'report_type' param

`report_type` must be one of "hfmetar", "routine", or "specials", not "11111".

---

`report_type` must be one of "hfmetar", "routine", or "specials", not "zzzzz".

---

`report_type` must be one of "hfmetar", "routine", or "specials", not "zzzzz".

Loading
Loading