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

Add progress bars and test #57

Merged
merged 13 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .github/workflows/check-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ on:
pull_request:
branches: [main, master]
schedule:
- cron: '0 16 * * 1,6'
- cron: '27 17 * * 1,5'
workflow_dispatch:

name: R-CMD-check-examples

Expand All @@ -26,6 +27,7 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
AEMET_API_KEY: ${{ secrets.AEMET_API_KEY }}
AEMET_API_KEY01: ${{ secrets.AEMET_API_KEY_PKGDOWN }}
R_KEEP_PKG_SOURCE: yes

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rostemplate-gh-pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: windows-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
AEMET_API_KEY: ${{ secrets.AEMET_API_KEY }}
AEMET_API_KEY: ${{ secrets.AEMET_API_KEY_PKGDOWN }}
steps:
- uses: actions/checkout@v4

Expand Down
58 changes: 58 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage

jobs:
test-coverage:
runs-on: windows-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
AEMET_API_KEY: ${{ secrets.AEMET_API_KEY_CODECOV }}
AEMET_API_KEY01: ${{ secrets.AEMET_API_KEY }}
AEMET_API_KEY02: ${{ secrets.AEMET_API_KEY_PKGDOWN }}


steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
needs: coverage

- name: Test coverage
run: |
token <- Sys.getenv("CODECOV_TOKEN", "")
covr::codecov(
quiet = FALSE,
clean = FALSE,
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package"),
token = if (token != "") token
)
shell: Rscript {0}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package

1 change: 1 addition & 0 deletions .github/workflows/update-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
AEMET_API_KEY: ${{ secrets.AEMET_API_KEY }}
AEMET_API_KEY01: ${{ secrets.AEMET_API_KEY_PKGDOWN }}
steps:
- uses: actions/checkout@v4

Expand Down
27 changes: 24 additions & 3 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ references:
address: Vienna, Austria
year: '2024'
version: '>= 3.6.0'
- type: software
title: cli
abstract: 'cli: Helpers for Developing Command Line Interfaces'
notes: Imports
url: https://cli.r-lib.org
repository: https://CRAN.R-project.org/package=cli
authors:
- family-names: Csárdi
given-names: Gábor
email: [email protected]
year: '2024'
version: '>= 3.0.0'
- type: software
title: dplyr
abstract: 'dplyr: A Grammar of Data Manipulation'
Expand Down Expand Up @@ -180,9 +192,6 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0002-4035-0289
year: '2024'
identifiers:
- type: url
value: https://arxiv.org/abs/1403.2805
version: '>= 1.7.0'
- type: software
title: rappdirs
Expand Down Expand Up @@ -421,4 +430,16 @@ references:
orcid: https://orcid.org/0000-0001-8049-7069
year: '2024'
version: '>= 0.9.0'
- type: software
title: testthat
abstract: 'testthat: Unit Testing for R'
notes: Suggests
url: https://testthat.r-lib.org
repository: https://CRAN.R-project.org/package=testthat
authors:
- family-names: Wickham
given-names: Hadley
email: [email protected]
year: '2024'
version: '>= 3.0.0'

6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ BugReports: https://github.com/rOpenSpain/climaemet/issues
Depends:
R (>= 3.6.0)
Imports:
cli (>= 3.0.0),
dplyr (>= 1.0.0),
ggplot2 (>= 3.3.2),
httr2 (>= 1.0.0),
Expand All @@ -39,12 +40,15 @@ Suggests:
lubridate,
rmarkdown,
scales,
sf (>= 0.9.0)
sf (>= 0.9.0),
testthat (>= 3.0.0)
VignetteBuilder:
knitr
Config/Needs/website: mapSpain, classInt, terra, sf, gstat, geoR,
tidyverse, tidyterra, scales, gifski, reactable, leaflet, geofacet,
devtools, crosstalk, usethis
Config/testthat/edition: 3
Config/testthat/parallel: true
Copyright: © AEMET. See file COPYRIGHTS
Encoding: UTF-8
LazyData: true
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(aemet_monthly_period)
export(aemet_monthly_period_all)
export(aemet_normal_clim)
export(aemet_normal_clim_all)
export(aemet_show_api_key)
export(aemet_stations)
export(climaemet_news)
export(climatestripes_station)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# climaemet (development version)

