From 0a3b3cbb2908944d5dca0c43e270fa3f2cd5b849 Mon Sep 17 00:00:00 2001 From: almac2022 Date: Thu, 27 Jun 2024 13:04:26 -0700 Subject: [PATCH] make rayshader, elevatr and fasstr suggests to the functions they are used in, adding checks within the function sto tell the user to install it if they don't have and remove from depends in Description to address #7 --- DESCRIPTION | 6 +++--- NAMESPACE | 1 - R/fpr_create_hydrograph.R | 9 ++++++++- R/spatial.R | 23 +++++++++++++++++++---- man/fpr_make_geopackage.Rd | 2 +- man/fpr_make_html_tbl.Rd | 5 +++++ man/fpr_sp_watershed.Rd | 4 ++-- man/fpr_sp_wshd_stats.Rd | 16 ++++++++++++++-- 8 files changed, 52 insertions(+), 14 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 04e4f58..f5aed04 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,9 +28,7 @@ Imports: tidyselect, fwapgr, data.table, - elevatr, raster, - rayshader, readr, stats, generics, @@ -45,7 +43,6 @@ Imports: glue, RPostgres, rlang, - fasstr, ggdark, ggplot2, tidyhydat, @@ -53,5 +50,8 @@ Imports: Remotes: poissonconsulting/fwapgr, Suggests: + rayshader, + elevatr, + fasstr, testthat (>= 3.0.0) Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 399a392..7cd1072 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -98,7 +98,6 @@ importFrom(dplyr,rowwise) importFrom(dplyr,select) importFrom(dplyr,summarise) importFrom(dplyr,ungroup) -importFrom(fasstr,plot_data_screening) importFrom(ggdark,dark_theme_bw) importFrom(ggplot2,geom_line) importFrom(ggplot2,geom_ribbon) diff --git a/R/fpr_create_hydrograph.R b/R/fpr_create_hydrograph.R index 6f4be76..0967a62 100644 --- a/R/fpr_create_hydrograph.R +++ b/R/fpr_create_hydrograph.R @@ -11,7 +11,6 @@ #' @importFrom lubridate year #' @importFrom tidyhydat hy_daily_flows search_stn_number #' @importFrom stringr str_to_title -#' @importFrom fasstr plot_data_screening #' @importFrom ggdark dark_theme_bw #' @importFrom dplyr mutate group_by summarise #' @importFrom ggplot2 ggsave ggplot geom_ribbon scale_x_date labs geom_line scale_colour_manual @@ -29,6 +28,14 @@ fpr_create_hydrograph <- function( start_year = NULL, end_year = NULL){ + required_packages <- c("fasstr") + + for (package in required_packages) { + if (!require(package, character.only = TRUE)) { + cli::abort(paste("Package", package, "is not installed. Please install it using install.packages(\"", package, "\").", sep = "")) + } + } + if(is.null(station)){ cli::cli_abort('Please provide a station number, for example "08EE004"') } diff --git a/R/spatial.R b/R/spatial.R index ca032e4..af8e204 100644 --- a/R/spatial.R +++ b/R/spatial.R @@ -8,6 +8,7 @@ #' in the fish_passage_skeena_2021_reporting repo for now. #' #' @param dat Dataframe or tibble which usually only has one row +#' @family spatial #' #' @return html object #' @export @@ -27,7 +28,7 @@ fpr_make_html_tbl <- function(dat) { } -#' Retrieve watershed geometry using fwapg +#' Retrieve watershed geometry using fwapgr #' #' @param dat Dataframe with a stream_crossing_id, blue_line_key and downstream_route_measure defined. Ususally pulled from bcfishpass #' @@ -47,15 +48,29 @@ fpr_sp_watershed <- function(dat){ sf::st_as_sf() } -#' Get elevation statistics +#' Get elevation statistics. #' #' @param dat sf object of polygons to gather elevation statistics for. Must contain `stream_crossing_id` column. #' #' @return sf object of polygons with elevation details (ie. p60, median, min, max) and median aspect information added. +#' @family spatial #' @export #' -#' @examples +#' @examples \dontrun{wshd <- fpr_sp_wshd_stats( +#' fwapgr::fwa_watershed_at_measure( +#' blue_line_key = 360746095, downstream_route_measure = 100) |> +#' dplyr::mutate(stream_crossing_id = 1) +#' )} fpr_sp_wshd_stats <- function(dat){ + + required_packages <- c("elevatr", "rayshader") + + for (package in required_packages) { + if (!require(package, character.only = TRUE)) { + cli::abort(paste("Package", package, "is not installed. Please install it using install.packages(\"", package, "\").", sep = "")) + } + } + directions <- tibble::tibble( degree_centre = seq(0, 360 , by=22.5), cardinal = c("N","NNE","NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N")) %>% @@ -106,7 +121,7 @@ fpr_sp_wshd_stats <- function(dat){ arrange(stream_crossing_id) } -#' Make geopackage in directory called +#' Make geopackage from an sf object. Create directory to burn it to if it doesn't exist. #' #' Names the layer the same as the name of the dataframe that is burned #' diff --git a/man/fpr_make_geopackage.Rd b/man/fpr_make_geopackage.Rd index b925fb6..5d1ae42 100644 --- a/man/fpr_make_geopackage.Rd +++ b/man/fpr_make_geopackage.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/spatial.R \name{fpr_make_geopackage} \alias{fpr_make_geopackage} -\title{Make geopackage in directory called} +\title{Make geopackage from an sf object. Create directory to burn it to if it doesn't exist.} \usage{ fpr_make_geopackage( dat, diff --git a/man/fpr_make_html_tbl.Rd b/man/fpr_make_html_tbl.Rd index 390500c..00ddb7c 100644 --- a/man/fpr_make_html_tbl.Rd +++ b/man/fpr_make_html_tbl.Rd @@ -17,3 +17,8 @@ Usually just a one row tibble is transformed as this is helper function for making kml files from PSCIS spreadsheets. Other functions for the kml are in the 0110-make-kml.R file in the fish_passage_skeena_2021_reporting repo for now. } +\seealso{ +Other spatial: +\code{\link{fpr_sp_wshd_stats}()} +} +\concept{spatial} diff --git a/man/fpr_sp_watershed.Rd b/man/fpr_sp_watershed.Rd index 25e4549..a68a405 100644 --- a/man/fpr_sp_watershed.Rd +++ b/man/fpr_sp_watershed.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/spatial.R \name{fpr_sp_watershed} \alias{fpr_sp_watershed} -\title{Retrieve watershed geometry using fwapg} +\title{Retrieve watershed geometry using fwapgr} \usage{ fpr_sp_watershed(dat) } @@ -13,5 +13,5 @@ fpr_sp_watershed(dat) sf object representing watershed areas } \description{ -Retrieve watershed geometry using fwapg +Retrieve watershed geometry using fwapgr } diff --git a/man/fpr_sp_wshd_stats.Rd b/man/fpr_sp_wshd_stats.Rd index d146be5..ebc8784 100644 --- a/man/fpr_sp_wshd_stats.Rd +++ b/man/fpr_sp_wshd_stats.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/spatial.R \name{fpr_sp_wshd_stats} \alias{fpr_sp_wshd_stats} -\title{Get elevation statistics} +\title{Get elevation statistics.} \usage{ fpr_sp_wshd_stats(dat) } @@ -13,5 +13,17 @@ fpr_sp_wshd_stats(dat) sf object of polygons with elevation details (ie. p60, median, min, max) and median aspect information added. } \description{ -Get elevation statistics +Get elevation statistics. } +\examples{ +\dontrun{wshd <- fpr_sp_wshd_stats( +fwapgr::fwa_watershed_at_measure( +blue_line_key = 360746095, downstream_route_measure = 100) |> +dplyr::mutate(stream_crossing_id = 1) +)} +} +\seealso{ +Other spatial: +\code{\link{fpr_make_html_tbl}()} +} +\concept{spatial}