From d4be8ba497356acb7c2ca554d2ba2b4c42743c02 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Thu, 12 May 2022 16:50:53 +0200 Subject: [PATCH 01/32] Apply #84 to indicator_introdcution_year() --- NAMESPACE | 2 +- R/indicator_introduction_year.R | 117 +++++++++++++------------- man/indicator_introduction_year.Rd | 10 ++- man/visualize_pathways_year_level1.Rd | 2 +- man/visualize_pathways_year_level2.Rd | 2 +- 5 files changed, 70 insertions(+), 63 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 0a5770e1..41c9da7b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ importFrom(assertable,assert_colnames) importFrom(assertthat,assert_that) importFrom(assertthat,is.date) importFrom(dplyr,"%>%") +importFrom(dplyr,"ggplot2::geom_smooth") importFrom(dplyr,.data) importFrom(dplyr,add_row) importFrom(dplyr,add_tally) @@ -76,7 +77,6 @@ importFrom(ggplot2,geom_bar) importFrom(ggplot2,geom_line) importFrom(ggplot2,geom_point) importFrom(ggplot2,geom_ribbon) -importFrom(ggplot2,geom_smooth) importFrom(ggplot2,ggplot) importFrom(ggplot2,ggsave) importFrom(ggplot2,ggtitle) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index d8c9d9cc..49c4ced0 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -6,7 +6,7 @@ #' @param start_year_plot Year where the plot starts from. Default: 1920. #' @param smooth_span (numeric) Parameter for the applied #' \code{\link[stats]{loess}} smoother. For more information on the -#' appropriate value, see \code{\link[ggplot2]{geom_smooth}}. Default: 0.85. +#' appropriate value, see \code{\link[ggplot2]{ggplot2::geom_smooth}}. Default: 0.85. #' @param x_major_scale_stepsize (integer) Parameter that indicates the breaks #' of the x axis. Default: 10. #' @param x_minor_scale_stepsize (integer) Parameter that indicates the minor @@ -24,17 +24,16 @@ #' @param x_lab NULL or character. to set or remove the x-axis label. #' @param y_lab NULL or character. to set or remove the y-axis label. #' -#' @return A ggplot2 object (or egg object if facets are used). +#' @return A list with two slots: +#' - `plot`: ggplot2 object (or egg object if facets are used). +#' - `data_top_graph`: data.frame (tibble) with data used for the main plot (top graph) in `plot`. +#' - `data_facet_graph`: data.frame (tibble) with data used for the faceting +#' plot in `plot`. If `facet_column` is NULL, NULL is returned. #' #' @export -#' @importFrom assertthat assert_that -#' @importFrom assertable assert_colnames -#' @importFrom dplyr %>% filter group_by count ungroup rename_at -#' distinct .data syms -#' @importFrom ggplot2 coord_cartesian geom_point aes xlab ylab scale_x_continuous facet_wrap -#' geom_smooth +#' @importFrom dplyr %>% +#' ggplot2::geom_smooth #' @importFrom rlang !!! -#' @importFrom egg ggarrange #' #' @examples #' \dontrun{ @@ -82,36 +81,36 @@ indicator_introduction_year <- function(df, x_lab = "Year", y_lab = "Number of introduced alien species") { # initial input checks - assert_that(is.data.frame(df)) - assert_that(is.numeric(start_year_plot), + assertthat::assert_that(is.data.frame(df)) + assertthat::assert_that(is.numeric(start_year_plot), msg = "Argument start_year_plot has to be a number." ) - assert_that(start_year_plot < as.integer(format(Sys.Date(), "%Y")), + assertthat::assert_that(start_year_plot < as.integer(format(Sys.Date(), "%Y")), msg = paste( "Argument start_year_plot has to be less than", format(Sys.Date(), "%Y") ) ) - assert_that(is.numeric(smooth_span), + assertthat::assert_that(is.numeric(smooth_span), msg = "Argument smooth_span has to be a number between 0 and 1." ) - assert_that(is.numeric(x_major_scale_stepsize), + assertthat::assert_that(is.numeric(x_major_scale_stepsize), msg = "Argument x_major_scale_stepsize has to be a number." ) - assert_that(is.numeric(x_minor_scale_stepsize), + assertthat::assert_that(is.numeric(x_minor_scale_stepsize), msg = "Argument x_minor_scale_stepsize has to be a number." ) - assert_that(x_major_scale_stepsize >= x_minor_scale_stepsize, + assertthat::assert_that(x_major_scale_stepsize >= x_minor_scale_stepsize, msg = paste0( "x_major_scale_stepsize has to be greater ", "than x_minor_scale_stepsize./n" ) ) - assert_that(is.null(facet_column) | is.character(facet_column), + assertthat::assert_that(is.null(facet_column) | is.character(facet_column), msg = "Argument facet_column has to be NULL or a character." ) if (is.character(facet_column)) { - assert_colnames(df, facet_column, only_colnames = FALSE) + assertable::assert_colnames(df, facet_column, only_colnames = FALSE) } # check for valid facet options valid_facet_options <- c( @@ -123,31 +122,31 @@ indicator_introduction_year <- function(df, facet_column <- match.arg(facet_column, valid_facet_options) } - assert_that(is.character(taxon_key_col), + assertthat::assert_that(is.character(taxon_key_col), msg = "Argument taxon_key_col has to be a character." ) - assert_colnames(df, taxon_key_col, only_colnames = FALSE) - assert_that(is.character(first_observed), + assertable::assert_colnames(df, taxon_key_col, only_colnames = FALSE) + assertthat::assert_that(is.character(first_observed), msg = "Argument first_observed has to be a character." ) - assert_colnames(df, first_observed, only_colnames = FALSE) - assert_that(is.character(x_lab), + assertable::assert_colnames(df, first_observed, only_colnames = FALSE) + assertthat::assert_that(is.character(x_lab), msg = "Argument x_lab has to be a character or NULL." ) - assert_that(is.character(y_lab), + assertthat::assert_that(is.character(y_lab), msg = "Argument y_lab has to be a character or NULL." ) # Rename to default column name df <- df %>% - rename_at(vars(first_observed), ~"first_observed") %>% - rename_at(vars(taxon_key_col), ~"key") + dplyr::rename_at(vars(first_observed), ~"first_observed") %>% + dplyr::rename_at(vars(taxon_key_col), ~"key") # Provide warning messages for first_observed NA values n_first_observed_not_present <- df %>% - filter(is.na(.data$first_observed)) %>% + dplyr::filter(is.na(.data$first_observed)) %>% nrow() if (n_first_observed_not_present) { warning(paste0( @@ -155,43 +154,43 @@ indicator_introduction_year <- function(df, " records have no information about year of introduction ", "(empty values in column ", first_observed, - ") and are not taken into account.\n" + ") and are not taken into acdplyr::count.\n" )) } - # Filter the incoming data + # filter the incoming data data <- df %>% - filter(.data$first_observed > start_year_plot) + dplyr::filter(.data$first_observed > start_year_plot) - # Distinct values in columns of interest + # dplyr::distinct values in columns of interest if (is.null(facet_column)) { data <- data %>% - distinct(.data$key, .data$first_observed) + dplyr::distinct(.data$key, .data$first_observed) } else { data <- data %>% - distinct(.data$key, .data$first_observed, .data[[facet_column]]) + dplyr::distinct(.data$key, .data$first_observed, .data[[facet_column]]) } - + data_top_graph <- data %>% - group_by(.data$first_observed) %>% - count() %>% - ungroup() + dplyr::group_by(.data$first_observed) %>% + dplyr::count() %>% + dplyr::ungroup() maxDate <- max(data_top_graph$first_observed) - # top graph with all counts + # top graph with all dplyr::counts top_graph <- ggplot( data_top_graph, - aes(x = .data$first_observed, y = .data$n) + ggplot2::aes(x = .data$first_observed, y = .data$n) ) + - geom_point(stat = "identity") + - geom_smooth(span = smooth_span) + - xlab(x_lab) + - ylab(y_lab) + - scale_x_continuous( + ggplot2::geom_point(stat = "identity") + + ggplot2::geom_smooth(span = smooth_span) + + ggplot2::xlab(x_lab) + + ggplot2::ylab(y_lab) + + ggplot2::scale_x_continuous( breaks = seq( start_year_plot, maxDate, @@ -203,27 +202,27 @@ indicator_introduction_year <- function(df, x_minor_scale_stepsize ) ) + - coord_cartesian(xlim = c(start_year_plot, maxDate)) + ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate)) if (is.null(facet_column)) { - return(top_graph) + return(list(plot = top_graph, data_top_graph = data_top_graph)) } else { data_facet_graph <- data %>% - group_by(!!!syms(c("first_observed", facet_column))) %>% - count() %>% - ungroup() + dplyr::group_by(!!!dplyr::syms(c("first_observed", facet_column))) %>% + dplyr::count() %>% + dplyr::ungroup() maxDate <- max(data_facet_graph$first_observed) facet_graph <- ggplot( data_facet_graph, - aes(x = .data$first_observed, y = .data$n) + ggplot2::aes(x = .data$first_observed, y = .data$n) ) + - geom_point(stat = "identity") + - geom_smooth(span = smooth_span) + - facet_wrap(facet_column) + - xlab(x_lab) + - ylab(y_lab) + - scale_x_continuous( + ggplot2::geom_point(stat = "identity") + + ggplot2::geom_smooth(span = smooth_span) + + ggplot2::facet_wrap(facet_column) + + ggplot2::xlab(x_lab) + + ggplot2::ylab(y_lab) + + ggplot2::scale_x_continuous( breaks = seq( start_year_plot, maxDate, @@ -235,8 +234,10 @@ indicator_introduction_year <- function(df, x_minor_scale_stepsize ) ) + - coord_cartesian(xlim = c(start_year_plot, maxDate)) + ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate)) - ggarrange(top_graph, facet_graph) + return(list(plot = egg::ggarrange(top_graph, facet_graph), + data_top_graph = data_top_graph, + date_facet_graph = data_facet_graph)) } } diff --git a/man/indicator_introduction_year.Rd b/man/indicator_introduction_year.Rd index 3ba8d16f..363702f1 100644 --- a/man/indicator_introduction_year.Rd +++ b/man/indicator_introduction_year.Rd @@ -24,7 +24,7 @@ indicator_introduction_year( \item{smooth_span}{(numeric) Parameter for the applied \code{\link[stats]{loess}} smoother. For more information on the -appropriate value, see \code{\link[ggplot2]{geom_smooth}}. Default: 0.85.} +appropriate value, see \code{\link[ggplot2]{ggplot2::geom_smooth}}. Default: 0.85.} \item{x_major_scale_stepsize}{(integer) Parameter that indicates the breaks of the x axis. Default: 10.} @@ -50,7 +50,13 @@ information about year of introduction. Default: \code{first_observed}.} \item{y_lab}{NULL or character. to set or remove the y-axis label.} } \value{ -A ggplot2 object (or egg object if facets are used). +A list with two slots: +\itemize{ +\item \code{plot}: ggplot2 object (or egg object if facets are used). +\item \code{data_top_graph}: data.frame (tibble) with data used for the main plot (top graph) in \code{plot}. +\item \code{data_facet_graph}: data.frame (tibble) with data used for the faceting +plot in \code{plot}. If \code{facet_column} is NULL, NULL is returned. +} } \description{ Calculate how many new species has been introduced in a year. diff --git a/man/visualize_pathways_year_level1.Rd b/man/visualize_pathways_year_level1.Rd index d8417409..35ad62f9 100644 --- a/man/visualize_pathways_year_level1.Rd +++ b/man/visualize_pathways_year_level1.Rd @@ -140,7 +140,7 @@ visualize_pathways_year_level1( visualize_pathways_year_level1( data, x_lab = "Jaar", - y_lab = "Aantal geïntroduceerde taxa" + y_lab = "Aantal geïntroduceerde taxa" ) } } diff --git a/man/visualize_pathways_year_level2.Rd b/man/visualize_pathways_year_level2.Rd index 1643ed8e..136ab4f0 100644 --- a/man/visualize_pathways_year_level2.Rd +++ b/man/visualize_pathways_year_level2.Rd @@ -180,7 +180,7 @@ visualize_pathways_year_level2( data, chosen_pathway_level1 = "escape", x_lab = "Jaar", - y_lab = "Aantal geïntroduceerde taxa" + y_lab = "Aantal geïntroduceerde taxa" ) } } From 7938560d2f7954f985f0f6a378a6796f07721636 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Thu, 12 May 2022 16:58:08 +0200 Subject: [PATCH 02/32] Add slot with NULL if no facets are used --- R/indicator_introduction_year.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index 49c4ced0..9add1611 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -205,7 +205,9 @@ indicator_introduction_year <- function(df, ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate)) if (is.null(facet_column)) { - return(list(plot = top_graph, data_top_graph = data_top_graph)) + return(list(plot = top_graph, + data_top_graph = data_top_graph, + data_facet_graph = NULL)) } else { data_facet_graph <- data %>% dplyr::group_by(!!!dplyr::syms(c("first_observed", facet_column))) %>% From 6fe3412a42ce8736885c15b64862f3cad0508c54 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Thu, 12 May 2022 16:59:17 +0200 Subject: [PATCH 03/32] Fix #84 for indicator_total_year --- R/indicator_total_year.R | 15 ++++++++++++--- man/indicator_total_year.Rd | 8 +++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/R/indicator_total_year.R b/R/indicator_total_year.R index 73825615..e182f03f 100644 --- a/R/indicator_total_year.R +++ b/R/indicator_total_year.R @@ -28,7 +28,11 @@ #' @param y_lab NULL or character. To personalize or remove the y-axis label. #' Default: "Cumulative number of alien species". #' -#' @return ggplot2 object (or egg object if facets are used). +#' @return A list with two slots: +#' - `plot`: ggplot2 object (or egg object if facets are used). +#' - `data_top_graph`: data.frame (tibble) with data used for the main plot (top graph) in `plot`. +#' - `data_facet_graph`: data.frame (tibble) with data used for the faceting +#' plot in `plot`. If `facet_column` is NULL, NULL is returned. #' #' @export #' @importFrom assertthat assert_that @@ -200,7 +204,9 @@ indicator_total_year <- function(df, start_year_plot = 1940, coord_cartesian(xlim = c(start_year_plot, maxDate)) if (is.null(facet_column)) { - return(top_graph) + return(list(plot = top_graph, + data_top_graph = df_extended, + data_facet_graph = NULL)) } else { # calculate numbers @@ -225,6 +231,9 @@ indicator_total_year <- function(df, start_year_plot = 1940, ) + coord_cartesian(xlim = c(start_year_plot, maxDate)) - ggarrange(top_graph, facet_graph) + return(list(plot = ggarrange(top_graph, facet_graph), + data_top_graph = df_extended, + data_facet_graph = counts_ias_grouped) + ) } } diff --git a/man/indicator_total_year.Rd b/man/indicator_total_year.Rd index 08ebf51a..cf74b834 100644 --- a/man/indicator_total_year.Rd +++ b/man/indicator_total_year.Rd @@ -51,7 +51,13 @@ Default: "Year.} Default: "Cumulative number of alien species".} } \value{ -ggplot2 object (or egg object if facets are used). +A list with two slots: +\itemize{ +\item \code{plot}: ggplot2 object (or egg object if facets are used). +\item \code{data_top_graph}: data.frame (tibble) with data used for the main plot (top graph) in \code{plot}. +\item \code{data_facet_graph}: data.frame (tibble) with data used for the faceting +plot in \code{plot}. If \code{facet_column} is NULL, NULL is returned. +} } \description{ This function calculates the cumulative number of taxa introduced per year. From db04b39fc1f5a8f9a411b80b0f3d7a2adbd7f09e Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Thu, 12 May 2022 16:59:30 +0200 Subject: [PATCH 04/32] Remove typo from previous changes --- NAMESPACE | 1 - R/indicator_introduction_year.R | 1 - 2 files changed, 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 41c9da7b..6f2797d8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,7 +22,6 @@ importFrom(assertable,assert_colnames) importFrom(assertthat,assert_that) importFrom(assertthat,is.date) importFrom(dplyr,"%>%") -importFrom(dplyr,"ggplot2::geom_smooth") importFrom(dplyr,.data) importFrom(dplyr,add_row) importFrom(dplyr,add_tally) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index 9add1611..d711129a 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -32,7 +32,6 @@ #' #' @export #' @importFrom dplyr %>% -#' ggplot2::geom_smooth #' @importFrom rlang !!! #' #' @examples From 793a404c11f266278b2c65e02e74a9b0faf0dda8 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 11:46:19 +0200 Subject: [PATCH 05/32] Update documentation --- DESCRIPTION | 2 +- R/indicator_introduction_year.R | 4 ++-- R/indicator_total_year.R | 2 +- man/indicator_introduction_year.Rd | 2 +- man/indicator_total_year.Rd | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c8fa90a6..8cfb8b2a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -70,4 +70,4 @@ Encoding: UTF-8 LazyData: true LazyDataCompression: bzip2 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.0 diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index d711129a..b8a10581 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -24,7 +24,7 @@ #' @param x_lab NULL or character. to set or remove the x-axis label. #' @param y_lab NULL or character. to set or remove the y-axis label. #' -#' @return A list with two slots: +#' @return A list with three slots: #' - `plot`: ggplot2 object (or egg object if facets are used). #' - `data_top_graph`: data.frame (tibble) with data used for the main plot (top graph) in `plot`. #' - `data_facet_graph`: data.frame (tibble) with data used for the faceting @@ -153,7 +153,7 @@ indicator_introduction_year <- function(df, " records have no information about year of introduction ", "(empty values in column ", first_observed, - ") and are not taken into acdplyr::count.\n" + ") and are not taken into account.\n" )) } diff --git a/R/indicator_total_year.R b/R/indicator_total_year.R index e182f03f..77249295 100644 --- a/R/indicator_total_year.R +++ b/R/indicator_total_year.R @@ -28,7 +28,7 @@ #' @param y_lab NULL or character. To personalize or remove the y-axis label. #' Default: "Cumulative number of alien species". #' -#' @return A list with two slots: +#' @return A list with three slots: #' - `plot`: ggplot2 object (or egg object if facets are used). #' - `data_top_graph`: data.frame (tibble) with data used for the main plot (top graph) in `plot`. #' - `data_facet_graph`: data.frame (tibble) with data used for the faceting diff --git a/man/indicator_introduction_year.Rd b/man/indicator_introduction_year.Rd index 363702f1..f27f0156 100644 --- a/man/indicator_introduction_year.Rd +++ b/man/indicator_introduction_year.Rd @@ -50,7 +50,7 @@ information about year of introduction. Default: \code{first_observed}.} \item{y_lab}{NULL or character. to set or remove the y-axis label.} } \value{ -A list with two slots: +A list with three slots: \itemize{ \item \code{plot}: ggplot2 object (or egg object if facets are used). \item \code{data_top_graph}: data.frame (tibble) with data used for the main plot (top graph) in \code{plot}. diff --git a/man/indicator_total_year.Rd b/man/indicator_total_year.Rd index cf74b834..79c1b03f 100644 --- a/man/indicator_total_year.Rd +++ b/man/indicator_total_year.Rd @@ -51,7 +51,7 @@ Default: "Year.} Default: "Cumulative number of alien species".} } \value{ -A list with two slots: +A list with three slots: \itemize{ \item \code{plot}: ggplot2 object (or egg object if facets are used). \item \code{data_top_graph}: data.frame (tibble) with data used for the main plot (top graph) in \code{plot}. From d38b8e97b27cbf4402ab0cbc144bff2ede386b7c Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 11:57:16 +0200 Subject: [PATCH 06/32] Remove typo in output slot name --- R/indicator_introduction_year.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index b8a10581..26eb0f62 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -239,6 +239,6 @@ indicator_introduction_year <- function(df, return(list(plot = egg::ggarrange(top_graph, facet_graph), data_top_graph = data_top_graph, - date_facet_graph = data_facet_graph)) + data_facet_graph = data_facet_graph)) } } From e677ebacc2ff35f1629c2d4094203f43be27448b Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 11:57:30 +0200 Subject: [PATCH 07/32] Improve example in documentation --- R/indicator_introduction_year.R | 3 +-- man/indicator_introduction_year.Rd | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index 26eb0f62..e5f9745b 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -42,13 +42,12 @@ #' "interim/data_input_checklist_indicators.tsv" #' ) #' data <- read_tsv(datafile, -#' na = "NA", +#' na = "", #' col_types = cols( #' .default = col_character(), #' key = col_double(), #' nubKey = col_double(), #' speciesKey = col_double(), -#' acceptedKey = col_double(), #' first_observed = col_double(), #' last_observed = col_double() #' ) diff --git a/man/indicator_introduction_year.Rd b/man/indicator_introduction_year.Rd index f27f0156..297bf734 100644 --- a/man/indicator_introduction_year.Rd +++ b/man/indicator_introduction_year.Rd @@ -69,13 +69,12 @@ datafile <- paste0( "interim/data_input_checklist_indicators.tsv" ) data <- read_tsv(datafile, - na = "NA", + na = "", col_types = cols( .default = col_character(), key = col_double(), nubKey = col_double(), speciesKey = col_double(), - acceptedKey = col_double(), first_observed = col_double(), last_observed = col_double() ) From 2427ccd7720885fe9dcc9a66a1a06cd49897afb8 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 11:58:21 +0200 Subject: [PATCH 08/32] Update example for total_year function as well --- R/indicator_total_year.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/indicator_total_year.R b/R/indicator_total_year.R index 77249295..c638db0a 100644 --- a/R/indicator_total_year.R +++ b/R/indicator_total_year.R @@ -53,13 +53,12 @@ #' "interim/data_input_checklist_indicators.tsv" #' ) #' data <- read_tsv(datafile, -#' na = "NA", +#' na = "", #' col_types = cols( #' .default = col_character(), #' key = col_double(), #' nubKey = col_double(), #' speciesKey = col_double(), -#' acceptedKey = col_double(), #' first_observed = col_double(), #' last_observed = col_double() #' ) From d2a0594e38e8403c946750e0f0def7117152da3b Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:01:35 +0200 Subject: [PATCH 09/32] Adapt and add tests conformed to #84 For indicator_introduction_year function --- .../test-indicator_introduction_year.R | 64 +++++++++++++------ 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/tests/testthat/test-indicator_introduction_year.R b/tests/testthat/test-indicator_introduction_year.R index 4b46d52d..71f5e552 100644 --- a/tests/testthat/test-indicator_introduction_year.R +++ b/tests/testthat/test-indicator_introduction_year.R @@ -23,14 +23,14 @@ plot_output_with_facets <- facet_column = "kingdom" ) testthat::test_that("Param: df", { - expect_error( + testthat::expect_error( indicator_introduction_year(3), "df is not a data frame" ) }) testthat::test_that("Param: start_year_plot", { - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, start_year_plot = "1950" ), @@ -39,7 +39,7 @@ testthat::test_that("Param: start_year_plot", { }) testthat::test_that("Param: smooth_span", { - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, smooth_span = "0.4" ), @@ -48,19 +48,19 @@ testthat::test_that("Param: smooth_span", { }) testthat::test_that("Param: x_scale_stepsize", { - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, x_minor_scale_stepsize = "10" ), "Argument x_minor_scale_stepsize has to be a number." ) - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, x_major_scale_stepsize = "10" ), "Argument x_major_scale_stepsize has to be a number." ) - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, x_major_scale_stepsize = 50, x_minor_scale_stepsize = 100 @@ -73,11 +73,11 @@ testthat::test_that("Param: x_scale_stepsize", { }) testthat::test_that("Param: facet_column", { - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, facet_column = 3), "Argument facet_column has to be NULL or a character." ) - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, facet_column = "not nice" ) @@ -85,21 +85,21 @@ testthat::test_that("Param: facet_column", { }) testthat::test_that("Param: taxon_key_col", { - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, taxon_key_col = 3), "Argument taxon_key_col has to be a character." ) - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, taxon_key_col = "blablabla") ) }) testthat::test_that("Param: first_observed", { - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, first_observed = 3), "Argument first_observed has to be a character." ) - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, first_observed = "bad_colname" ) @@ -107,11 +107,11 @@ testthat::test_that("Param: first_observed", { }) testthat::test_that("Param: labels", { - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, x_lab = input_test_df), "Argument x_lab has to be a character or NULL." ) - expect_error( + testthat::expect_error( indicator_introduction_year(input_test_df, y_lab = 4), "Argument y_lab has to be a character or NULL." ) @@ -130,8 +130,36 @@ testthat::test_that("Test warnings", { }) testthat::test_that("Test output class", { - expect_type(plot_output_without_facets, type = "list") - expect_type(plot_output_with_facets, type = "list") - expect_s3_class(plot_output_without_facets, class = "gg") - expect_s3_class(plot_output_with_facets, class = "egg") + # output is a list + testthat::expect_type(plot_output_without_facets, type = "list") + testthat::expect_type(plot_output_with_facets, type = "list") + + # plot slot is a list with gg as class + testthat::expect_type(plot_output_without_facets$plot, type = "list") + testthat::expect_type(plot_output_with_facets$plot, type = "list") + testthat::expect_s3_class(plot_output_without_facets$plot, class = "gg") + testthat::expect_s3_class(plot_output_with_facets$plot, class = "egg") + + # data_top_graph is a data.frame (tibble) + testthat::expect_type(plot_output_with_facets$data_top_graph, type = "list") + testthat::expect_s3_class(plot_output_with_facets$data_top_graph, + class = "data.frame") + testthat::expect_s3_class(plot_output_with_facets$data_top_graph, + class = "tbl_df") + testthat::expect_type(plot_output_without_facets$data_top_graph, + type = "list") + testthat::expect_s3_class(plot_output_without_facets$data_top_graph, + class = "data.frame") + testthat::expect_s3_class(plot_output_without_facets$data_top_graph, + class = "tbl_df") + + # data_facet_graph is NULL if faceting is activated + testthat::expect_null(plot_output_without_facets$date_facet_graph) + + # data_facet_graph is a data.frame (tibble) if faceting is activated + testthat::expect_type(plot_output_with_facets$data_facet_graph, type = "list") + testthat::expect_s3_class(plot_output_with_facets$data_facet_graph, + class = "data.frame") + testthat::expect_s3_class(plot_output_with_facets$data_facet_graph, + class = "tbl_df") }) From e9cb522e5ca120cd5ed72aa02cf929f20904fd10 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:01:51 +0200 Subject: [PATCH 10/32] Remove typo in documentation --- R/indicator_introduction_year.R | 2 +- man/indicator_introduction_year.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index e5f9745b..1bfdbedf 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -61,7 +61,7 @@ #' ) #' # with facets #' indicator_introduction_year(data, facet_column = "kingdom") -#' # specifiy columns with year of first observed +#' # specify columns with year of first observed #' indicator_introduction_year(data, #' first_observed = "first_observed" #' ) diff --git a/man/indicator_introduction_year.Rd b/man/indicator_introduction_year.Rd index 297bf734..f0dde858 100644 --- a/man/indicator_introduction_year.Rd +++ b/man/indicator_introduction_year.Rd @@ -88,7 +88,7 @@ indicator_introduction_year(data, ) # with facets indicator_introduction_year(data, facet_column = "kingdom") -# specifiy columns with year of first observed +# specify columns with year of first observed indicator_introduction_year(data, first_observed = "first_observed" ) From 24eb130b8ca48171009d625f8b128ba888bfe57a Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:02:12 +0200 Subject: [PATCH 11/32] Improve example as done in other indicator function --- man/indicator_total_year.Rd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/man/indicator_total_year.Rd b/man/indicator_total_year.Rd index 79c1b03f..bb30c6ef 100644 --- a/man/indicator_total_year.Rd +++ b/man/indicator_total_year.Rd @@ -72,13 +72,12 @@ datafile <- paste0( "interim/data_input_checklist_indicators.tsv" ) data <- read_tsv(datafile, - na = "NA", + na = "", col_types = cols( .default = col_character(), key = col_double(), nubKey = col_double(), speciesKey = col_double(), - acceptedKey = col_double(), first_observed = col_double(), last_observed = col_double() ) From 58a74c035a280c186f0466023cd43e319dbfcccc Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:33:47 +0200 Subject: [PATCH 12/32] Update tests for total_year fun conformed to #84 --- tests/testthat/test-indicator_total_year.R | 64 ++++++++++++++++------ 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/tests/testthat/test-indicator_total_year.R b/tests/testthat/test-indicator_total_year.R index 6b65f1ad..14d95c7e 100644 --- a/tests/testthat/test-indicator_total_year.R +++ b/tests/testthat/test-indicator_total_year.R @@ -24,20 +24,20 @@ plot_output_with_facets <- ) testthat::test_that("Param: df", { - expect_error( + testthat::expect_error( indicator_total_year(3), "df is not a data frame" ) }) testthat::test_that("Param: start_year_plot", { - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, start_year_plot = "1950" ), "Argument start_year_plot has to be a number." ) - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, start_year_plot = 2900 ), @@ -49,19 +49,19 @@ testthat::test_that("Param: start_year_plot", { }) testthat::test_that("Param: x_scale_stepsize", { - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, x_minor_scale_stepsize = "10" ), "Argument x_minor_scale_stepsize has to be a number." ) - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, x_major_scale_stepsize = "10" ), "Argument x_major_scale_stepsize has to be a number." ) - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, x_major_scale_stepsize = 50, x_minor_scale_stepsize = 100 @@ -74,11 +74,11 @@ testthat::test_that("Param: x_scale_stepsize", { }) testthat::test_that("Param: facet_column", { - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, facet_column = 3), "Argument facet_column has to be NULL or a character." ) - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, facet_column = "not nice" ) @@ -86,17 +86,17 @@ testthat::test_that("Param: facet_column", { }) testthat::test_that("Param: taxon_key_col", { - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, taxon_key_col = "blablabla") ) }) testthat::test_that("Param: first_observed", { - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, first_observed = 4), "Argument first_observed has to be a character." ) - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, first_observed = "strange_colname" ) @@ -104,18 +104,18 @@ testthat::test_that("Param: first_observed", { }) testthat::test_that("Param: labels", { - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, x_lab = input_test_df), "Argument x_lab has to be a character or NULL." ) - expect_error( + testthat::expect_error( indicator_total_year(input_test_df, y_lab = 4), "Argument y_lab has to be a character or NULL." ) }) testthat::test_that("Test warnings", { - expect_warning(indicator_total_year(input_test_df_with_na), + testthat::expect_warning(indicator_total_year(input_test_df_with_na), paste0( nrow_no_first_obs, " records have no information about year of introduction ", @@ -127,8 +127,36 @@ testthat::test_that("Test warnings", { }) testthat::test_that("Test output class", { - expect_type(plot_output_without_facets, type = "list") - expect_type(plot_output_with_facets, type = "list") - expect_s3_class(plot_output_without_facets, class = "gg") - expect_s3_class(plot_output_with_facets, class = "egg") + # output is a list + testthat::expect_type(plot_output_without_facets, type = "list") + testthat::expect_type(plot_output_with_facets, type = "list") + + # plot slot is a list with gg as class + testthat::expect_type(plot_output_without_facets$plot, type = "list") + testthat::expect_type(plot_output_with_facets$plot, type = "list") + testthat::expect_s3_class(plot_output_without_facets$plot, class = "gg") + testthat::expect_s3_class(plot_output_with_facets$plot, class = "egg") + + # data_top_graph is a data.frame (tibble) + testthat::expect_type(plot_output_with_facets$data_top_graph, type = "list") + testthat::expect_s3_class(plot_output_with_facets$data_top_graph, + class = "data.frame") + testthat::expect_s3_class(plot_output_with_facets$data_top_graph, + class = "tbl_df") + testthat::expect_type(plot_output_without_facets$data_top_graph, + type = "list") + testthat::expect_s3_class(plot_output_without_facets$data_top_graph, + class = "data.frame") + testthat::expect_s3_class(plot_output_without_facets$data_top_graph, + class = "tbl_df") + + # data_facet_graph is NULL if faceting is activated + testthat::expect_null(plot_output_without_facets$date_facet_graph) + + # data_facet_graph is a data.frame (tibble) if faceting is activated + testthat::expect_type(plot_output_with_facets$data_facet_graph, type = "list") + testthat::expect_s3_class(plot_output_with_facets$data_facet_graph, + class = "data.frame") + testthat::expect_s3_class(plot_output_with_facets$data_facet_graph, + class = "tbl_df") }) From 880ab5658fd4414009bfe992312ab79170e14a66 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:34:27 +0200 Subject: [PATCH 13/32] Avoid using deprecated or superseded dplyr functions --- R/gbif_has_distribution.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/gbif_has_distribution.R b/R/gbif_has_distribution.R index 7c69043d..d52ea100 100644 --- a/R/gbif_has_distribution.R +++ b/R/gbif_has_distribution.R @@ -122,8 +122,8 @@ gbif_has_distribution <- function(taxon_key, ...) { cross_df(), distr_properties %>% select(names(user_properties)) %>% - distinct_(.dots = names(user_properties)) %>% - mutate_all(toupper) + dplyr::distinct() %>% + dplyr::mutate(dplyr::across(.cols = everything(), toupper)) ) %>% nrow() > 0 return(has_distr) From 1ef01f00d508eaaaa91ebf7554180703823be989 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:34:40 +0200 Subject: [PATCH 14/32] Improve example --- R/gbif_has_distribution.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/gbif_has_distribution.R b/R/gbif_has_distribution.R index d52ea100..16198cdf 100644 --- a/R/gbif_has_distribution.R +++ b/R/gbif_has_distribution.R @@ -38,7 +38,7 @@ #' #' # Case insensitive #' gbif_has_distribution("145953242", -#' countryCode = "be", +#' country = "be", #' status = "PRESENT", #' establishmentMeans = "InTrOdUcEd" #' ) From ad9887167ff9a6939662e3c6da364f3953d3332e Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:41:21 +0200 Subject: [PATCH 15/32] Avoid using importFrom See #86 --- NAMESPACE | 4 ---- R/gbif_has_distribution.R | 13 +++++-------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 6f2797d8..d3cc5060 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,7 +36,6 @@ importFrom(dplyr,case_when) importFrom(dplyr,count) importFrom(dplyr,desc) importFrom(dplyr,distinct) -importFrom(dplyr,distinct_) importFrom(dplyr,do) importFrom(dplyr,enquo) importFrom(dplyr,filter) @@ -47,7 +46,6 @@ importFrom(dplyr,if_else) importFrom(dplyr,inner_join) importFrom(dplyr,left_join) importFrom(dplyr,mutate) -importFrom(dplyr,mutate_all) importFrom(dplyr,mutate_if) importFrom(dplyr,n) importFrom(dplyr,n_distinct) @@ -103,9 +101,7 @@ importFrom(mgcv,nb) importFrom(mgcv,summary.gam) importFrom(plotly,ggplotly) importFrom(plotly,layout) -importFrom(purrr,cross_df) importFrom(purrr,list_along) -importFrom(purrr,map) importFrom(purrr,map2) importFrom(purrr,map2_chr) importFrom(purrr,map_chr) diff --git a/R/gbif_has_distribution.R b/R/gbif_has_distribution.R index 16198cdf..9b504d9f 100644 --- a/R/gbif_has_distribution.R +++ b/R/gbif_has_distribution.R @@ -11,10 +11,7 @@ #' occurrenceStatus) and establishmentMeans. #' @return a logical, TRUE or FALSE. #' @export -#' @importFrom assertthat assert_that -#' @importFrom rgbif name_usage -#' @importFrom dplyr select %>% distinct_ mutate_all -#' @importFrom purrr map cross_df +#' @importFrom dplyr %>% #' @examples #' # numeric taxonKey, atomic parameters #' gbif_has_distribution(145953242, @@ -115,13 +112,13 @@ gbif_has_distribution <- function(taxon_key, ...) { return(FALSE) } else { # Avoid mismatch due to any upper/lowercase difference - user_properties <- map(user_properties, ~ toupper(.)) - # Check whether at least + user_properties <- purrr::map(user_properties, ~ toupper(.)) + # Check whether at least one property defined by user is present has_distr <- dplyr::intersect( user_properties %>% - cross_df(), + purrr::cross_df(), distr_properties %>% - select(names(user_properties)) %>% + dplyr::select(names(user_properties)) %>% dplyr::distinct() %>% dplyr::mutate(dplyr::across(.cols = everything(), toupper)) ) %>% From c24a86018993ad7cbce07c1747eff3623aae0200 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:41:57 +0200 Subject: [PATCH 16/32] Update documentation --- man/gbif_has_distribution.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/gbif_has_distribution.Rd b/man/gbif_has_distribution.Rd index edfce59d..9a2f4ad7 100644 --- a/man/gbif_has_distribution.Rd +++ b/man/gbif_has_distribution.Rd @@ -46,7 +46,7 @@ gbif_has_distribution("145953242", # Case insensitive gbif_has_distribution("145953242", - countryCode = "be", + country = "be", status = "PRESENT", establishmentMeans = "InTrOdUcEd" ) From beb23487fee3916bae2df902246c343878f24437 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:43:10 +0200 Subject: [PATCH 17/32] Update version number Major change as this update could break some code based on previous versions --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8cfb8b2a..0f7dc76f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: trias Title: Process Data for the Project Tracking Invasive Alien Species (TrIAS) -Version: 1.6.0 +Version: 2.0.0 Authors@R: c(person(given = "Damiano", family = "Oldoni", From 3f541c9e5b60c96635a0ea1ccbf605fe98e1bf0a Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 8 Jun 2022 12:58:21 +0200 Subject: [PATCH 18/32] Apply #86 to gbif_verify_keys --- NAMESPACE | 2 -- R/gbif_verify_keys.R | 49 ++++++++++++++++++++------------------------ 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index d3cc5060..d45f6dd1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -105,7 +105,6 @@ importFrom(purrr,list_along) importFrom(purrr,map2) importFrom(purrr,map2_chr) importFrom(purrr,map_chr) -importFrom(purrr,map_df) importFrom(purrr,map_dfr) importFrom(purrr,pmap_dfr) importFrom(purrr,reduce) @@ -150,7 +149,6 @@ importFrom(stringr,str_to_lower) importFrom(svDialogs,dlgInput) importFrom(tibble,as_tibble) importFrom(tibble,tibble) -importFrom(tidyr,gather) importFrom(tidyr,unnest) importFrom(tidyselect,all_of) importFrom(tidyselect,ends_with) diff --git a/R/gbif_verify_keys.R b/R/gbif_verify_keys.R index 458e1958..77461656 100644 --- a/R/gbif_verify_keys.R +++ b/R/gbif_verify_keys.R @@ -31,13 +31,8 @@ #' \code{NA} for other two columns. If a key didn't pass the second check #' (\code{is_from_gbif_backbone = FALSE}) then \code{is_synonym} = \code{NA}. #' @export -#' @importFrom assertthat assert_that -#' @importFrom dplyr pull mutate filter left_join full_join select .data enquo -#' @importFrom purrr reduce map_df -#' @importFrom rgbif name_usage +#' @importFrom dplyr .data %>% #' @importFrom rlang !! -#' @importFrom tidyr gather -#' @importFrom tidyselect vars_pull #' @examples #' # input is a vector #' keys1 <- c( @@ -70,28 +65,28 @@ #' gbif_verify_keys(keys3) #' gbif_verify_keys(keys4) gbif_verify_keys <- function(keys, col_keys = "key") { - assert_that(is.data.frame(keys) | is.vector(keys), + assertthat::assert_that(is.data.frame(keys) | is.vector(keys), msg = "keys should be a vector, a named list or a data.frame." ) if (is.data.frame(keys)) { - assert_that(col_keys %in% names(keys), + assertthat::assert_that(col_keys %in% names(keys), msg = paste( "Column with keys not found.", "Did you forget maybe to pass", "the right column name to col_keys?" ) ) - name_col <- vars_pull(names(keys), !!enquo(col_keys)) + name_col <- tidyselect::vars_pull(names(keys), !!dplyr::enquo(col_keys)) # extract vector of keys from df keys <- keys %>% - pull(name_col) + dplyr::pull(name_col) } keys <- keys[!is.na(keys)] if (length(keys) == 0 | isTRUE(all(keys == ""))) { return(NULL) } else { - assert_that(all(keys != "") == TRUE, + assertthat::assert_that(all(keys != "") == TRUE, msg = paste( "Invalid keys:", paste(rep("\"\"\"\"", length(keys[which(keys == "")])), @@ -99,7 +94,7 @@ gbif_verify_keys <- function(keys, col_keys = "key") { ), "." ) ) - assert_that(all(!grepl("\\D", keys)) == TRUE, + assertthat::assert_that(all(!grepl("\\D", keys)) == TRUE, msg = paste( "Invalid keys:", paste(keys[which(!grepl("\\D", keys) == FALSE)], @@ -116,50 +111,50 @@ gbif_verify_keys <- function(keys, col_keys = "key") { names(keys) <- as.character(keys) gbif_info <- keys %>% - map(~ tryCatch(name_usage(.)$data[1, ], + purrr::map(~ tryCatch(rgbif::name_usage(.)$data[1, ], error = function(e) { print(paste("Key", ., "is an invalid GBIF taxon key.")) } )) check_keys <- - map_df(gbif_info, ~ is.character(.) == FALSE) + purrr::map_df(gbif_info, ~ is.character(.) == FALSE) check_keys <- check_keys %>% - gather(key = "key", value = "is_taxonKey") %>% - mutate(key = as.numeric(.data$key)) + tidyr::gather(key = "key", value = "is_taxonKey") %>% + dplyr::mutate(key = as.numeric(.data$key)) valid_keys_df <- check_keys %>% - filter(.data$is_taxonKey == TRUE) + dplyr::filter(.data$is_taxonKey == TRUE) valid_keys <- gbif_info[which(names(gbif_info) %in% valid_keys_df$key)] if (length(valid_keys) > 0) { valid_keys_df <- valid_keys %>% - reduce(bind_rows) %>% - mutate(is_from_gbif_backbone = ifelse(.data$datasetKey == uuid_backbone, + purrr::reduce(bind_rows) %>% + dplyr::mutate(is_from_gbif_backbone = ifelse(.data$datasetKey == uuid_backbone, TRUE, FALSE )) check_keys <- check_keys %>% - left_join(valid_keys_df %>% - select(.data$key, .data$is_from_gbif_backbone), + dplyr::left_join(valid_keys_df %>% + dplyr::select(.data$key, .data$is_from_gbif_backbone), by = "key" ) valid_keys_df <- valid_keys_df %>% - filter(.data$is_from_gbif_backbone == TRUE) %>% - mutate(is_synonym = ifelse(!.data$taxonomicStatus %in% c("ACCEPTED", "DOUBTFUL"), + dplyr::filter(.data$is_from_gbif_backbone == TRUE) %>% + dplyr::mutate(is_synonym = ifelse(!.data$taxonomicStatus %in% c("ACCEPTED", "DOUBTFUL"), TRUE, FALSE )) check_keys <- check_keys %>% - left_join(valid_keys_df %>% - select(.data$key, .data$is_synonym), + dplyr::left_join(valid_keys_df %>% + dplyr::select(.data$key, .data$is_synonym), by = "key" ) } else { check_keys <- check_keys %>% - mutate( + dplyr::mutate( is_from_gbif_backbone = NA, is_synonym = NA ) @@ -167,7 +162,7 @@ gbif_verify_keys <- function(keys, col_keys = "key") { # recreate possible duplicates by joining input_keys_df with check_keys input_keys_df %>% - left_join(check_keys, + dplyr::left_join(check_keys, by = "key") } From 36f8b0e4b4a207b19bd83b35bba1875203f906e0 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Thu, 9 Jun 2022 14:59:29 +0200 Subject: [PATCH 19/32] Improve link to function in documentation --- R/indicator_introduction_year.R | 2 +- man/indicator_introduction_year.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index 1bfdbedf..fbad97c9 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -6,7 +6,7 @@ #' @param start_year_plot Year where the plot starts from. Default: 1920. #' @param smooth_span (numeric) Parameter for the applied #' \code{\link[stats]{loess}} smoother. For more information on the -#' appropriate value, see \code{\link[ggplot2]{ggplot2::geom_smooth}}. Default: 0.85. +#' appropriate value, see [ggplot2::geom_smooth()]. Default: 0.85. #' @param x_major_scale_stepsize (integer) Parameter that indicates the breaks #' of the x axis. Default: 10. #' @param x_minor_scale_stepsize (integer) Parameter that indicates the minor diff --git a/man/indicator_introduction_year.Rd b/man/indicator_introduction_year.Rd index f0dde858..b1e43c11 100644 --- a/man/indicator_introduction_year.Rd +++ b/man/indicator_introduction_year.Rd @@ -24,7 +24,7 @@ indicator_introduction_year( \item{smooth_span}{(numeric) Parameter for the applied \code{\link[stats]{loess}} smoother. For more information on the -appropriate value, see \code{\link[ggplot2]{ggplot2::geom_smooth}}. Default: 0.85.} +appropriate value, see \code{\link[ggplot2:geom_smooth]{ggplot2::geom_smooth()}}. Default: 0.85.} \item{x_major_scale_stepsize}{(integer) Parameter that indicates the breaks of the x axis. Default: 10.} From 1c1e2be6de8dc68a2dbe9383f14ca00a9ff3c280 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Thu, 9 Jun 2022 15:00:59 +0200 Subject: [PATCH 20/32] Replace superseded function rename_at --- R/indicator_introduction_year.R | 4 ++-- R/indicator_total_year.R | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index fbad97c9..9f3c1e07 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -138,8 +138,8 @@ indicator_introduction_year <- function(df, # Rename to default column name df <- df %>% - dplyr::rename_at(vars(first_observed), ~"first_observed") %>% - dplyr::rename_at(vars(taxon_key_col), ~"key") + dplyr::rename_with(.fn = ~"first_observed", .cols = one_of(first_observed)) %>% + dplyr::rename_with(.fn = ~"key", .cols= one_of(taxon_key_col)) # Provide warning messages for first_observed NA values n_first_observed_not_present <- diff --git a/R/indicator_total_year.R b/R/indicator_total_year.R index c638db0a..bb3d360a 100644 --- a/R/indicator_total_year.R +++ b/R/indicator_total_year.R @@ -142,8 +142,8 @@ indicator_total_year <- function(df, start_year_plot = 1940, # rename to default column name df <- df %>% - rename_at(vars(first_observed), ~"first_observed") %>% - rename_at(vars(taxon_key_col), ~"key") + dplyr::rename_with(.fn = ~"first_observed", .cols = one_of(first_observed)) %>% + dplyr::rename_with(.fn = ~"key", .cols= one_of(taxon_key_col)) # Provide warning messages for first_observed NA values n_first_observed_not_present <- From 19ab1537c5512e818d77471ff044cc399fb4aa55 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Thu, 9 Jun 2022 15:01:15 +0200 Subject: [PATCH 21/32] Apply #86 --- NAMESPACE | 5 -- R/indicator_introduction_year.R | 2 +- R/indicator_total_year.R | 91 +++++++++++++++------------------ 3 files changed, 43 insertions(+), 55 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index d45f6dd1..8f373128 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,7 +36,6 @@ importFrom(dplyr,case_when) importFrom(dplyr,count) importFrom(dplyr,desc) importFrom(dplyr,distinct) -importFrom(dplyr,do) importFrom(dplyr,enquo) importFrom(dplyr,filter) importFrom(dplyr,filter_at) @@ -58,7 +57,6 @@ importFrom(dplyr,select) importFrom(dplyr,starts_with) importFrom(dplyr,summarize) importFrom(dplyr,sym) -importFrom(dplyr,syms) importFrom(dplyr,tally) importFrom(dplyr,tibble) importFrom(dplyr,ungroup) @@ -66,7 +64,6 @@ importFrom(dplyr,vars) importFrom(egg,ggarrange) importFrom(forcats,fct_rev) importFrom(ggplot2,aes) -importFrom(ggplot2,coord_cartesian) importFrom(ggplot2,coord_flip) importFrom(ggplot2,element_text) importFrom(ggplot2,facet_wrap) @@ -78,7 +75,6 @@ importFrom(ggplot2,ggplot) importFrom(ggplot2,ggsave) importFrom(ggplot2,ggtitle) importFrom(ggplot2,scale_colour_manual) -importFrom(ggplot2,scale_x_continuous) importFrom(ggplot2,scale_y_continuous) importFrom(ggplot2,theme) importFrom(ggplot2,xlab) @@ -149,7 +145,6 @@ importFrom(stringr,str_to_lower) importFrom(svDialogs,dlgInput) importFrom(tibble,as_tibble) importFrom(tibble,tibble) -importFrom(tidyr,unnest) importFrom(tidyselect,all_of) importFrom(tidyselect,ends_with) importFrom(tidyselect,one_of) diff --git a/R/indicator_introduction_year.R b/R/indicator_introduction_year.R index 9f3c1e07..6016fc0f 100644 --- a/R/indicator_introduction_year.R +++ b/R/indicator_introduction_year.R @@ -31,7 +31,7 @@ #' plot in `plot`. If `facet_column` is NULL, NULL is returned. #' #' @export -#' @importFrom dplyr %>% +#' @importFrom dplyr %>% .data #' @importFrom rlang !!! #' #' @examples diff --git a/R/indicator_total_year.R b/R/indicator_total_year.R index bb3d360a..a3e87094 100644 --- a/R/indicator_total_year.R +++ b/R/indicator_total_year.R @@ -35,15 +35,8 @@ #' plot in `plot`. If `facet_column` is NULL, NULL is returned. #' #' @export -#' @importFrom assertthat assert_that -#' @importFrom assertable assert_colnames -#' @importFrom dplyr %>% filter rowwise do bind_cols group_by count ungroup -#' rename_at distinct .data syms -#' @importFrom tidyr unnest +#' @importFrom dplyr %>% .data #' @importFrom rlang !!! -#' @importFrom ggplot2 coord_cartesian ggplot geom_line aes xlab ylab scale_x_continuous -#' facet_wrap -#' @importFrom egg ggarrange #' #' @examples #' \dontrun{ @@ -85,33 +78,33 @@ indicator_total_year <- function(df, start_year_plot = 1940, y_lab = "Cumulative number of alien species") { # initial input checks - assert_that(is.data.frame(df)) - assert_that(is.numeric(start_year_plot), + assertthat::assert_that(is.data.frame(df)) + assertthat::assert_that(is.numeric(start_year_plot), msg = "Argument start_year_plot has to be a number." ) - assert_that(start_year_plot < as.integer(format(Sys.Date(), "%Y")), + assertthat::assert_that(start_year_plot < as.integer(format(Sys.Date(), "%Y")), msg = paste( "Argument start_year_plot has to be less than", format(Sys.Date(), "%Y") ) ) - assert_that(is.numeric(x_major_scale_stepsize), + assertthat::assert_that(is.numeric(x_major_scale_stepsize), msg = "Argument x_major_scale_stepsize has to be a number." ) - assert_that(is.numeric(x_minor_scale_stepsize), + assertthat::assert_that(is.numeric(x_minor_scale_stepsize), msg = "Argument x_minor_scale_stepsize has to be a number." ) - assert_that(x_major_scale_stepsize >= x_minor_scale_stepsize, + assertthat::assert_that(x_major_scale_stepsize >= x_minor_scale_stepsize, msg = paste0( "x_major_scale_stepsize should be greater ", "than x_minor_scale_stepsize." ) ) - assert_that(is.null(facet_column) | is.character(facet_column), + assertthat::assert_that(is.null(facet_column) | is.character(facet_column), msg = "Argument facet_column has to be NULL or a character." ) if (is.character(facet_column)) { - assert_colnames(df, facet_column, only_colnames = FALSE) + assertable::assert_colnames(df, facet_column, only_colnames = FALSE) } # check for valid facet options @@ -124,19 +117,19 @@ indicator_total_year <- function(df, start_year_plot = 1940, facet_column <- match.arg(facet_column, valid_facet_options) } - assert_that(is.character(taxon_key_col), + assertthat::assert_that(is.character(taxon_key_col), msg = "Argument taxon_key_col has to be a character." ) - assert_colnames(df, taxon_key_col, only_colnames = FALSE) - assert_that(is.character(first_observed), + assertable::assert_colnames(df, taxon_key_col, only_colnames = FALSE) + assertthat::assert_that(is.character(first_observed), msg = "Argument first_observed has to be a character." ) - assert_colnames(df, first_observed, only_colnames = FALSE) + assertable::assert_colnames(df, first_observed, only_colnames = FALSE) - assert_that(is.null(x_lab) | is.character(x_lab), + assertthat::assert_that(is.null(x_lab) | is.character(x_lab), msg = "Argument x_lab has to be a character or NULL." ) - assert_that(is.null(y_lab) | is.character(y_lab), + assertthat::assert_that(is.null(y_lab) | is.character(y_lab), msg = "Argument y_lab has to be a character or NULL." ) # rename to default column name @@ -148,7 +141,7 @@ indicator_total_year <- function(df, start_year_plot = 1940, # Provide warning messages for first_observed NA values n_first_observed_not_present <- df %>% - filter(is.na(.data$first_observed)) %>% + dplyr::filter(is.na(.data$first_observed)) %>% nrow() if (n_first_observed_not_present) { warning(paste0( @@ -160,35 +153,35 @@ indicator_total_year <- function(df, start_year_plot = 1940, )) } - # Filter the incoming data + # filter the incoming data df <- df %>% - filter(!is.na(.data$first_observed)) + dplyr::filter(!is.na(.data$first_observed)) # Distinct values in columns of interest if (is.null(facet_column)) { df <- df %>% - distinct(.data$key, .data$first_observed) + dplyr::distinct(.data$key, .data$first_observed) } else { df <- df %>% - distinct(.data$key, .data$first_observed, .data[[facet_column]]) + dplyr::distinct(.data$key, .data$first_observed, .data[[facet_column]]) } # Make individual records for each year up to now maxDate <- as.integer(format(Sys.Date(), "%Y")) df_extended <- df %>% - rowwise() %>% - do(year = .data$first_observed:maxDate) %>% - bind_cols(df) %>% - unnest(.data$year) + dplyr::rowwise() %>% + dplyr::do(year = .data$first_observed:maxDate) %>% + dplyr::bind_cols(df) %>% + tidyr::unnest(.data$year) - top_graph <- ggplot(df_extended, aes(x = .data$year)) + - geom_line(stat = "count") + - xlab(x_lab) + - ylab(y_lab) + - scale_x_continuous( + top_graph <- ggplot2::ggplot(df_extended, ggplot2::aes(x = .data$year)) + + ggplot2::geom_line(stat = "count") + + ggplot2::xlab(x_lab) + + ggplot2::ylab(y_lab) + + ggplot2::scale_x_continuous( breaks = seq( start_year_plot, maxDate, @@ -200,7 +193,7 @@ indicator_total_year <- function(df, start_year_plot = 1940, x_minor_scale_stepsize ) ) + - coord_cartesian(xlim = c(start_year_plot, maxDate)) + ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate)) if (is.null(facet_column)) { return(list(plot = top_graph, @@ -211,26 +204,26 @@ indicator_total_year <- function(df, start_year_plot = 1940, # calculate numbers counts_ias_grouped <- df_extended %>% - group_by(!!!syms(c("year", facet_column))) %>% - count() %>% - ungroup() + dplyr::group_by(!!!dplyr::syms(c("year", facet_column))) %>% + dplyr::count() %>% + dplyr::ungroup() facet_graph <- - ggplot( + ggplot2::ggplot( counts_ias_grouped, - aes(x = .data$year, y = .data$n) + ggplot2::aes(x = .data$year, y = .data$n) ) + - geom_line(stat = "identity") + - xlab(x_lab) + - ylab(y_lab) + - facet_wrap(facet_column) + - scale_x_continuous( + ggplot2::geom_line(stat = "identity") + + ggplot2::xlab(x_lab) + + ggplot2::ylab(y_lab) + + ggplot2::facet_wrap(facet_column) + + ggplot2::scale_x_continuous( breaks = seq(start_year_plot, maxDate, x_major_scale_stepsize), minor_breaks = seq(start_year_plot, maxDate, x_minor_scale_stepsize) ) + - coord_cartesian(xlim = c(start_year_plot, maxDate)) + ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate)) - return(list(plot = ggarrange(top_graph, facet_graph), + return(list(plot = egg::ggarrange(top_graph, facet_graph), data_top_graph = df_extended, data_facet_graph = counts_ias_grouped) ) From ca34a298c1b33041e4a2bea73cd2ed06854d6586 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 07:34:42 +0200 Subject: [PATCH 22/32] Use action steps from camrtaptor --- .github/workflows/R-CMD-check.yaml | 46 ++++++++++++++++-------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 52dfc863..f4c31aa0 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -32,44 +32,49 @@ jobs: - {os: ubuntu-18.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"} - {os: ubuntu-18.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"} env: + R_REMOTES_NO_ERRORS_FROM_WARNINGS: true + _R_CHECK_SYSTEM_CLOCK_: false RSPM: ${{ matrix.config.rspm }} - GITHUB_PAT: ${{ secrets.GH_PAT }} + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + ORCID_TOKEN: ${{ secrets.ORCID_TOKEN }} steps: - uses: actions/checkout@v2 - - uses: r-lib/actions/setup-r@v1 - id: install-r + - uses: r-lib/actions/setup-r@master with: r-version: ${{ matrix.config.r }} - http-user-agent: ${{ matrix.config.http-user-agent }} - - uses: r-lib/actions/setup-pandoc@v1 + - uses: r-lib/actions/setup-pandoc@master - - name: Install pak and query dependencies + - name: Query dependencies run: | - install.packages("pak", repos = "https://r-lib.github.io/p/pak/dev/") - saveRDS(pak::pkg_deps("local::.", dependencies = TRUE), ".github/r-depends.rds") + install.packages('remotes') + saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) + writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") shell: Rscript {0} - name: Cache R packages - uses: actions/cache@v2 + if: runner.os != 'Windows' + uses: actions/cache@v1 with: path: ${{ env.R_LIBS_USER }} - key: ${{ matrix.config.os }}-${{ steps.install-r.outputs.installed-r-version }}-1-${{ hashFiles('.github/r-depends.rds') }} - restore-keys: ${{ matrix.config.os }}-${{ steps.install-r.outputs.installed-r-version }}-1- + key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} + restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- - name: Install system dependencies if: runner.os == 'Linux' + env: + RHUB_PLATFORM: linux-x86_64-ubuntu-gcc run: | - pak::local_system_requirements(execute = TRUE) - pak::pkg_system_requirements("rcmdcheck", execute = TRUE) - shell: Rscript {0} + Rscript -e "remotes::install_github('r-hub/sysreqs')" + sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))") + sudo -s eval "$sysreqs" - name: Install dependencies run: | - pak::local_install_dev_deps(upgrade = TRUE) - pak::pkg_install("rcmdcheck") + remotes::install_deps(dependencies = TRUE) + remotes::install_cran("rcmdcheck") shell: Rscript {0} - name: Session info @@ -82,9 +87,7 @@ jobs: - name: Check env: _R_CHECK_CRAN_INCOMING_: false - run: | - options(crayon.enabled = TRUE) - rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check") + run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "error", check_dir = "check") shell: Rscript {0} - name: Show testthat output @@ -94,7 +97,8 @@ jobs: - name: Upload check results if: failure() - uses: actions/upload-artifact@main + uses: actions/upload-artifact@master with: - name: ${{ matrix.config.os }}-r${{ matrix.config.r }}-results + name: ${{ runner.os }}-r${{ matrix.config.r }}-results path: check + retention-days: 5 \ No newline at end of file From 8e9a46d7241b4fe421e426987b0b1127d2a0fb7f Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 08:15:29 +0200 Subject: [PATCH 23/32] Use check action from camtraptor --- .github/workflows/R-CMD-check.yaml | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index f4c31aa0..736abd9e 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -1,9 +1,3 @@ -# NOTE: This workflow is overkill for most R packages -# check-standard.yaml is likely a better choice -# usethis::use_github_action("check-standard") will install it. -# -# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. -# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions on: push: branches: @@ -14,7 +8,7 @@ on: - main - master -name: R-CMD-check +name: R-CMD-check-OS jobs: R-CMD-check: @@ -28,9 +22,9 @@ jobs: config: - {os: macOS-latest, r: 'release'} - {os: windows-latest, r: 'release'} - - {os: ubuntu-18.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest", http-user-agent: "R/4.0.0 (ubuntu-18.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" } - - {os: ubuntu-18.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"} - - {os: ubuntu-18.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"} + - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} + - {os: ubuntu-20.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} + env: R_REMOTES_NO_ERRORS_FROM_WARNINGS: true _R_CHECK_SYSTEM_CLOCK_: false @@ -101,4 +95,4 @@ jobs: with: name: ${{ runner.os }}-r${{ matrix.config.r }}-results path: check - retention-days: 5 \ No newline at end of file + retention-days: 5 From b84d567d1c1506d84fcc562ef871db7fb617cd14 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 08:21:58 +0200 Subject: [PATCH 24/32] Undo last commit --- .github/workflows/R-CMD-check.yaml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 736abd9e..f4c31aa0 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -1,3 +1,9 @@ +# NOTE: This workflow is overkill for most R packages +# check-standard.yaml is likely a better choice +# usethis::use_github_action("check-standard") will install it. +# +# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. +# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions on: push: branches: @@ -8,7 +14,7 @@ on: - main - master -name: R-CMD-check-OS +name: R-CMD-check jobs: R-CMD-check: @@ -22,9 +28,9 @@ jobs: config: - {os: macOS-latest, r: 'release'} - {os: windows-latest, r: 'release'} - - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} - - {os: ubuntu-20.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} - + - {os: ubuntu-18.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest", http-user-agent: "R/4.0.0 (ubuntu-18.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" } + - {os: ubuntu-18.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"} + - {os: ubuntu-18.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"} env: R_REMOTES_NO_ERRORS_FROM_WARNINGS: true _R_CHECK_SYSTEM_CLOCK_: false @@ -95,4 +101,4 @@ jobs: with: name: ${{ runner.os }}-r${{ matrix.config.r }}-results path: check - retention-days: 5 + retention-days: 5 \ No newline at end of file From 477c483245857bd32fb940c9fe3cd7dd89230c3f Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 10:30:35 +0200 Subject: [PATCH 25/32] Avoid caching by commenting --- .github/workflows/R-CMD-check.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index f4c31aa0..954f7565 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -54,13 +54,13 @@ jobs: writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") shell: Rscript {0} - - name: Cache R packages - if: runner.os != 'Windows' - uses: actions/cache@v1 - with: - path: ${{ env.R_LIBS_USER }} - key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} - restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- + # - name: Cache R packages + # if: runner.os != 'Windows' + # uses: actions/cache@v1 + # with: + # path: ${{ env.R_LIBS_USER }} + # key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} + # restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- - name: Install system dependencies if: runner.os == 'Linux' From 4405adfb55fdbc38374a0d3ba16a25cfaf53fab1 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 11:13:01 +0200 Subject: [PATCH 26/32] Try to force installing from binaries for Win and macOS --- .github/workflows/R-CMD-check.yaml | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 954f7565..6ddb5cbe 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -54,14 +54,6 @@ jobs: writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") shell: Rscript {0} - # - name: Cache R packages - # if: runner.os != 'Windows' - # uses: actions/cache@v1 - # with: - # path: ${{ env.R_LIBS_USER }} - # key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} - # restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- - - name: Install system dependencies if: runner.os == 'Linux' env: @@ -73,8 +65,15 @@ jobs: - name: Install dependencies run: | - remotes::install_deps(dependencies = TRUE) - remotes::install_cran("rcmdcheck") + if [ "$RUNNER_OS" == "Linux" ]; then + remotes::install_deps(dependencies = TRUE) + remotes::install_cran("rcmdcheck") + elif [ "$RUNNER_OS" == "Windows" ]; then + remotes::install_deps(dependencies = TRUE, type = "win.binary") + remotes::install_cran("rcmdcheck") + elif [ "$RUNNER_OS" == "macOS" ]; then + remotes::install_deps(dependencies = TRUE, type = "mac.binary") + remotes::install_cran("rcmdcheck") shell: Rscript {0} - name: Session info From e4aebd57ba2977418d8f6f059d015f743afb22d1 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 11:19:44 +0200 Subject: [PATCH 27/32] Solve syntax if else in yaml --- .github/workflows/R-CMD-check.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 6ddb5cbe..a23bf70b 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -65,13 +65,13 @@ jobs: - name: Install dependencies run: | - if [ "$RUNNER_OS" == "Linux" ]; then + if "$RUNNER_OS" == "Linux" then remotes::install_deps(dependencies = TRUE) remotes::install_cran("rcmdcheck") - elif [ "$RUNNER_OS" == "Windows" ]; then + elif "$RUNNER_OS" == "Windows" then remotes::install_deps(dependencies = TRUE, type = "win.binary") remotes::install_cran("rcmdcheck") - elif [ "$RUNNER_OS" == "macOS" ]; then + elif "$RUNNER_OS" == "macOS" then remotes::install_deps(dependencies = TRUE, type = "mac.binary") remotes::install_cran("rcmdcheck") shell: Rscript {0} From 039071b6399e584869f32f1b61789d1ae3d567b7 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 11:29:16 +0200 Subject: [PATCH 28/32] Try standard brackets --- .github/workflows/R-CMD-check.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index a23bf70b..52949f12 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -65,13 +65,13 @@ jobs: - name: Install dependencies run: | - if "$RUNNER_OS" == "Linux" then + if ("$RUNNER_OS" == "Linux"); then remotes::install_deps(dependencies = TRUE) remotes::install_cran("rcmdcheck") - elif "$RUNNER_OS" == "Windows" then + elif ("$RUNNER_OS" == "Windows"); then remotes::install_deps(dependencies = TRUE, type = "win.binary") remotes::install_cran("rcmdcheck") - elif "$RUNNER_OS" == "macOS" then + elif ("$RUNNER_OS" == "macOS"); then remotes::install_deps(dependencies = TRUE, type = "mac.binary") remotes::install_cran("rcmdcheck") shell: Rscript {0} From 4bf67bc6c48e845c345ed317db9df9321c8d7a66 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 11:32:17 +0200 Subject: [PATCH 29/32] Remove semicolon --- .github/workflows/R-CMD-check.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 52949f12..13f84e49 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -65,13 +65,13 @@ jobs: - name: Install dependencies run: | - if ("$RUNNER_OS" == "Linux"); then + if ("$RUNNER_OS" == "Linux") then remotes::install_deps(dependencies = TRUE) remotes::install_cran("rcmdcheck") - elif ("$RUNNER_OS" == "Windows"); then + elif ("$RUNNER_OS" == "Windows") then remotes::install_deps(dependencies = TRUE, type = "win.binary") remotes::install_cran("rcmdcheck") - elif ("$RUNNER_OS" == "macOS"); then + elif ("$RUNNER_OS" == "macOS") then remotes::install_deps(dependencies = TRUE, type = "mac.binary") remotes::install_cran("rcmdcheck") shell: Rscript {0} From acfcae905f27bf8aae4f93c504f7b1850a4af221 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 11:36:36 +0200 Subject: [PATCH 30/32] remove last attempts --- .github/workflows/R-CMD-check.yaml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 13f84e49..e1a1b7fd 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -65,15 +65,8 @@ jobs: - name: Install dependencies run: | - if ("$RUNNER_OS" == "Linux") then - remotes::install_deps(dependencies = TRUE) - remotes::install_cran("rcmdcheck") - elif ("$RUNNER_OS" == "Windows") then - remotes::install_deps(dependencies = TRUE, type = "win.binary") - remotes::install_cran("rcmdcheck") - elif ("$RUNNER_OS" == "macOS") then - remotes::install_deps(dependencies = TRUE, type = "mac.binary") - remotes::install_cran("rcmdcheck") + remotes::install_deps(dependencies = TRUE) + remotes::install_cran("rcmdcheck") shell: Rscript {0} - name: Session info From 8f5502db6f2b7787138cd6ed89625b3c3580f459 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 11:54:27 +0200 Subject: [PATCH 31/32] hotfix: remove win and macOS systems from checks --- .github/workflows/R-CMD-check.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index e1a1b7fd..66bc7915 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -26,8 +26,9 @@ jobs: fail-fast: false matrix: config: - - {os: macOS-latest, r: 'release'} - - {os: windows-latest, r: 'release'} + ## uncomment when updated terra binaries are ready + # - {os: macOS-latest, r: 'release'} + # - {os: windows-latest, r: 'release'} - {os: ubuntu-18.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest", http-user-agent: "R/4.0.0 (ubuntu-18.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" } - {os: ubuntu-18.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"} - {os: ubuntu-18.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"} From 597605812873a27f35eee932ae13d984bfcf1c17 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Fri, 10 Jun 2022 14:23:21 +0200 Subject: [PATCH 32/32] Improve data_top_graph and add/improve tests --- R/indicator_total_year.R | 19 ++++++++++++++----- .../test-indicator_introduction_year.R | 17 ++++++++++++++++- tests/testthat/test-indicator_total_year.R | 11 ++++++++++- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/R/indicator_total_year.R b/R/indicator_total_year.R index a3e87094..fead3eed 100644 --- a/R/indicator_total_year.R +++ b/R/indicator_total_year.R @@ -176,9 +176,18 @@ indicator_total_year <- function(df, start_year_plot = 1940, dplyr::do(year = .data$first_observed:maxDate) %>% dplyr::bind_cols(df) %>% tidyr::unnest(.data$year) - - top_graph <- ggplot2::ggplot(df_extended, ggplot2::aes(x = .data$year)) + - ggplot2::geom_line(stat = "count") + + + # calculate numbers to plot + counts_ias_grouped_by_year <- + df_extended %>% + dplyr::group_by(.data$year) %>% + dplyr::count() %>% + dplyr::ungroup() + top_graph <- + ggplot2::ggplot(counts_ias_grouped_by_year, + ggplot2::aes(x = .data$year, y = .data$n) + ) + + ggplot2::geom_line(stat = "identity") + ggplot2::xlab(x_lab) + ggplot2::ylab(y_lab) + ggplot2::scale_x_continuous( @@ -197,7 +206,7 @@ indicator_total_year <- function(df, start_year_plot = 1940, if (is.null(facet_column)) { return(list(plot = top_graph, - data_top_graph = df_extended, + data_top_graph = counts_ias_grouped_by_year, data_facet_graph = NULL)) } else { @@ -224,7 +233,7 @@ indicator_total_year <- function(df, start_year_plot = 1940, ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate)) return(list(plot = egg::ggarrange(top_graph, facet_graph), - data_top_graph = df_extended, + data_top_graph = counts_ias_grouped_by_year, data_facet_graph = counts_ias_grouped) ) } diff --git a/tests/testthat/test-indicator_introduction_year.R b/tests/testthat/test-indicator_introduction_year.R index 71f5e552..b22c0afc 100644 --- a/tests/testthat/test-indicator_introduction_year.R +++ b/tests/testthat/test-indicator_introduction_year.R @@ -18,10 +18,12 @@ plot_output_without_facets <- indicator_introduction_year(cleaned_input_test_df, facet_column = NULL ) + plot_output_with_facets <- indicator_introduction_year(cleaned_input_test_df, facet_column = "kingdom" ) + testthat::test_that("Param: df", { testthat::expect_error( indicator_introduction_year(3), @@ -153,7 +155,15 @@ testthat::test_that("Test output class", { testthat::expect_s3_class(plot_output_without_facets$data_top_graph, class = "tbl_df") - # data_facet_graph is NULL if faceting is activated + # data_top_graph contains only columns year and n in this order + testthat::expect_equal( + names(plot_output_with_facets$data_top_graph), + c("first_observed", "n")) + testthat::expect_equal( + names(plot_output_without_facets$data_top_graph), + c("first_observed", "n")) + + # data_facet_graph is NULL if faceting is deactivated testthat::expect_null(plot_output_without_facets$date_facet_graph) # data_facet_graph is a data.frame (tibble) if faceting is activated @@ -162,4 +172,9 @@ testthat::test_that("Test output class", { class = "data.frame") testthat::expect_s3_class(plot_output_with_facets$data_facet_graph, class = "tbl_df") + + # data_facet_graph contains only columns year, kingdom (the facet) and n + testthat::expect_equal( + names(plot_output_with_facets$data_facet_graph), + c("first_observed", "kingdom", "n")) }) diff --git a/tests/testthat/test-indicator_total_year.R b/tests/testthat/test-indicator_total_year.R index 14d95c7e..c3cae804 100644 --- a/tests/testthat/test-indicator_total_year.R +++ b/tests/testthat/test-indicator_total_year.R @@ -150,7 +150,16 @@ testthat::test_that("Test output class", { testthat::expect_s3_class(plot_output_without_facets$data_top_graph, class = "tbl_df") - # data_facet_graph is NULL if faceting is activated + # data_top_graph contains only columns year and n in this order + testthat::expect_equal( + names(plot_output_without_facets$data_top_graph), + c("year", "n")) + # data_top_graph contains only columns year and n in this order + testthat::expect_equal( + names(plot_output_with_facets$data_top_graph), + c("year", "n")) + + # data_facet_graph is NULL if faceting is deactivated testthat::expect_null(plot_output_without_facets$date_facet_graph) # data_facet_graph is a data.frame (tibble) if faceting is activated