- Migrate from **httr** to **httr2** (#50).
- Use progress bars in downloads thanks to **cli**. New argument
`progress = TRUE` in most functions.
- It is possible to use several API keys to avoid API throttling, see
`?climaemet::aemet_api_key` (#53). `?climaemet::aemet_api_key`.
`?climaemet::aemet_api_key` (#53).
- New helper function `dms2decdegrees_2()`.
- Update `aemet_munic` with January 2024 data.

Expand Down
44 changes: 29 additions & 15 deletions R/aemet_api_key.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@
#'
#' @description
#' This function will store your AEMET API key on your local machine so it can
#' be called securely without being stored in your code. After you have
#' installed your key, it can be called any time by typing
#' `Sys.getenv("AEMET_API_KEY")` and can be
#' used in package functions by simply typing `AEMET_API_KEY`.
#' be called securely without being stored in your code.
#'
#' Alternatively, you can install the API Key manually:
#' * Run `Sys.setenv(AEMET_API_KEY = "Your_Key")`. You would need to run this
#' command on each session (Similar to `install = FALSE`).
#' * Write this line on your .Renviron file: `AEMET_API_KEY = "Your_Key"` (
#' same behavior than `install = TRUE`). This would store your API key
#' permanently.
#' - Run `Sys.setenv(AEMET_API_KEY = "Your_Key")`. You would need to run this
#' command on each session (Similar to `install = FALSE`).
#' - Write this line on your .Renviron file: `AEMET_API_KEY = "Your_Key"`
#' (same behavior than `install = TRUE`). This would store your API key
#' permanently.
#'
#' @return None
#'
#' @param apikey The API key provided to you from the AEMET formatted in quotes.
#' A key can be acquired at
#' <https://opendata.aemet.es/centrodedescargas/inicio>.
#' <https://opendata.aemet.es/centrodedescargas/inicio>. You can install
#' several API Keys as a vector of characters, see **Details**.
#' @param install if `TRUE`, will install the key in your local machine for
#' use in future sessions. Defaults to `FALSE.`
#' @param overwrite If this is set to `TRUE`, it will overwrite an existing
#' AEMET_API_KEY that you already have in local machine.
#' `AEMET_API_KEY` that you already have in local machine.
#'
#' @details
#' You can pass several `apikey` values as a vector `c(api1, api2)`, in this
Expand Down Expand Up @@ -62,7 +60,7 @@ aemet_api_key <- function(apikey, overwrite = FALSE, install = FALSE) {
# Validate
stopifnot(is.character(apikey), is.logical(overwrite), is.logical(install))


apikey <- trimws(apikey)

if (install) {
cachedir <- rappdirs::user_cache_dir("climaemet", "R")
Expand Down Expand Up @@ -109,23 +107,30 @@ aemet_api_key <- function(apikey, overwrite = FALSE, install = FALSE) {
#'
#' @description
#' The function would detect if an API Key is available on this session:
#' * If an API Key is already set as an environment variable it would be
#' - If an API Key is already set as an environment variable it would be
#' preserved
#' * If no environment variable has been set and you have stored permanently
#' - If no environment variable has been set and you have stored permanently
#' an API Key using [aemet_api_key()], the latter would be loaded.
#'
#' @return `TRUE` or `FALSE`
#' @return
#' `TRUE` or `FALSE`. `aemet_show_api_key()` would display your stored API keys.
#'
#' @family aemet_auth
#'
#' @export
#'
#' @param ... Ignored
#'
#' @rdname aemet_detect_api_key
#'
#' @examples
#'
#' aemet_detect_api_key()
#'
#' # CAUTION: This may reveal API Keys
#' if (FALSE) {
#' aemet_show_api_key()
#' }
aemet_detect_api_key <- function(...) {
allvar <- Sys.getenv()

Expand Down Expand Up @@ -166,6 +171,15 @@ aemet_detect_api_key <- function(...) {
}
}

#' @export
#' @rdname aemet_detect_api_key
aemet_show_api_key <- function(...) {
# Expose internal function
# nocov start
aemet_hlp_get_allkeys(...)
# nocov end
}

aemet_hlp_get_allkeys <- function(...) {
allkeys <- Sys.getenv()[grepl("^AEMET_API", names(Sys.getenv()))]
allkeys <- unname(as.character(allkeys))
Expand Down
3 changes: 2 additions & 1 deletion R/aemet_api_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#'
#' @family aemet_api
#'
#' @source <https://opendata.aemet.es/dist/index.html>
#' @source
#' <https://opendata.aemet.es/dist/index.html>.
#'
#' @param apidest Character string as destination URL. See
#' <https://opendata.aemet.es/dist/index.html>.
Expand Down Expand Up @@ -55,7 +56,7 @@
#'
#' gganimate::gif_file(tmp)
#' @export
get_data_aemet <- function(apidest, verbose = FALSE) {

Check warning on line 59 in R/aemet_api_query.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/aemet_api_query.R,line=59,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 18.
# API Key management
apikey_detected <- aemet_detect_api_key()
if (isFALSE(apikey_detected)) {
Expand Down Expand Up @@ -153,7 +154,7 @@
#' @rdname get_data_aemet
#' @name get_data_aemet
#' @export
get_metadata_aemet <- function(apidest, verbose = FALSE) {

Check warning on line 157 in R/aemet_api_query.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/aemet_api_query.R,line=157,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 19.
# API Key management
apikey_detected <- aemet_detect_api_key()
if (isFALSE(apikey_detected)) {
Expand Down Expand Up @@ -270,7 +271,7 @@
#' - On fatal errors, an error as of [httr2::resp_check_status()].
#'
#' @noRd
aemet_api_call <- function(apidest, verbose = FALSE, data_call = FALSE,

Check warning on line 274 in R/aemet_api_query.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/aemet_api_query.R,line=274,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 25.
apikey = NULL) {
if (is.null(apikey)) stop("Provide an apikey argument")

Expand Down
Loading
Loading