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

misc fixes #51

Merged
merged 3 commits into from
Nov 2, 2023
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
6 changes: 5 additions & 1 deletion .github/workflows/R-CMD-check-macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ jobs:
install.packages("sf")
install.packages("terra")
install.packages("lwgeom")
install.packages("rgdal")
remotes::install_github("dickoa/prepr")
remotes::install_github("r-spatial/link2GI")
shell: Rscript {0}
Expand All @@ -83,6 +82,11 @@ jobs:
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}

- name: Set R environmental variables
run: |
echo GGMAP_STADIAMAPS_API_KEY="$GGMAP_KEY" >> ~/.Renviron
shell: bash

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/R-CMD-check-ubuntu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
_R_CHECK_TIMINGS_: 10
_R_CHECK_CRAN_INCOMING_REMOTE_: false
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
GGMAP_KEY: ${{ secrets.GGMAP_STADIAMAPS_API_KEY }}
CI: true

steps:
Expand Down Expand Up @@ -76,7 +77,6 @@ jobs:
- name: Install spatial R packages
run: |
options(repos = "https://cloud.r-project.org/")
install.packages("rgdal")
install.packages("sf")
install.packages("terra")
install.packages("lwgeom")
Expand All @@ -93,6 +93,11 @@ jobs:
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}

- name: Set R environmental variables
run: |
echo GGMAP_STADIAMAPS_API_KEY="$GGMAP_KEY" >> ~/.Renviron
shell: bash

- name: Environmental variables
if: matrix.config.pkgs == 'depends only'
run: |
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/R-CMD-check-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ jobs:
install.packages("sf")
install.packages("terra")
install.packages("lwgeom")
install.packages("rgdal")
remotes::install_github("dickoa/prepr")
shell: Rscript {0}

Expand All @@ -80,6 +79,11 @@ jobs:
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}

- name: Set R environmental variables
run: |
echo GGMAP_STADIAMAPS_API_KEY="$GGMAP_KEY" >> ~/.Renviron
shell: bash

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ jobs:
- name: Install spatial R packages
run: |
options(repos = "https://cloud.r-project.org/")
install.packages("rgdal")
install.packages("sf")
install.packages("terra")
install.packages("lwgeom")
Expand Down
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: aoh
Type: Package
Version: 0.0.2.8
Version: 0.0.2.9
Title: Create Area of Habitat Data
Description: Create Area of Habitat data to characterize species distributions.
Data are produced following procedures outlined by Brooks et al. (2019)
Expand Down Expand Up @@ -42,19 +42,19 @@ Imports:
ggplot2 (>= 3.1.0),
fasterize (>= 1.0.3),
raster (>= 3.5-15),
rvest (>= 1.0.3)
rvest (>= 1.0.3),
jsonlite (>= 1.8.7)
Suggests:
sp (>= 2.0.0),
testthat (>= 2.0.1),
knitr (>= 1.2.0),
roxygen2 (>= 6.1.1),
rmarkdown (>= 1.10),
pingr (>= 1.1.2),
ggmap (>= 2.6.1),
ggmap (>= 3.0.2.9002),
fields (>= 14.0),
smoothr (>= 0.2.2),
rnaturalearth (>= 0.3.3),
rgdal (>= 1.5.27),
gdalUtilities (>= 1.2.1),
archive (>= 1.1.2),
link2GI (>= 0.5-0),
Expand Down Expand Up @@ -132,4 +132,5 @@ Collate:
Roxygen: list(markdown = TRUE)
Remotes:
dickoa/prepr,
r-spatial/link2GI
r-spatial/link2GI,
dkahle/ggmap
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# aoh 0.0.2.9

- Remove _rgdal_ package dependency.
- Updates URLs.
- Update functions for downloading preprocessed data from Zenodo.
- Update `plot_spp_aoh_data()` and `plot_spp_frc_data()` to be compatible
with changes to the _ggmap_ package for creating maps with a basemap.

# aoh 0.0.2.8

