From f78bd93b28012e467292a0ef2eaac18a7fd27ee2 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sat, 28 Feb 2026 23:34:05 +0100 Subject: [PATCH 01/11] add write_current_defaults() --- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/defaults.R | 30 ++++++++++++++++++++++++++++++ man/write_current_defaults.Rd | 18 ++++++++++++++++++ tests/testthat/test-defaults.R | 27 +++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 man/write_current_defaults.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 406f50a..4ccde07 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -37,7 +37,8 @@ Imports: ggnewscale, ggiraph, png, - jpeg + jpeg, + jsonlite Suggests: roxygen2, spelling, diff --git a/NAMESPACE b/NAMESPACE index f661033..291fa1e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,6 +21,7 @@ export(reset_fisdata_defaults) export(set_fisdata_defaults) export(show_url) export(summarise_results) +export(write_current_defaults) importFrom(dplyr,"%>%") importFrom(rlang,":=") importFrom(rlang,.data) diff --git a/R/defaults.R b/R/defaults.R index 0406912..c94843c 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -127,6 +127,36 @@ fd_def <- function(name = c("sector", "season", "gender", } +#' Read and Write Defaults from a JSON File +#' +#' Default settings can be written to a JSON file and read again from +#' this file. If the file `.fisdata.json` exists in the user's home +#' it is read automatically. +#' +#' @param file name of the JSON file to read or write +#' @param overwrite should an existing file be overwritten? +#' +#' @export + +write_current_defaults <- function(file = "~/.fisdata.json", + overwrite = FALSE) { + + if (file.exists(file) && !overwrite) { + cli::cli_abort("The file {file} exists. Use `overwrite = TRUE` to overwrite it.") + } + + defaults <- get_fisdata_defaults() + json <- defaults %>% + # convert to a list to avoid having an unnecessary length-one array + as.list() %>% + jsonlite::toJSON(pretty = TRUE, auto_unbox = TRUE) + + writeLines(json, file) + + invisible(json) +} + + # issue a message describing the default value that has been set. alert_default <- function(type, value, verbose) { diff --git a/man/write_current_defaults.Rd b/man/write_current_defaults.Rd new file mode 100644 index 0000000..7f003b4 --- /dev/null +++ b/man/write_current_defaults.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/defaults.R +\name{write_current_defaults} +\alias{write_current_defaults} +\title{Read and Write Defaults from a JSON File} +\usage{ +write_current_defaults(file = "~/.fisdata.json", overwrite = FALSE) +} +\arguments{ +\item{file}{name of the JSON file to read or write} + +\item{overwrite}{should an existing file be overwritten?} +} +\description{ +Default settings can be written to a JSON file and read again from +this file. If the file \code{.fisdata.json} exists in the user's home +it is read automatically. +} diff --git a/tests/testthat/test-defaults.R b/tests/testthat/test-defaults.R index bce9688..e378ed4 100644 --- a/tests/testthat/test-defaults.R +++ b/tests/testthat/test-defaults.R @@ -2,6 +2,8 @@ library(stringr) library(lubridate, warn.conflicts = FALSE) library(tibble) library(glue) +library(withr) +library(jsonlite) fd_opts <- paste0( "fisdata_", @@ -196,6 +198,31 @@ test_that("fd_def() works", { }) +test_that("write_current_defaults() works", { + reset_fisdata_defaults() + set_fisdata_defaults(sector = "AL", gender = "F", category = "WC") + ref <- toJSON( + list(sector = "AL", season = "", gender = "W", category = "WC", + discipline = "", active_only = FALSE), + auto_unbox = TRUE, + pretty = TRUE + ) + local_file("fisdata.json") + expect_equal(write_current_defaults("fisdata.json"), ref) + expect_true(file.exists("fisdata.json")) + expect_equal(paste(readLines("fisdata.json"), collapse = "\n"), ref, + ignore_attr = TRUE) +}) + + +test_that("write_current_defaults() handles existing file", { + local_file("fisdata.json") + write_current_defaults("fisdata.json") + expect_error(write_current_defaults("fisdata.json"), + "The file fisdata.json exists.") + expect_silent(write_current_defaults("fisdata.json", overwrite = TRUE)) +}) + # reset all defaults to their initial state reset_fisdata_defaults() From 760f1d0705f2dfa18b7af33732603209c38757a5 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sun, 1 Mar 2026 14:02:54 +0100 Subject: [PATCH 02/11] make some dependencies optional --- DESCRIPTION | 8 ++++---- R/helpers.R | 9 ++++++++- R/img_audio.R | 2 ++ R/plot.R | 18 +++++++++++++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4ccde07..36a203c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,18 +28,18 @@ Imports: rlang, cli, magrittr, - clipr, cachem, + jsonlite +Suggests: ggplot2, - grid, scales, colorspace, ggnewscale, ggiraph, + grid, png, jpeg, - jsonlite -Suggests: + clipr, roxygen2, spelling, withr, diff --git a/R/helpers.R b/R/helpers.R index b771f1e..1bf5ea8 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -24,7 +24,14 @@ show_url <- function(fisdata_df) { url <- attr(fisdata_df, "url") if (interactive() && !is.null(url)) { - clipr::write_clip(url) # nocov + if (rlang::is_installed("clipr")) { # nocov start + clipr::write_clip(url) + } else { + cli::cli_alert_info( + c("Install {.pkg clipr} with {.run install.packages(\"clipr\")} ", + "to automatically copy the url to the clipboard.") + ) + } # nocov end } url } diff --git a/R/img_audio.R b/R/img_audio.R index 351efa6..b967a4a 100644 --- a/R/img_audio.R +++ b/R/img_audio.R @@ -25,6 +25,8 @@ #' @export get_athlete_image <- function(athlete, file = NULL) { + rlang::check_installed(c("grid", "png", "jpeg"), "to process images") + athlete <- ensure_one_athlete(athlete) id <- athlete$competitor_id diff --git a/R/plot.R b/R/plot.R index 15e2f32..5cd4f7f 100644 --- a/R/plot.R +++ b/R/plot.R @@ -32,6 +32,10 @@ plot_rank_summary <- function(results, interactive = TRUE, width = NULL, height = NULL) { + rlang::check_installed( + c("ggplot2", "colorspace", "ggnewscale", "ggiraph"), + "to create plots." + ) by <- match_groupings(by, c("category", "discipline")) @@ -101,6 +105,10 @@ plot_results_summary <- function(results, interactive = TRUE, width = NULL, height = NULL) { + rlang::check_installed( + c("ggplot2", "scales", "ggiraph"), + "to create plots." + ) variable <- match.arg(variable) @@ -218,7 +226,11 @@ plot_ranks_over_time <- function(results, interactive = TRUE, width = NULL, height = NULL) { - + rlang::check_installed( + c("ggplot2", "colorspace", "ggnewscale", "ggiraph"), + "to create plots." + ) + by <- if (length(by) > 0 && all(!is.na(by))) match.arg(by) time <- match.arg(time) type <- match.arg(type) @@ -328,6 +340,10 @@ plot_results_over_time <- function(results, interactive = TRUE, width = NULL, height = NULL) { + rlang::check_installed( + c("ggplot2", "scales", "ggiraph"), + "to create plots." + ) variable <- match.arg(variable) by <- if (length(by) > 0 && all(!is.na(by))) match.arg(by) From b3569884c14b9407cb1b16989808565558f62a17 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sun, 22 Mar 2026 13:58:05 +0100 Subject: [PATCH 03/11] reexport %>% --- NAMESPACE | 1 + R/fisdata-package.R | 5 +++++ man/reexports.Rd | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 man/reexports.Rd diff --git a/NAMESPACE b/NAMESPACE index 291fa1e..1e3d618 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export("%>%") export(browse_url) export(fd_def) export(get_athlete_image) diff --git a/R/fisdata-package.R b/R/fisdata-package.R index 6641390..ff4ef25 100644 --- a/R/fisdata-package.R +++ b/R/fisdata-package.R @@ -30,3 +30,8 @@ cache <- new.env() } # nocov end + + +# reexport %>% for convenience +#' @export +dplyr::`%>%` \ No newline at end of file diff --git a/man/reexports.Rd b/man/reexports.Rd new file mode 100644 index 0000000..3238477 --- /dev/null +++ b/man/reexports.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fisdata-package.R +\docType{import} +\name{reexports} +\alias{reexports} +\alias{\%>\%} +\title{Objects exported from other packages} +\keyword{internal} +\description{ +These objects are imported from other packages. Follow the links +below to see their documentation. + +\describe{ + \item{dplyr}{\code{\link[dplyr:reexports]{\%>\%}}} +}} + From 3641aaa9aab0e7ea2a4d0eadb80a21751deba107 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sun, 22 Mar 2026 17:14:00 +0100 Subject: [PATCH 04/11] fix test name such that it is recognised by positron --- tests/testthat/test-defaults.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-defaults.R b/tests/testthat/test-defaults.R index e378ed4..56db2f7 100644 --- a/tests/testthat/test-defaults.R +++ b/tests/testthat/test-defaults.R @@ -53,7 +53,7 @@ test_that("set_fisdata_defaults() works with valid inputs", { }) -test_that("set_fisdata_defaults() works with \"\"", { +test_that('set_fisdata_defaults() works with ""', { set_fisdata_defaults(sector = "") expect_equal(getOption("fisdata_sector"), "") From a704df867217aba1de90f72864916c7952f36ed7 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sun, 22 Mar 2026 17:16:19 +0100 Subject: [PATCH 05/11] move preparation of defaults to a function --- R/defaults.R | 129 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 52 deletions(-) diff --git a/R/defaults.R b/R/defaults.R index c94843c..6bc3ecd 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -38,58 +38,20 @@ set_fisdata_defaults <- function(sector = NULL, } } - if (!is.null(sector)) { - use_sector <- find_code(sector, "sector") - options(fisdata_sector = use_sector) - alert_default("sector", use_sector, verbose) - } - - if (!is.null(season)) { - season_int <- suppressWarnings(as.integer(season)) - if (season == "") { - options(fisdata_season = "") - alert_default("season", season, verbose) - } else if (is.na(season_int) | season_int < 1950 | - season_int > lubridate::year(today()) + 1) { - cli::cli_warn("'{season}' is not a valid season.") - } else { - options(fisdata_season = as.character(season_int)) - alert_default("season", season_int, verbose) - } - } - - if (!is.null(gender)) { - use_gender <- standardise_gender(gender) - if (!use_gender %in% c("", "M", "W")) { - cli::cli_warn("'{gender}' is not a valid gender code.") - } else { - options(fisdata_gender = use_gender) - alert_default("gender", use_gender, verbose) - } - } - - if (!is.null(category)) { - use_category <- find_code(category, "category") - options(fisdata_category = use_category) - alert_default("category", use_category, verbose) - } - - if (!is.null(discipline)) { - # when matching the discipline, use the default value for the sector - # if set_fisdata_defaults has been called with a sector, it's value has - # already been set as the default such that it will also be used here - use_discipline <- find_code(discipline, "discipline", - sector = fd_def("sector")) - options(fisdata_discipline = use_discipline) - alert_default("discipline", use_discipline, verbose) - } - - if (!is.null(active_only)) { - if (!active_only %in% c(TRUE, FALSE)) { - cli::cli_warn("'{active_only}' is not valid for active_only.") - } else { - options(fisdata_active_only = active_only) - alert_default("active_only", active_only, verbose) + defs <- prepare_defaults(sector = sector, + season = season, + gender = gender, + category = category, + discipline = discipline, + active_only = active_only) + + for (name in names(defs)) { + value <- defs[[name]] + if (!is.null(value)) { + options( + magrittr::set_names(list(value), paste0("fisdata_", name)) + ) + alert_default(name, value, verbose) } } } @@ -181,3 +143,66 @@ alert_default <- function(type, value, verbose) { } } } + + +# prepare inputs for default values +prepare_defaults <- function(sector = NULL, + season = NULL, + gender = NULL, + category = NULL, + discipline = NULL, + active_only = NULL) { + + if (!is.null(sector)) { + sector <- find_code(sector, "sector") + } + + if (!is.null(season)) { + season_int <- suppressWarnings(as.integer(season)) + if (season == "") { + season <- "" + } else if (is.na(season_int) | season_int < 1950 | + season_int > lubridate::year(today()) + 1) { + cli::cli_warn("'{season}' is not a valid season.") + season <- NULL + } else { + season <- as.character(season_int) + } + } + + if (!is.null(gender)) { + std_gender <- standardise_gender(gender) + if (!std_gender %in% c("", "M", "W")) { + cli::cli_warn("'{gender}' is not a valid gender code.") + gender <- NULL + } else { + gender <- std_gender + } + } + + if (!is.null(category)) { + category <- find_code(category, "category") + } + + if (!is.null(discipline)) { + # discipline depends on sector. If a sector has been passed to this + # function, use it. Otherwise, use the current default. + discipline_sector <- if (!is.null(sector)) sector else fd_def("sector") + discipline <- find_code(discipline, "discipline", + sector = discipline_sector) + } + + if (!is.null(active_only)) { + if (!active_only %in% c(TRUE, FALSE)) { + cli::cli_warn("'{active_only}' is not valid for active_only.") + active_only <- NULL + } + } + + list(sector = sector, + season = season, + gender = gender, + category = category, + discipline = discipline, + active_only = active_only) +} \ No newline at end of file From 24a683e355f4eafdf8f574593004774da218f940 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sun, 22 Mar 2026 17:57:46 +0100 Subject: [PATCH 06/11] remove obsolete argument from alert_default() --- R/defaults.R | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/R/defaults.R b/R/defaults.R index 6bc3ecd..4fc2430 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -51,7 +51,7 @@ set_fisdata_defaults <- function(sector = NULL, options( magrittr::set_names(list(value), paste0("fisdata_", name)) ) - alert_default(name, value, verbose) + alert_default(name, verbose) } } } @@ -120,12 +120,16 @@ write_current_defaults <- function(file = "~/.fisdata.json", # issue a message describing the default value that has been set. -alert_default <- function(type, value, verbose) { +# The function must be called AFTER setting the default. +alert_default <- function(type, verbose) { if (!verbose) { return(NULL) } + # get the default value that has been set + value <- fd_def(type) + # if the value is an empty string, issue a message saying this if (value == "") { cli::cli_alert_info("The default for '{type}' has been set to ''.") From 4f062ee87a0c3447dcd5e5f4034c973aa72fe888 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Mon, 23 Mar 2026 17:04:52 +0100 Subject: [PATCH 07/11] move writing of defaults json to a function --- R/defaults.R | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/R/defaults.R b/R/defaults.R index 4fc2430..fd49fbc 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -102,12 +102,20 @@ fd_def <- function(name = c("sector", "season", "gender", write_current_defaults <- function(file = "~/.fisdata.json", overwrite = FALSE) { + write_defaults_(get_fisdata_defaults(), file, overwrite) +} + +# helper function that writes a list or tibble of defaults to a JSON file +write_defaults_ <- function(defaults, + file = "~/.fisdata.json", + overwrite = FALSE, + error_call = rlang::caller_env()) { + if (file.exists(file) && !overwrite) { cli::cli_abort("The file {file} exists. Use `overwrite = TRUE` to overwrite it.") } - defaults <- get_fisdata_defaults() json <- defaults %>% # convert to a list to avoid having an unnecessary length-one array as.list() %>% @@ -129,7 +137,7 @@ alert_default <- function(type, verbose) { # get the default value that has been set value <- fd_def(type) - + # if the value is an empty string, issue a message saying this if (value == "") { cli::cli_alert_info("The default for '{type}' has been set to ''.") From 8c6af267d2cd4f4b3819d221ece5aafb28d9c845 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Wed, 25 Mar 2026 22:30:46 +0100 Subject: [PATCH 08/11] add write_defaults() --- NAMESPACE | 1 + R/defaults.R | 36 ++++++++++++++++++++++ man/write_current_defaults.Rd | 18 ----------- man/write_defaults.Rd | 56 ++++++++++++++++++++++++++++++++++ tests/testthat/test-defaults.R | 36 ++++++++++++++++++++++ 5 files changed, 129 insertions(+), 18 deletions(-) delete mode 100644 man/write_current_defaults.Rd create mode 100644 man/write_defaults.Rd diff --git a/NAMESPACE b/NAMESPACE index 1e3d618..a682858 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,6 +23,7 @@ export(set_fisdata_defaults) export(show_url) export(summarise_results) export(write_current_defaults) +export(write_defaults) importFrom(dplyr,"%>%") importFrom(rlang,":=") importFrom(rlang,.data) diff --git a/R/defaults.R b/R/defaults.R index fd49fbc..ae7d12c 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -97,9 +97,32 @@ fd_def <- function(name = c("sector", "season", "gender", #' #' @param file name of the JSON file to read or write #' @param overwrite should an existing file be overwritten? +#' @inheritParams query_athletes +#' @inheritParams query_results #' #' @export +write_defaults <- function(file = "~/.fisdata.json", + overwrite = FALSE, + sector = "", + season = "", + gender = "", + category = "", + discipline = "", + active_only = FALSE) { + defs <- prepare_defaults(sector = sector, + season = season, + gender = gender, + category = category, + discipline = discipline, + active_only = active_only) + write_defaults_(defs, file, overwrite) +} + + +#' @rdname write_defaults +#' @export + write_current_defaults <- function(file = "~/.fisdata.json", overwrite = FALSE) { write_defaults_(get_fisdata_defaults(), file, overwrite) @@ -116,6 +139,19 @@ write_defaults_ <- function(defaults, cli::cli_abort("The file {file} exists. Use `overwrite = TRUE` to overwrite it.") } + # don't write NULL for any default to the file + def_is_null <- purrr::map_lgl(defaults, is.null) + if (any(def_is_null)) { + null_names <- names(def_is_null)[def_is_null] + cli::cli_abort( + c( + "!" = "Defaults must no be NULL when writing to a json file.", + "i" = "The following value{?s} {?is/are} NULL: {null_names}." + ), + call = error_call + ) + } + json <- defaults %>% # convert to a list to avoid having an unnecessary length-one array as.list() %>% diff --git a/man/write_current_defaults.Rd b/man/write_current_defaults.Rd deleted file mode 100644 index 7f003b4..0000000 --- a/man/write_current_defaults.Rd +++ /dev/null @@ -1,18 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/defaults.R -\name{write_current_defaults} -\alias{write_current_defaults} -\title{Read and Write Defaults from a JSON File} -\usage{ -write_current_defaults(file = "~/.fisdata.json", overwrite = FALSE) -} -\arguments{ -\item{file}{name of the JSON file to read or write} - -\item{overwrite}{should an existing file be overwritten?} -} -\description{ -Default settings can be written to a JSON file and read again from -this file. If the file \code{.fisdata.json} exists in the user's home -it is read automatically. -} diff --git a/man/write_defaults.Rd b/man/write_defaults.Rd new file mode 100644 index 0000000..7f3a517 --- /dev/null +++ b/man/write_defaults.Rd @@ -0,0 +1,56 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/defaults.R +\name{write_defaults} +\alias{write_defaults} +\alias{write_current_defaults} +\title{Read and Write Defaults from a JSON File} +\usage{ +write_defaults( + file = "~/.fisdata.json", + overwrite = FALSE, + sector = "", + season = "", + gender = "", + category = "", + discipline = "", + active_only = FALSE +) + +write_current_defaults(file = "~/.fisdata.json", overwrite = FALSE) +} +\arguments{ +\item{file}{name of the JSON file to read or write} + +\item{overwrite}{should an existing file be overwritten?} + +\item{sector}{abbreviation of the sector, e.g., "AL" for +alpine skiing. Not case-sensitive. +See the dataset \link{sectors} for possible values. +If a string not matching a sector code is used, a similar string +is searched for in the description column of \link{sectors}.} + +\item{season}{year when the season ended, i.e., 2020 stands for the season +2019/2020. It is not possible to filter for multiple seasons at once.} + +\item{gender}{abbreviation of the gender: "M" for male/men, +"F" or "W" for female/women.} + +\item{category}{abbreviation of the category of the race, e.g., "WC" for +"World Cup". Not case-sensitive. +See the dataset \link{categories} for possible values. +If a string not matching a category code is used, a similar string +is searched for in the description column of \link{categories}.} + +\item{discipline}{abbreviation for the discipline, e.g., "DH" for +"Downhill". Not case sensitive. +See the dataset \link{disciplines} for possible values. +If a string not matching a discipline code is used, a similar string +is searched for in the description column of \link{disciplines}.} + +\item{active_only}{should the query be restricted to active athletes.} +} +\description{ +Default settings can be written to a JSON file and read again from +this file. If the file \code{.fisdata.json} exists in the user's home +it is read automatically. +} diff --git a/tests/testthat/test-defaults.R b/tests/testthat/test-defaults.R index 56db2f7..5170a66 100644 --- a/tests/testthat/test-defaults.R +++ b/tests/testthat/test-defaults.R @@ -223,6 +223,42 @@ test_that("write_current_defaults() handles existing file", { expect_silent(write_current_defaults("fisdata.json", overwrite = TRUE)) }) + +test_that("write_defaults() works", { + ref <- toJSON( + list(sector = "CC", season = "", gender = "M", category = "WC", + discipline = "", active_only = FALSE), + auto_unbox = TRUE, + pretty = TRUE + ) + local_file("fisdata.json") + expect_equal( + write_defaults("fisdata.json", sector = "CC", gender = "M", category = "WC"), + ref + ) + expect_true(file.exists("fisdata.json")) + expect_equal(paste(readLines("fisdata.json"), collapse = "\n"), ref, + ignore_attr = TRUE) +}) + + +test_that("write_defaults() handles existing file", { + local_file("fisdata.json") + write_defaults("fisdata.json", sector = "CC", gender = "M") + expect_error(write_defaults("fisdata.json", sector = "CC", gender = "M"), + "The file fisdata.json exists.") + expect_silent(write_current_defaults("fisdata.json", overwrite = TRUE)) +}) + + +test_that("write_defaults() rejects NULL as default", { + local_file("fisdata.json") + expect_error(write_defaults("fisdata.json", sector = "CC", discipline = NULL), + "Defaults must no be NULL.*NULL: discipline") + expect_false(file.exists("fisdata.json")) +}) + + # reset all defaults to their initial state reset_fisdata_defaults() From a06836266b1e74f29038e1b025ee3a3a14b95082 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sat, 28 Mar 2026 11:14:07 +0100 Subject: [PATCH 09/11] add read_defaults() --- NAMESPACE | 1 + R/defaults.R | 59 ++++++++++++++++++++ man/write_defaults.Rd | 12 +++++ tests/testthat/test-defaults.R | 99 +++++++++++++++++++++++++++++++++- 4 files changed, 169 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index a682858..7b8d37b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,6 +18,7 @@ export(query_events) export(query_race) export(query_results) export(query_standings) +export(read_defaults) export(reset_fisdata_defaults) export(set_fisdata_defaults) export(show_url) diff --git a/R/defaults.R b/R/defaults.R index ae7d12c..9ec4fce 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -163,6 +163,65 @@ write_defaults_ <- function(defaults, } +#' @param apply should the defaults be applied? +#' @param verbose should the function create output. This defaults +#' to `TRUE` in interactive sessions or when `apply` is `FALSE`. +#' @rdname write_defaults +#' @export + +read_defaults <- function(file = "~/.fisdata.json", + apply = TRUE, + verbose = !apply || interactive()) { + + if (!file.exists(file)) { + cli::cli_abort("The file {file} does not exist.") + } + + raw <- purrr::possibly(jsonlite::fromJSON)(file) + if (is.null(raw)) { + cli::cli_abort("Failed to parse file {file} as JSON.") + } + + # check that all the expected values are present. + expected <- c("sector", "season", "gender", "category", "discipline", "active_only") + is_present <- expected %in% names(raw) + if (any(!is_present)) { + cli::cli_abort("Some defaults have no value set: {expected[!is_present]}") + } + + # run the contents of the file through prepare_defs() to check + # that the values are valid. + error_call = rlang::current_call() + defs <- tryCatch( + do.call(prepare_defaults, raw[names(raw) %in% expected]), + error = function(e) { + cli::cli_abort(c("The contents of file {file} are not valid.", + "i" = "Error message: {e$message}"), + call = error_call) + }, + warning = function(w) { + cli::cli_abort(c("The contents of file {file} are not valid.", + "i" = "Warning message: {w$message}"), + call = error_call) + } + ) + + # if verbose and the default are not to be applied, print them here + # in case the defaults are applied, set_fisdata_defaults() will create output. + if (verbose && !apply) { + cli::cli_alert_info("The file {file} contains the following defaults:") + print(dplyr::as_tibble(defs)) + } + + # if requested, apply the defaults + if (apply) { + do.call(set_fisdata_defaults, c(defs, list(verbose = verbose))) + } + + invisible(dplyr::as_tibble(defs)) +} + + # issue a message describing the default value that has been set. # The function must be called AFTER setting the default. alert_default <- function(type, verbose) { diff --git a/man/write_defaults.Rd b/man/write_defaults.Rd index 7f3a517..0a4fcfd 100644 --- a/man/write_defaults.Rd +++ b/man/write_defaults.Rd @@ -3,6 +3,7 @@ \name{write_defaults} \alias{write_defaults} \alias{write_current_defaults} +\alias{read_defaults} \title{Read and Write Defaults from a JSON File} \usage{ write_defaults( @@ -17,6 +18,12 @@ write_defaults( ) write_current_defaults(file = "~/.fisdata.json", overwrite = FALSE) + +read_defaults( + file = "~/.fisdata.json", + apply = TRUE, + verbose = !apply || interactive() +) } \arguments{ \item{file}{name of the JSON file to read or write} @@ -48,6 +55,11 @@ If a string not matching a discipline code is used, a similar string is searched for in the description column of \link{disciplines}.} \item{active_only}{should the query be restricted to active athletes.} + +\item{apply}{should the defaults be applied?} + +\item{verbose}{should the function create output. This defaults +to \code{TRUE} in interactive sessions or when \code{apply} is \code{FALSE}.} } \description{ Default settings can be written to a JSON file and read again from diff --git a/tests/testthat/test-defaults.R b/tests/testthat/test-defaults.R index 5170a66..683b7e1 100644 --- a/tests/testthat/test-defaults.R +++ b/tests/testthat/test-defaults.R @@ -259,6 +259,101 @@ test_that("write_defaults() rejects NULL as default", { }) -# reset all defaults to their initial state -reset_fisdata_defaults() +test_that("read_defaults() reads defaults without applying them", { + local_file("fisdata.json") + write_defaults("fisdata.json", sector = "AL", season = "2024", gender = "W", + category = "WC", discipline = "SL", active_only = TRUE) + reset_fisdata_defaults() + + expect_equal( + read_defaults("fisdata.json", apply = FALSE, verbose = FALSE), + tibble(sector = "AL", season = "2024", gender = "W", category = "WC", + discipline = "SL", active_only = TRUE) + ) + expect_equal( + get_fisdata_defaults(), + tibble(sector = "", season = "", gender = "", + category = "", discipline = "", active_only = FALSE) + ) +}) + + +test_that("read_defaults() applies defaults", { + local_file("fisdata.json") + write_defaults("fisdata.json", sector = "CC", season = "2025", gender = "M", + category = "WC", discipline = "SP", active_only = TRUE) + reset_fisdata_defaults() + + expect_equal( + read_defaults("fisdata.json", verbose = FALSE), + tibble(sector = "CC", season = "2025", gender = "M", + category = "WC", discipline = "SP", active_only = TRUE) + ) + expect_equal( + get_fisdata_defaults(), + tibble(sector = "CC", season = "2025", gender = "M", + category = "WC", discipline = "SP", active_only = TRUE) + ) +}) + +test_that("read_defaults() handles invalid files", { + local_file("fisdata.json") + + expect_error(read_defaults("fisdata.json"), "does not exist") + + writeLines("{", "fisdata.json") + expect_error(read_defaults("fisdata.json"), "Failed to parse") + + writeLines( + toJSON( + list(sector = "AL", season = "2024", gender = "W", category = "WC", + discipline = "SL"), + auto_unbox = TRUE + ), + "fisdata.json" + ) + expect_error(read_defaults("fisdata.json"), "Some defaults have no value set: active_only") + + writeLines( + toJSON( + list(sector = c("AL", "CC"), season = "1940", gender = "W", category = "WC", + discipline = "SL", active_only = TRUE), + auto_unbox = TRUE + ), + "fisdata.json" + ) + expect_error(read_defaults("fisdata.json"), "contents.*not valid") + + writeLines( + toJSON( + list(sector = "AL", season = "1940", gender = "W", category = "WC", + discipline = "SL", active_only = TRUE), + auto_unbox = TRUE + ), + "fisdata.json" + ) + expect_error(read_defaults("fisdata.json"), "'1940' is not a valid season") +}) + + +test_that("read_defaults() creates output in the appropriate situations", { + local_file("fisdata.json") + write_defaults("fisdata.json", sector = "CC", season = "2025", gender = "M", + category = "WC", discipline = "SP", active_only = TRUE) + + expect_silent(read_defaults("fisdata.json", apply = FALSE, verbose = FALSE)) + expect_silent(read_defaults("fisdata.json", apply = TRUE, verbose = FALSE)) + read_defaults("fisdata.json", apply = FALSE, verbose = TRUE) %>% + expect_message("contains the following defaults") %>% + expect_output("tibble.*sector +season +gender +category") + read_defaults("fisdata.json", apply = TRUE, verbose = TRUE) %>% + expect_message("sector.*CC.*Cross-Country") %>% + expect_message("season.*2025") %>% + expect_message("gender.*M") %>% + expect_message("category.*WC.*World Cup") %>% + expect_message("discipline.*SP") %>% + expect_message("active_only.*'TRUE'") +}) + +reset_fisdata_defaults() From 6220f37ae2fcd471b54075a5af76523576ad376e Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sat, 28 Mar 2026 12:34:46 +0100 Subject: [PATCH 10/11] let fisdata read defaults on load --- DESCRIPTION | 2 +- R/defaults.R | 17 ++++++++++++++++- R/fisdata-package.R | 12 ++++++++++++ inst/WORDLIST | 1 + man/write_defaults.Rd | 16 +++++++++++++++- 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 36a203c..633c0c6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: fisdata Title: Query Data from the FIS Web Page -Version: 0.2.0.9001 +Version: 0.2.0.9002 Authors@R: person("Stefan", "Lanz", , "slanz1137@gmail.com", role = c("aut", "cre")) Description: Query data from the web site of the International Ski and diff --git a/R/defaults.R b/R/defaults.R index 9ec4fce..3d7e00c 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -93,13 +93,27 @@ fd_def <- function(name = c("sector", "season", "gender", #' #' Default settings can be written to a JSON file and read again from #' this file. If the file `.fisdata.json` exists in the user's home -#' it is read automatically. +#' it is read automatically when fisdata is loaded in an interactive +#' session (see 'Details' for how to configure this behaviour). #' #' @param file name of the JSON file to read or write #' @param overwrite should an existing file be overwritten? #' @inheritParams query_athletes #' @inheritParams query_results #' +#' @details +#' When fisdata is loaded in an interactive session, it tries to load defaults +#' from a file `fisdata.json`. You can use another file by setting the environment +#' variable `FISDATA_DEFAULTS_FILE` to the path to this file before loading +#' fisdata. To do this once, you can use [Sys.setenv()], to configure R to always +#' load a different file, you can set `FISDATA_DEFAULTS_FILE` in your `.Renviron` +#' file. +#' +#' @returns +#' `write_defaults()` and `write_current_defaults()` return the json-string that +#' was written to the file (invisibly). `read_defaults()` returns the default +#' values that were read as a tibble (invisibly). +#' #' @export write_defaults <- function(file = "~/.fisdata.json", @@ -167,6 +181,7 @@ write_defaults_ <- function(defaults, #' @param verbose should the function create output. This defaults #' to `TRUE` in interactive sessions or when `apply` is `FALSE`. #' @rdname write_defaults +#' #' @export read_defaults <- function(file = "~/.fisdata.json", diff --git a/R/fisdata-package.R b/R/fisdata-package.R index ff4ef25..2f4ac0b 100644 --- a/R/fisdata-package.R +++ b/R/fisdata-package.R @@ -27,6 +27,18 @@ cache <- new.env() # set options to default values reset_fisdata_defaults() + + # determine the file that defaults should be read from. The file name is + # taken from the environment variable FISDATA_DEFAULTS_FILE. If the variable + # is unset, fall back to "~/.fisdata.json". + defaults_file <- Sys.getenv("FISDATA_DEFAULTS_FILE", "~/.fisdata.json") + + # if the file exists and R is running interactively, + # read defaults from it. + if (interactive() && file.exists(defaults_file)) { + cli::cli_alert_info("Reading default values from {defaults_file} ...") + read_defaults(defaults_file, verbose = TRUE) + } } # nocov end diff --git a/inst/WORDLIST b/inst/WORDLIST index 601035d..dea917c 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -14,6 +14,7 @@ Wengen dnf ggiraph ggplot +json parasports png pos diff --git a/man/write_defaults.Rd b/man/write_defaults.Rd index 0a4fcfd..5ced9ad 100644 --- a/man/write_defaults.Rd +++ b/man/write_defaults.Rd @@ -61,8 +61,22 @@ is searched for in the description column of \link{disciplines}.} \item{verbose}{should the function create output. This defaults to \code{TRUE} in interactive sessions or when \code{apply} is \code{FALSE}.} } +\value{ +\code{write_defaults()} and \code{write_current_defaults()} return the json-string that +was written to the file (invisibly). \code{read_defaults()} returns the default +values that were read as a tibble (invisibly). +} \description{ Default settings can be written to a JSON file and read again from this file. If the file \code{.fisdata.json} exists in the user's home -it is read automatically. +it is read automatically when fisdata is loaded in an interactive +session (see 'Details' for how to configure this behaviour). +} +\details{ +When fisdata is loaded in an interactive session, it tries to load defaults +from a file \code{fisdata.json}. You can use another file by setting the environment +variable \code{FISDATA_DEFAULTS_FILE} to the path to this file before loading +fisdata. To do this once, you can use \code{\link[=Sys.setenv]{Sys.setenv()}}, to configure R to always +load a different file, you can set \code{FISDATA_DEFAULTS_FILE} in your \code{.Renviron} +file. } From 9c2427012b8873c28e52d589823520c8f0847af8 Mon Sep 17 00:00:00 2001 From: stibu81 Date: Sat, 28 Mar 2026 13:09:11 +0100 Subject: [PATCH 11/11] rename functions to read/write defaults --- NAMESPACE | 6 +- R/defaults.R | 42 ++++++------ R/fisdata-package.R | 2 +- ..._defaults.Rd => write_fisdata_defaults.Rd} | 18 +++--- pkgdown/_pkgdown.yml | 1 + tests/testthat/test-defaults.R | 64 +++++++++---------- 6 files changed, 67 insertions(+), 66 deletions(-) rename man/{write_defaults.Rd => write_fisdata_defaults.Rd} (85%) diff --git a/NAMESPACE b/NAMESPACE index 7b8d37b..3af1eb9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,13 +18,13 @@ export(query_events) export(query_race) export(query_results) export(query_standings) -export(read_defaults) +export(read_fisdata_defaults) export(reset_fisdata_defaults) export(set_fisdata_defaults) export(show_url) export(summarise_results) -export(write_current_defaults) -export(write_defaults) +export(write_current_fisdata_defaults) +export(write_fisdata_defaults) importFrom(dplyr,"%>%") importFrom(rlang,":=") importFrom(rlang,.data) diff --git a/R/defaults.R b/R/defaults.R index 3d7e00c..ec3e6ad 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -110,44 +110,44 @@ fd_def <- function(name = c("sector", "season", "gender", #' file. #' #' @returns -#' `write_defaults()` and `write_current_defaults()` return the json-string that -#' was written to the file (invisibly). `read_defaults()` returns the default +#' `write_fisdata_defaults()` and `write_current_fisdata_defaults()` return the json-string that +#' was written to the file (invisibly). `read_fisdata_defaults()` returns the default #' values that were read as a tibble (invisibly). #' #' @export -write_defaults <- function(file = "~/.fisdata.json", - overwrite = FALSE, - sector = "", - season = "", - gender = "", - category = "", - discipline = "", - active_only = FALSE) { +write_fisdata_defaults <- function(file = "~/.fisdata.json", + overwrite = FALSE, + sector = "", + season = "", + gender = "", + category = "", + discipline = "", + active_only = FALSE) { defs <- prepare_defaults(sector = sector, season = season, gender = gender, category = category, discipline = discipline, active_only = active_only) - write_defaults_(defs, file, overwrite) + write_fisdata_defaults_(defs, file, overwrite) } -#' @rdname write_defaults +#' @rdname write_fisdata_defaults #' @export -write_current_defaults <- function(file = "~/.fisdata.json", - overwrite = FALSE) { - write_defaults_(get_fisdata_defaults(), file, overwrite) +write_current_fisdata_defaults <- function(file = "~/.fisdata.json", + overwrite = FALSE) { + write_fisdata_defaults_(get_fisdata_defaults(), file, overwrite) } # helper function that writes a list or tibble of defaults to a JSON file -write_defaults_ <- function(defaults, - file = "~/.fisdata.json", - overwrite = FALSE, - error_call = rlang::caller_env()) { +write_fisdata_defaults_ <- function(defaults, + file = "~/.fisdata.json", + overwrite = FALSE, + error_call = rlang::caller_env()) { if (file.exists(file) && !overwrite) { cli::cli_abort("The file {file} exists. Use `overwrite = TRUE` to overwrite it.") @@ -180,11 +180,11 @@ write_defaults_ <- function(defaults, #' @param apply should the defaults be applied? #' @param verbose should the function create output. This defaults #' to `TRUE` in interactive sessions or when `apply` is `FALSE`. -#' @rdname write_defaults +#' @rdname write_fisdata_defaults #' #' @export -read_defaults <- function(file = "~/.fisdata.json", +read_fisdata_defaults <- function(file = "~/.fisdata.json", apply = TRUE, verbose = !apply || interactive()) { diff --git a/R/fisdata-package.R b/R/fisdata-package.R index 2f4ac0b..791a23a 100644 --- a/R/fisdata-package.R +++ b/R/fisdata-package.R @@ -37,7 +37,7 @@ cache <- new.env() # read defaults from it. if (interactive() && file.exists(defaults_file)) { cli::cli_alert_info("Reading default values from {defaults_file} ...") - read_defaults(defaults_file, verbose = TRUE) + read_fisdata_defaults(defaults_file, verbose = TRUE) } } diff --git a/man/write_defaults.Rd b/man/write_fisdata_defaults.Rd similarity index 85% rename from man/write_defaults.Rd rename to man/write_fisdata_defaults.Rd index 5ced9ad..029a006 100644 --- a/man/write_defaults.Rd +++ b/man/write_fisdata_defaults.Rd @@ -1,12 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/defaults.R -\name{write_defaults} -\alias{write_defaults} -\alias{write_current_defaults} -\alias{read_defaults} +\name{write_fisdata_defaults} +\alias{write_fisdata_defaults} +\alias{write_current_fisdata_defaults} +\alias{read_fisdata_defaults} \title{Read and Write Defaults from a JSON File} \usage{ -write_defaults( +write_fisdata_defaults( file = "~/.fisdata.json", overwrite = FALSE, sector = "", @@ -17,9 +17,9 @@ write_defaults( active_only = FALSE ) -write_current_defaults(file = "~/.fisdata.json", overwrite = FALSE) +write_current_fisdata_defaults(file = "~/.fisdata.json", overwrite = FALSE) -read_defaults( +read_fisdata_defaults( file = "~/.fisdata.json", apply = TRUE, verbose = !apply || interactive() @@ -62,8 +62,8 @@ is searched for in the description column of \link{disciplines}.} to \code{TRUE} in interactive sessions or when \code{apply} is \code{FALSE}.} } \value{ -\code{write_defaults()} and \code{write_current_defaults()} return the json-string that -was written to the file (invisibly). \code{read_defaults()} returns the default +\code{write_fisdata_defaults()} and \code{write_current_fisdata_defaults()} return the json-string that +was written to the file (invisibly). \code{read_fisdata_defaults()} returns the default values that were read as a tibble (invisibly). } \description{ diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index 18c8023..6d06b7f 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -32,6 +32,7 @@ reference: - show_url - browse_url - set_fisdata_defaults + - write_fisdata_defaults - title: Datasets desc: Datasets describing values that can be used in the querying functions diff --git a/tests/testthat/test-defaults.R b/tests/testthat/test-defaults.R index 683b7e1..0dbfd48 100644 --- a/tests/testthat/test-defaults.R +++ b/tests/testthat/test-defaults.R @@ -198,7 +198,7 @@ test_that("fd_def() works", { }) -test_that("write_current_defaults() works", { +test_that("write_current_fisdata_defaults() works", { reset_fisdata_defaults() set_fisdata_defaults(sector = "AL", gender = "F", category = "WC") ref <- toJSON( @@ -208,23 +208,23 @@ test_that("write_current_defaults() works", { pretty = TRUE ) local_file("fisdata.json") - expect_equal(write_current_defaults("fisdata.json"), ref) + expect_equal(write_current_fisdata_defaults("fisdata.json"), ref) expect_true(file.exists("fisdata.json")) expect_equal(paste(readLines("fisdata.json"), collapse = "\n"), ref, ignore_attr = TRUE) }) -test_that("write_current_defaults() handles existing file", { +test_that("write_current_fisdata_defaults() handles existing file", { local_file("fisdata.json") - write_current_defaults("fisdata.json") - expect_error(write_current_defaults("fisdata.json"), + write_current_fisdata_defaults("fisdata.json") + expect_error(write_current_fisdata_defaults("fisdata.json"), "The file fisdata.json exists.") - expect_silent(write_current_defaults("fisdata.json", overwrite = TRUE)) + expect_silent(write_current_fisdata_defaults("fisdata.json", overwrite = TRUE)) }) -test_that("write_defaults() works", { +test_that("write_fisdata_defaults() works", { ref <- toJSON( list(sector = "CC", season = "", gender = "M", category = "WC", discipline = "", active_only = FALSE), @@ -233,7 +233,7 @@ test_that("write_defaults() works", { ) local_file("fisdata.json") expect_equal( - write_defaults("fisdata.json", sector = "CC", gender = "M", category = "WC"), + write_fisdata_defaults("fisdata.json", sector = "CC", gender = "M", category = "WC"), ref ) expect_true(file.exists("fisdata.json")) @@ -242,31 +242,31 @@ test_that("write_defaults() works", { }) -test_that("write_defaults() handles existing file", { +test_that("write_fisdata_defaults() handles existing file", { local_file("fisdata.json") - write_defaults("fisdata.json", sector = "CC", gender = "M") - expect_error(write_defaults("fisdata.json", sector = "CC", gender = "M"), + write_fisdata_defaults("fisdata.json", sector = "CC", gender = "M") + expect_error(write_fisdata_defaults("fisdata.json", sector = "CC", gender = "M"), "The file fisdata.json exists.") - expect_silent(write_current_defaults("fisdata.json", overwrite = TRUE)) + expect_silent(write_current_fisdata_defaults("fisdata.json", overwrite = TRUE)) }) -test_that("write_defaults() rejects NULL as default", { +test_that("write_fisdata_defaults() rejects NULL as default", { local_file("fisdata.json") - expect_error(write_defaults("fisdata.json", sector = "CC", discipline = NULL), + expect_error(write_fisdata_defaults("fisdata.json", sector = "CC", discipline = NULL), "Defaults must no be NULL.*NULL: discipline") expect_false(file.exists("fisdata.json")) }) -test_that("read_defaults() reads defaults without applying them", { +test_that("read_fisdata_defaults() reads defaults without applying them", { local_file("fisdata.json") - write_defaults("fisdata.json", sector = "AL", season = "2024", gender = "W", + write_fisdata_defaults("fisdata.json", sector = "AL", season = "2024", gender = "W", category = "WC", discipline = "SL", active_only = TRUE) reset_fisdata_defaults() expect_equal( - read_defaults("fisdata.json", apply = FALSE, verbose = FALSE), + read_fisdata_defaults("fisdata.json", apply = FALSE, verbose = FALSE), tibble(sector = "AL", season = "2024", gender = "W", category = "WC", discipline = "SL", active_only = TRUE) ) @@ -278,14 +278,14 @@ test_that("read_defaults() reads defaults without applying them", { }) -test_that("read_defaults() applies defaults", { +test_that("read_fisdata_defaults() applies defaults", { local_file("fisdata.json") - write_defaults("fisdata.json", sector = "CC", season = "2025", gender = "M", + write_fisdata_defaults("fisdata.json", sector = "CC", season = "2025", gender = "M", category = "WC", discipline = "SP", active_only = TRUE) reset_fisdata_defaults() expect_equal( - read_defaults("fisdata.json", verbose = FALSE), + read_fisdata_defaults("fisdata.json", verbose = FALSE), tibble(sector = "CC", season = "2025", gender = "M", category = "WC", discipline = "SP", active_only = TRUE) ) @@ -297,13 +297,13 @@ test_that("read_defaults() applies defaults", { }) -test_that("read_defaults() handles invalid files", { +test_that("read_fisdata_defaults() handles invalid files", { local_file("fisdata.json") - expect_error(read_defaults("fisdata.json"), "does not exist") + expect_error(read_fisdata_defaults("fisdata.json"), "does not exist") writeLines("{", "fisdata.json") - expect_error(read_defaults("fisdata.json"), "Failed to parse") + expect_error(read_fisdata_defaults("fisdata.json"), "Failed to parse") writeLines( toJSON( @@ -313,7 +313,7 @@ test_that("read_defaults() handles invalid files", { ), "fisdata.json" ) - expect_error(read_defaults("fisdata.json"), "Some defaults have no value set: active_only") + expect_error(read_fisdata_defaults("fisdata.json"), "Some defaults have no value set: active_only") writeLines( toJSON( @@ -323,7 +323,7 @@ test_that("read_defaults() handles invalid files", { ), "fisdata.json" ) - expect_error(read_defaults("fisdata.json"), "contents.*not valid") + expect_error(read_fisdata_defaults("fisdata.json"), "contents.*not valid") writeLines( toJSON( @@ -333,21 +333,21 @@ test_that("read_defaults() handles invalid files", { ), "fisdata.json" ) - expect_error(read_defaults("fisdata.json"), "'1940' is not a valid season") + expect_error(read_fisdata_defaults("fisdata.json"), "'1940' is not a valid season") }) -test_that("read_defaults() creates output in the appropriate situations", { +test_that("read_fisdata_defaults() creates output in the appropriate situations", { local_file("fisdata.json") - write_defaults("fisdata.json", sector = "CC", season = "2025", gender = "M", + write_fisdata_defaults("fisdata.json", sector = "CC", season = "2025", gender = "M", category = "WC", discipline = "SP", active_only = TRUE) - expect_silent(read_defaults("fisdata.json", apply = FALSE, verbose = FALSE)) - expect_silent(read_defaults("fisdata.json", apply = TRUE, verbose = FALSE)) - read_defaults("fisdata.json", apply = FALSE, verbose = TRUE) %>% + expect_silent(read_fisdata_defaults("fisdata.json", apply = FALSE, verbose = FALSE)) + expect_silent(read_fisdata_defaults("fisdata.json", apply = TRUE, verbose = FALSE)) + read_fisdata_defaults("fisdata.json", apply = FALSE, verbose = TRUE) %>% expect_message("contains the following defaults") %>% expect_output("tibble.*sector +season +gender +category") - read_defaults("fisdata.json", apply = TRUE, verbose = TRUE) %>% + read_fisdata_defaults("fisdata.json", apply = TRUE, verbose = TRUE) %>% expect_message("sector.*CC.*Cross-Country") %>% expect_message("season.*2025") %>% expect_message("gender.*M") %>%