-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
* skeleton of function and tests * hush R version warning and use_tidy_description * add download_dataset and toggle_message * up the cyclocomp limit in lintr * Move from a response type to a separate get-csv endpoint * style code * Correct test name * fix typo in invalid endpoint error message * re-document (got lost in conflicts)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
linters: linters_with_defaults( | ||
line_length_linter = line_length_linter(100L) | ||
line_length_linter = line_length_linter(100L), | ||
cyclocomp_linter = cyclocomp_linter(25L) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,30 @@ | ||
Package: eesyapi | ||
Title: EES-y API | ||
Version: 0.3.0 | ||
Authors@R: | ||
c( | ||
Authors@R: c( | ||
person("Rich", "Bielby", , "[email protected]", role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0001-9070-9969")), | ||
person("Cam", "Race", , "[email protected]", role = "aut"), | ||
person("Laura", "Selby", , "[email protected]", role = "ctb")) | ||
Description: An R package with useful utility functions for connecting to, and | ||
processing data from, the DfE's explore education statistics API. | ||
person("Laura", "Selby", , "[email protected]", role = "ctb") | ||
) | ||
Description: An R package with useful utility functions for connecting to, | ||
and processing data from, the DfE's explore education statistics API. | ||
License: MIT + file LICENSE | ||
Encoding: UTF-8 | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.3.2 | ||
URL: https://dfe-analytical-services.github.io/eesyapi | ||
Suggests: | ||
testthat (>= 3.0.0) | ||
Config/testthat/edition: 3 | ||
Depends: | ||
R (>= 3.5.0) | ||
Imports: | ||
data.table, | ||
dplyr, | ||
httr, | ||
jsonlite, | ||
dplyr, | ||
stringr, | ||
data.table, | ||
magrittr, | ||
rlang | ||
rlang, | ||
stringr | ||
Suggests: | ||
readr, | ||
testthat (>= 3.0.0) | ||
Config/testthat/edition: 3 | ||
Encoding: UTF-8 | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.3.2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#' Download the raw CSV for an API data set | ||
#' | ||
#' This gives a super quick way to just fetch the whole file in a human | ||
#' readable format. | ||
#' | ||
#' @description | ||
#' This function is mostly designed for exploring the API, and is unlikely to | ||
#' be suitable for long term production use. | ||
#' | ||
#' There is no filtering down of the file so you will always get the whole file | ||
#' and in some instances this may be very large. | ||
#' | ||
#' As there are no IDs involved, this is brittle and code relying on this | ||
#' function will likely break whenever there is renaming of variables or items | ||
#' in the data. | ||
#' | ||
#' It is recommended to take the time to set up custom queries using the | ||
#' `query_dataset()` function instead. If you are using this function for more | ||
#' than exploratory purposes, make sure you subscribe to the data set you're | ||
#' downloading and then keep track of any updates to the data. | ||
#' | ||
#' @param dataset_id ID of data set | ||
#' @param dataset_version Version number of data set | ||
#' @param api_version EES API version | ||
#' @param verbose Run with additional contextual messaging, logical, default = FALSE | ||
#' | ||
#' @return data.frame | ||
#' @export | ||
#' | ||
#' @examples | ||
#' download_dataset(example_id("dataset")) | ||
download_dataset <- function( | ||
dataset_id, | ||
dataset_version = NULL, | ||
api_version = NULL, | ||
verbose = FALSE) { | ||
# Validation ---------------------------------------------------------------- | ||
if (!is.null(dataset_version)) { | ||
warning( | ||
paste( | ||
"Support for dataset_version is not yet available for downloading", | ||
"full data sets. Returning latest available version of data set." | ||
) | ||
) | ||
} | ||
|
||
if (!is.logical(verbose)) { | ||
stop("verbose must be a logical value, either TRUE or FALSE") | ||
} | ||
|
||
eesyapi::validate_ees_id(dataset_id, level = "dataset") | ||
|
||
# Generate query ------------------------------------------------------------ | ||
query_url <- eesyapi::api_url( | ||
endpoint = "get-csv", | ||
dataset_id = dataset_id, | ||
verbose = verbose | ||
) | ||
|
||
toggle_message("Requesting data...", verbose = verbose) | ||
|
||
response <- httr::GET(query_url) | ||
|
||
eesyapi::http_request_error(response, verbose = verbose) | ||
|
||
toggle_message("Parsing response...", verbose = verbose) | ||
|
||
# Parse into data.frame ----------------------------------------------------- | ||
output <- httr::content( | ||
response, | ||
|
||
# All EES CSVs should be UTF-8 and are validated on import | ||
encoding = "UTF-8", | ||
|
||
# httr uses read_csv() underneath, controlling read_csv() verbosity | ||
show_col_types = verbose, | ||
progress = verbose | ||
) |> | ||
as.data.frame() | ||
|
||
return(output) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#' Controllable console messages | ||
#' | ||
#' Quick expansion to the `message()` function aimed for use in functions for | ||
#' an easy addition of a global verbose TRUE / FALSE argument to toggle the | ||
#' messages on or off | ||
#' | ||
#' @param ... any message you would normally pass into `message()`. See | ||
#' \code{\link{message}} for more details | ||
#' | ||
#' @param verbose logical, usually a variable passed from the function you are | ||
#' using this within | ||
#' | ||
#' @keywords internal | ||
toggle_message <- function(..., verbose) { | ||
if (verbose) { | ||
message(...) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.