- Fix failing tests on continuous integration services (due to issues unzipping
Expand Down
87 changes: 32 additions & 55 deletions R/misc_zenodo.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,14 @@ get_doi_files <- function(x) {
# scrape html file
d <- rvest::read_html(x)

# find file container
file_div <- rvest::html_elements(d, css = ".files-box")

# find table in file container
file_table <- rvest::html_element(file_div, "table")
file_rows <- rvest::html_children(rvest::html_element(file_table, "tbody"))
file_info <- rvest::html_element(file_rows, "a")
# extract metedata
md <- rvest::html_elements(d, css = "#recordVersions")
md <- jsonlite::fromJSON(rvest::html_attr(md, "data-record"))

# extract file details
file_names <- rvest::html_text(file_info)
file_urls <- paste0(
"https://zenodo.org", rvest::html_attr(file_info, "href")
)
file_names <- names(md$files$entries)
file_urls <- paste0(md$links$files, "/", file_names, "?download=1")
file_urls <- gsub("/api/", "/", file_urls, fixed = TRUE)

# return result
tibble::tibble(filename = file_names, download = file_urls)
Expand Down Expand Up @@ -83,53 +78,35 @@ get_doi_versions <- function(x) {
d <- rvest::read_html(x)

# find metadata containers
metadata_divs <- rvest::html_elements(d, css = ".metadata")

# extract div containing version numbers
is_version_div <- vapply(metadata_divs, FUN.VALUE = logical(1), function(x) {
h <- rvest::html_elements(x, css = "h4")
if (length(h) == 0) return(FALSE)
h <- h[[1]]
identical(rvest::html_text(h), "Versions")
md <- rvest::html_elements(d, css = "#recordVersions")
md <- jsonlite::fromJSON(rvest::html_attr(md, "data-record"))
md <- rvest::read_html(md$links$versions)
md <- jsonlite::fromJSON(rvest::html_text(md), "p")$hits[[1]]

# extract metadata
version <- vapply(md, FUN.VALUE = character(1), function(x) {
if (is.null(x$metadata)) return(NA_character_)
if (is.null(x$metadata$version)) return(NA_character_)
trimws(x$metadata$version)
})

# return input doi if it's not associated with any versions
if (!any(is_version_div)) {
d <- tibble::tibble(
version = NA_character_,
created = as.POSIXct(NA_real_),
doi = gsub("https://doi.org/", "", x, fixed = TRUE)
)
return(d)
}

# extract div containing version numbers
version_div <- metadata_divs[[which(is_version_div)[[1]]]]

# extract version table
version_table <- rvest::html_element(version_div, "table")
version_rows <- rvest::html_children(version_table)

# parse information for each version
info <- lapply(version_rows, function(x) {
tibble::tibble(
version = trimws(gsub(
"Version ", "", fixed = TRUE,
rvest::html_text(rvest::html_element(x, "a"))
)),
created = as.POSIXct(
trimws(rvest::html_text(
rvest::html_element(rvest::html_children(x)[[2]], "small")
)),
format = "%b %e, %Y"
),
doi = trimws(rvest::html_text(rvest::html_element(x, "small"))),
)
created <- vapply(md, FUN.VALUE = character(1), function(x) {
if (is.null(x$metadata)) return(NA_character_)
if (is.null(x$metadata$publication_date)) return(NA_character_)
trimws(x$metadata$publication_date)
})
doi <- vapply(md, FUN.VALUE = character(1), function(x) {
if (is.null(x$metadata)) return(NA_character_)
if (is.null(x$metadata$doi)) return(NA_character_)
trimws(x$metadata$doi)
})

# compile table
info <- dplyr::bind_rows(info)
# create table with metadata
info <- tibble::tibble(
version = version,
created = as.POSIXct(created, format = "%Y-%m-%d"),
doi = doi
)

## return result (reverse row ordering for compatibility with zen4R
# return result (reverse row ordering for compatibility with zen4R
info[rev(seq_len(nrow(info))), , drop = FALSE]
}
19 changes: 13 additions & 6 deletions R/plot_spp_aoh_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ NULL
#' spatial extent of the data.
#'
#' @param zoom `numeric` Value indicating the zoom level for the basemap.
#' See documentation for the `zoom` parameter in the [ggmap::get_stamenmap()]
#' See documentation for the `zoom` parameter in the [ggmap::get_stadiamap()]
#' function for details.
#' Defaults to `NULL` such that no basemap is shown.
#'
#' @param maptype `character` Value indicating the name of the
#' the basemap to use for the plot.
#' See documentation for the `maptype` parameter in the
#' [ggmap::get_stamenmap()]
#' [ggmap::get_stadiamap()]
#' function for details.
#' Defaults to `NULL` such that no basemap is shown.
#' Note that the \pkg{ggmap} package must be installed to show a basemap.
#'
#' @param maxcell `integer` Maximum number of grid cells for mapping.
#' Defaults to 50000.
#'
#' @param ... Additional arguments passed to [ggmap::get_stamenmap()].
#' @param ... Additional arguments passed to [ggmap::get_stadiamap()].
#'
#' @details
#' Note that data are automatically projected to a
Expand Down Expand Up @@ -110,7 +110,7 @@ NULL
#' if (require(ggmap)) {
#' ## create customized map with basemap
#' p3 <-
#' plot_spp_aoh_data(spp_aoh_data, zoom = 7, maptype = "toner") +
#' plot_spp_aoh_data(spp_aoh_data, zoom = 7, maptype = "stamen_toner") +
#' scale_fill_manual(
#' values = c("suitable" = "blue", "not suitable" = "transparent")
#' ) +
Expand Down Expand Up @@ -310,11 +310,18 @@ plot_spp_data <- function(x, max_plot = 9, expand = 0.05,
#'
#' @param x [sf::st_sf()] object.
#'
#' @return A [ggmap::get_stamenmap()] object.
#' @return A [ggmap::get_stadiamap()] object.
#'
#' @noRd
get_ggmap_basemap <- function(x, expand = 0.05, ...) {
assertthat::assert_that(sf::st_crs(x) == sf::st_crs(4326))
assertthat::assert_that(
nzchar(Sys.getenv("GGMAP_STADIAMAPS_API_KEY")),
msg = paste(
"StadiaMaps API key must be registered to plot a basemap,",
"see `?ggmap::register_stadiamaps` for details."
)
)
bb <- as.list(sf::st_bbox(x))
bb2 <- bb
if (expand > 0) {
Expand All @@ -325,5 +332,5 @@ get_ggmap_basemap <- function(x, expand = 0.05, ...) {
bb2[["ymin"]] <- bb[["ymin"]] - yf
bb2[["ymax"]] <- bb[["ymax"]] + yf
}
ggmap::get_stamenmap(unname(unlist(bb2)), ...)
ggmap::get_stadiamap(unname(unlist(bb2)), ...)
}
2 changes: 1 addition & 1 deletion R/plot_spp_frc_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ NULL
#' if (require(ggmap)) {
#' ## create customized map with basemap
#' p3 <-
#' plot_spp_frc_data(spp_frc_data, zoom = 7, maptype = "toner") +
#' plot_spp_frc_data(spp_frc_data, zoom = 7, maptype = "stamen_toner") +
#' scale_fill_viridis_c() +
#' scale_color_manual(values = c("range" = "red")) +
#' scale_size_manual(values = c("range" = 1.5)) +
Expand Down
6 changes: 5 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ Finally, let's create some maps to compare the range data with the Area of habit
# create maps
## N.B. you might need to install the ggmap package
map <-
plot_spp_aoh_data(spp_aoh_data, zoom = 6, maptype = "toner-background") +
plot_spp_aoh_data(
spp_aoh_data,
zoom = 6,
maptype = "stamen_toner_background"
) +
scale_fill_viridis_d() +
scale_color_manual(values = c("range" = "red")) +
scale_size_manual(values = c("range" = 0.5)) +
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ of habitat data.
# create maps
## N.B. you might need to install the ggmap package
map <-
plot_spp_aoh_data(spp_aoh_data, zoom = 6, maptype = "toner-background") +
plot_spp_aoh_data(
spp_aoh_data,
zoom = 6,
maptype = "stamen_toner_background"
) +
scale_fill_viridis_d() +
scale_color_manual(values = c("range" = "red")) +
scale_size_manual(values = c("range" = 0.5)) +
Expand All @@ -345,7 +349,11 @@ map <-
strip.text = element_text(color = "white"),
strip.background = element_rect(fill = "black", color = "black")
)
```

## |---------|---------|---------|---------|========================================= |---------|---------|---------|---------|=========================================

``` r
# display maps
print(map)
```
Expand All @@ -362,7 +370,7 @@ produce Area of Habitat data.
relevant data using:

Hanson JO (2023) aoh: Create Area of Habitat Data. R package version
0.0.2.8. Available at https://github.com/prioritizr/aoh.
0.0.2.9. Available at https://github.com/prioritizr/aoh.

IUCN [insert year] IUCN Red List of Threatened Species. Version
[insert version]. Available at www.iucnredlist.org.
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

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

Loading
Loading