-
-
Save matt-dray/7e55935166b5c01281fabb48d5e7db85 to your computer and use it in GitHub Desktop.
Scratch code while trying out Rapp for R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## cli.R | |
| #' Install 'bh' CLI application | |
| #' | |
| #' @inheritDotParams Rapp::install_pkg_cli_apps -package -lib.loc | |
| #' @export | |
| install_cli <- function(...) { | |
| Rapp::install_pkg_cli_apps(package = "bh", lib.loc = NULL, ...) | |
| } | |
| ## cli.R | |
| #!/usr/bin/env Rapp | |
| #| name: bh | |
| #| description: Get next bank holidays | |
| #| description: country | |
| #| short: c | |
| country <- "england-and-wales" | |
| #| description: include one after next? | |
| then <- TRUE | |
| cat(create_msg(country, then)) | |
| ## api.R | |
| get_holidays <- function(url = "https://www.gov.uk/bank-holidays.json") { | |
| req <- httr2::request(url) | |
| resp <- httr2::req_perform(req) | |
| resp |> httr2::resp_body_json() | |
| } | |
| wrangle_holidays <- function(holidays, country) { | |
| today <- lubridate::today() | |
| holidays |> | |
| purrr::pluck(country, "events") |> | |
| tibble::enframe() |> | |
| tidyr::unnest_wider(value) |> | |
| dplyr::mutate(date = lubridate::as_date(date)) |> | |
| dplyr::filter(date >= lubridate::today()) |> | |
| dplyr::mutate( | |
| bunting = dplyr::if_else(bunting, "▼▽▼▽▼▽", ""), | |
| string = glue::glue("{title} ({date}) {bunting}") | |
| ) | |
| } | |
| #' Create Printable Message | |
| #' @export | |
| create_msg <- function(country = "england-and-wales", then = TRUE) { | |
| df <- get_holidays() |> wrangle_holidays(country) | |
| strings <- df |> dplyr::pull(string) | |
| msg <- glue::glue("Next: {strings[1]}\n") | |
| if (then) { | |
| msg <- glue::glue("{msg}\nThen: {strings[2]}\n") | |
| } | |
| msg | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment