Skip to content

Commit

Permalink
use native pipe (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
boshek authored Oct 4, 2024
1 parent f72aab9 commit b07d42d
Show file tree
Hide file tree
Showing 53 changed files with 252 additions and 275 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tidyhydat
Title: Extract and Tidy Canadian 'Hydrometric' Data
Version: 0.6.1
Version: 0.6.1.9000
Authors@R: c(person("Sam", "Albers", email = "sam.albers@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-9270-7884")),
person("David", "Hutchinson", email = "david.hutchinson@canada.ca", role = "ctb"),
Expand Down Expand Up @@ -45,4 +45,4 @@ VignetteBuilder: knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ S3method(plot,hy)
S3method(plot,realtime)
S3method(print,hy)
S3method(print,realtime)
export("%>%")
export(download_hydat)
export(enexpr)
export(enquo)
Expand Down Expand Up @@ -59,7 +58,6 @@ export(search_stn_name)
export(search_stn_number)
export(sym)
export(syms)
importFrom(dplyr,"%>%")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,UQ)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tidyhydat 0.6.2
# tidyhydat 0.6.1.9000
- bump minimum R version to 4.0.0
- dropped httr in favour of httr2
- fix bug where `download_hydat()` fails if `tempdir()` is on a different device than `hydat_path` (@mpdavison, #192)
Expand Down
6 changes: 3 additions & 3 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ hy_check <- function(hydat_path = NULL) {


invisible(lapply(have_tbls, function(x) {
tbl_rows <- dplyr::tbl(con, x) %>%
utils::head(1) %>%
dplyr::collect() %>%
tbl_rows <- dplyr::tbl(con, x) |>
utils::head(1) |>
dplyr::collect() |>
nrow()

if (tbl_rows == 0) {
Expand Down
10 changes: 5 additions & 5 deletions R/hy.R
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ hy_agency_list <- function(hydat_path = NULL) {
on.exit(hy_src_disconnect(hydat_con), add = TRUE)
}

agency_list <- dplyr::tbl(hydat_con, "AGENCY_LIST") %>%
agency_list <- dplyr::tbl(hydat_con, "AGENCY_LIST") |>
dplyr::collect()

as.hy(agency_list)
Expand Down Expand Up @@ -389,7 +389,7 @@ hy_reg_office_list <- function(hydat_path = NULL) {
on.exit(hy_src_disconnect(hydat_con), add = TRUE)
}

regional_office_list <- dplyr::tbl(hydat_con, "REGIONAL_OFFICE_LIST") %>%
regional_office_list <- dplyr::tbl(hydat_con, "REGIONAL_OFFICE_LIST") |>
dplyr::collect()

as.hy(regional_office_list)
Expand Down Expand Up @@ -418,7 +418,7 @@ hy_datum_list <- function(hydat_path = NULL) {
on.exit(hy_src_disconnect(hydat_con), add = TRUE)
}

datum_list <- dplyr::tbl(hydat_con, "DATUM_LIST") %>%
datum_list <- dplyr::tbl(hydat_con, "DATUM_LIST") |>
dplyr::collect()

as.hy(datum_list)
Expand Down Expand Up @@ -448,8 +448,8 @@ hy_version <- function(hydat_path = NULL) {
on.exit(hy_src_disconnect(hydat_con), add = TRUE)
}

version <- dplyr::tbl(hydat_con, "VERSION") %>%
dplyr::collect() %>%
version <- dplyr::tbl(hydat_con, "VERSION") |>
dplyr::collect() |>
dplyr::mutate(Date = lubridate::ymd_hms(Date))

version
Expand Down
4 changes: 2 additions & 2 deletions R/hy_annual_instant_peaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ hy_annual_instant_peaks <- function(station_number = NULL,
sym_STATION_NUMBER <- sym("STATION_NUMBER")

## Data manipulations
aip <- dplyr::tbl(hydat_con, "ANNUAL_INSTANT_PEAKS") %>%
dplyr::filter(!!sym_STATION_NUMBER %in% stns) %>%
aip <- dplyr::tbl(hydat_con, "ANNUAL_INSTANT_PEAKS") |>
dplyr::filter(!!sym_STATION_NUMBER %in% stns) |>
dplyr::collect()

## Add in english data type
Expand Down
10 changes: 5 additions & 5 deletions R/hy_annual_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ hy_annual_stats <- function(station_number = NULL,
annual_statistics <- dplyr::filter(annual_statistics, !!sym_YEAR >= start_year & !!sym_YEAR <= end_year)
}

annual_statistics <- dplyr::filter(annual_statistics, !!sym_STATION_NUMBER %in% stns) %>%
annual_statistics <- dplyr::filter(annual_statistics, !!sym_STATION_NUMBER %in% stns) |>
dplyr::collect()

## TODO: Figure out how to do this in fewer steps
Expand Down Expand Up @@ -106,10 +106,10 @@ hy_annual_stats <- function(station_number = NULL,
colnames(as_max) <- gsub("MAX_", "", names(as_max))

## bind into 1 dataframe and by year and join in the symbol
annual_statistics <- as_mean %>%
dplyr::bind_rows(as_min) %>%
dplyr::bind_rows(as_max) %>%
dplyr::arrange(YEAR) %>%
annual_statistics <- as_mean |>
dplyr::bind_rows(as_min) |>
dplyr::bind_rows(as_max) |>
dplyr::arrange(YEAR) |>
dplyr::left_join(tidyhydat::hy_data_symbols, by = c("SYMBOL" = "SYMBOL_ID"))

## Format date of occurence; SuppressWarnings are justified because NA's are valid for MEAN Sum_stat
Expand Down
4 changes: 2 additions & 2 deletions R/hy_db.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#'
#' # one you're sure the results are what you want
#' # get a data.frame using collect()
#' tbl(src, "STATIONS") %>%
#' filter(PROV_TERR_STATE_LOC == "BC") %>%
#' tbl(src, "STATIONS") |>
#' filter(PROV_TERR_STATE_LOC == "BC") |>
#' collect()
#'
#' # close the connection to the database
Expand Down
8 changes: 4 additions & 4 deletions R/hy_stations.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ hy_stations <- function(station_number = NULL,
sym_STATION_NUMBER <- sym("STATION_NUMBER")

## Create the dataframe to return
df <- dplyr::tbl(hydat_con, "STATIONS") %>%
dplyr::filter(!!sym_STATION_NUMBER %in% stns) %>%
dplyr::collect() %>%
dplyr::mutate(REGIONAL_OFFICE_ID = as.numeric(REGIONAL_OFFICE_ID)) %>%
df <- dplyr::tbl(hydat_con, "STATIONS") |>
dplyr::filter(!!sym_STATION_NUMBER %in% stns) |>
dplyr::collect() |>
dplyr::mutate(REGIONAL_OFFICE_ID = as.numeric(REGIONAL_OFFICE_ID)) |>
dplyr::mutate(
HYD_STATUS = dplyr::case_when(
HYD_STATUS == "D" ~ "DISCONTINUED",
Expand Down
4 changes: 2 additions & 2 deletions R/realtime.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ realtime_stations <- function(prov_terr_state_loc = NULL) {
#' @examples
#' \dontrun{
#'
#' realtime_dd(c("08MF005", "02LA004")) %>%
#' realtime_dd(c("08MF005", "02LA004")) |>
#' realtime_add_local_datetime()
#' }
#'
Expand Down Expand Up @@ -188,7 +188,7 @@ realtime_add_local_datetime <- function(.data, set_tz = NULL) {
#'
#' @examples
#' \dontrun{
#' realtime_dd("08MF005") %>% realtime_daily_mean()
#' realtime_dd("08MF005") |> realtime_daily_mean()
#' }
#'
#' @export
Expand Down
12 changes: 6 additions & 6 deletions R/utils-search.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ search_stn_name <- function(search_term, hydat_path = NULL) {
on.exit(hy_src_disconnect(hydat_con), add = TRUE)
}

results <- realtime_stations() %>%
dplyr::bind_rows(suppressMessages(hy_stations(hydat_path = hydat_con))) %>%
dplyr::distinct(STATION_NUMBER, .keep_all = TRUE) %>%
results <- realtime_stations() |>
dplyr::bind_rows(suppressMessages(hy_stations(hydat_path = hydat_con))) |>
dplyr::distinct(STATION_NUMBER, .keep_all = TRUE) |>
dplyr::select(STATION_NUMBER, STATION_NAME, PROV_TERR_STATE_LOC, LATITUDE, LONGITUDE)

results <- results[grepl(toupper(search_term), results$STATION_NAME), ]
Expand All @@ -51,9 +51,9 @@ search_stn_number <- function(search_term, hydat_path = NULL) {
on.exit(hy_src_disconnect(hydat_con), add = TRUE)
}

results <- realtime_stations() %>%
dplyr::bind_rows(suppressMessages(hy_stations(hydat_path = hydat_con))) %>%
dplyr::distinct(STATION_NUMBER, .keep_all = TRUE) %>%
results <- realtime_stations() |>
dplyr::bind_rows(suppressMessages(hy_stations(hydat_path = hydat_con))) |>
dplyr::distinct(STATION_NUMBER, .keep_all = TRUE) |>
dplyr::select(STATION_NUMBER, STATION_NAME, PROV_TERR_STATE_LOC, LATITUDE, LONGITUDE)

results <- results[grepl(toupper(search_term), results$STATION_NUMBER), ]
Expand Down
27 changes: 11 additions & 16 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ station_choice <- function(hydat_con, station_number, prov_terr_state_loc) {

## Get all stations
if (is.null(station_number) && is.null(prov_terr_state_loc)) {
stns <- dplyr::tbl(hydat_con, "STATIONS") %>%
dplyr::collect() %>%
stns <- dplyr::tbl(hydat_con, "STATIONS") |>
dplyr::collect() |>
dplyr::pull(STATION_NUMBER)
return(stns)
}
Expand All @@ -63,9 +63,9 @@ station_choice <- function(hydat_con, station_number, prov_terr_state_loc) {

if (any(!prov_terr_state_loc %in% stn_option) == TRUE) stop("Invalid prov_terr_state_loc value")

dplyr::tbl(hydat_con, "STATIONS") %>%
dplyr::filter(!!sym_PROV_TERR_STATE_LOC %in% prov_terr_state_loc) %>%
dplyr::collect() %>%
dplyr::tbl(hydat_con, "STATIONS") |>
dplyr::filter(!!sym_PROV_TERR_STATE_LOC %in% prov_terr_state_loc) |>
dplyr::collect() |>
dplyr::pull(STATION_NUMBER)
}
}
Expand Down Expand Up @@ -95,11 +95,6 @@ date_check <- function(start_date = NULL, end_date = NULL) {
invisible(list(start_is_null = start_is_null, end_is_null = end_is_null))
}

#' @importFrom dplyr %>%
#' @export
dplyr::`%>%`


## Simple error handler
#' @noRd
handle_error <- function(code) {
Expand Down Expand Up @@ -147,10 +142,10 @@ multi_param_msg <- function(data_arg, stns, params) {

sym_Parameter <- sym("Parameter")

flow_stns <- data_arg %>%
dplyr::filter(!!sym_Parameter == params) %>%
dplyr::distinct(STATION_NUMBER) %>%
dplyr::arrange(STATION_NUMBER) %>%
flow_stns <- data_arg |>
dplyr::filter(!!sym_Parameter == params) |>
dplyr::distinct(STATION_NUMBER) |>
dplyr::arrange(STATION_NUMBER) |>
dplyr::pull(STATION_NUMBER)

good_stns <- c()
Expand Down Expand Up @@ -225,8 +220,8 @@ tidyhydat_agent <- function(req) {
#' @examples
#' \dontrun{
#'
#' hy_stations(prov_terr_state_loc = "PE") %>%
#' pull_station_number() %>%
#' hy_stations(prov_terr_state_loc = "PE") |>
#' pull_station_number() |>
#' hy_annual_instant_peaks()
#' }
#'
Expand Down
24 changes: 12 additions & 12 deletions data-raw/HYDAT_internal_data/process_internal_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ create_olson <- function(t) {
}

#' A tibble of all Canadian Stations stations and their names.
allstations <- realtime_stations() %>%
mutate(HYD_STATUS = "ACTIVE", REAL_TIME = TRUE) %>%
bind_rows(hy_stations()) %>%
distinct(STATION_NUMBER, .keep_all = TRUE) %>%
select(STATION_NUMBER, STATION_NAME, PROV_TERR_STATE_LOC, HYD_STATUS, REAL_TIME, LATITUDE, LONGITUDE) %>%
mutate(station_tz = tz_lookup_coords(LATITUDE, LONGITUDE, method = "accurate")) %>%
mutate(standard_offset = map_dbl(station_tz, ~ tz_offset(.x))) %>%
mutate(OlsonName = map_chr(standard_offset, ~ create_olson(.x))) %>%
allstations <- realtime_stations() |>
mutate(HYD_STATUS = "ACTIVE", REAL_TIME = TRUE) |>
bind_rows(hy_stations()) |>
distinct(STATION_NUMBER, .keep_all = TRUE) |>
select(STATION_NUMBER, STATION_NAME, PROV_TERR_STATE_LOC, HYD_STATUS, REAL_TIME, LATITUDE, LONGITUDE) |>
mutate(station_tz = tz_lookup_coords(LATITUDE, LONGITUDE, method = "accurate")) |>
mutate(standard_offset = map_dbl(station_tz, ~ tz_offset(.x))) |>
mutate(OlsonName = map_chr(standard_offset, ~ create_olson(.x))) |>
write_csv("./data-raw/HYDAT_internal_data/allstations.csv")

## Manually adding NL for now
Expand All @@ -46,14 +46,14 @@ if (!all(unique(allstations$OlsonName) %in% c(OlsonNames(), "Etc/GMT+3.5"))) {
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), file.path(hy_dir(), "Hydat.sqlite3"))

## DATA_TYPES
hy_data_types <- tbl(hydat_con, "DATA_TYPES") %>%
collect() %>%
hy_data_types <- tbl(hydat_con, "DATA_TYPES") |>
collect() |>
mutate(DATA_TYPE_FR = iconv(DATA_TYPE_FR, from = "UTF-8", to = "ASCII//TRANSLIT"))
use_data(hy_data_types, overwrite = TRUE)

## DATA_SYMBOLS
hy_data_symbols <- tbl(hydat_con, "DATA_SYMBOLS") %>%
collect() %>%
hy_data_symbols <- tbl(hydat_con, "DATA_SYMBOLS") |>
collect() |>
mutate(SYMBOL_FR = iconv(SYMBOL_FR, from = "UTF-8", to = "ASCII//TRANSLIT"))
use_data(hy_data_symbols, overwrite = TRUE)

Expand Down
20 changes: 10 additions & 10 deletions data-raw/HYDAT_internal_data/tinyhydat_proc.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ table_vector <- c(
)

## List of tables with STATION_NUMBER INFORMATION
list_of_small_tables <- table_vector %>%
map(~ tbl(src = hydat_con, .) %>%
list_of_small_tables <- table_vector |>
map(~ tbl(src = hydat_con, .) |>
filter(STATION_NUMBER %in% c(
"08MF005", "08NM083", "08NE102",
"05AA008", "05HD008"
)) %>%
head(2000) %>%
collect()) %>%
)) |>
head(2000) |>
collect()) |>
set_names(table_vector)

## All tables without STATION_NUMBER
no_stn_table_vector <- all_tables[!all_tables %in% table_vector]

list_of_no_stn_tables <- no_stn_table_vector %>%
map(~ tbl(src = hydat_con, .) %>%
head(50) %>%
collect()) %>%
list_of_no_stn_tables <- no_stn_table_vector |>
map(~ tbl(src = hydat_con, .) |>
head(50) |>
collect()) |>
set_names(no_stn_table_vector)

SED_DATA_TYPES <- dplyr::tbl(hydat_con, "SED_DATA_TYPES") %>% collect()
SED_DATA_TYPES <- dplyr::tbl(hydat_con, "SED_DATA_TYPES") |> collect()

DBI::dbDisconnect(hydat_con)

Expand Down
4 changes: 2 additions & 2 deletions man/hy_agency_list.Rd

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

4 changes: 2 additions & 2 deletions man/hy_annual_instant_peaks.Rd

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

4 changes: 2 additions & 2 deletions man/hy_annual_stats.Rd

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

Loading

0 comments on commit b07d42d

Please sign in to comment.