From 864e3d97587018af405f6fa60ed3f7acab3bec72 Mon Sep 17 00:00:00 2001 From: nperez Date: Fri, 29 Oct 2021 12:00:04 +0200 Subject: [PATCH 1/2] New ArrayToList function --- NAMESPACE | 1 + R/ArrayToList.R | 69 ++++++++++++++++++++++++++++++++++++++++++++++ man/ArrayToList.Rd | 41 +++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 R/ArrayToList.R create mode 100644 man/ArrayToList.Rd diff --git a/NAMESPACE b/NAMESPACE index 8364b61..2a5d243 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(AnoAgree) +export(ArrayToList) export(Climdex) export(CombineIndices) export(DTRIndicator) diff --git a/R/ArrayToList.R b/R/ArrayToList.R new file mode 100644 index 0000000..aee373e --- /dev/null +++ b/R/ArrayToList.R @@ -0,0 +1,69 @@ +#' Split an array into list by a given array dimension +#' +#'@description This function splits an array into a list as required by PlotLayout function from s2dv when 'special_args' parameter is used. The function ArrayToList allows to add names to the elements of the list in two different levels the 'list' or the 'sublist'. +#' +#'@param data A multidimensional array. +#'@param dim A character string indicating the name of the dimension to split or an integer indicating the position of the dimension. +#'@param level A string character 'list' or 'sublist' indicating if it should be a list or a sublist. By default it creates a list. +#'@param names A vector of character strings to name the list (if it is a single string, it would be reused) or a single character string to name the elements in the sublist. +#' +#'@return A list of arrays of the length of the dimension set in parameter 'dim'. +#' +#'@examples +#'data <- array(1:240, c(month = 12, member = 5, time = 4)) +#'# Create a list: +#'datalist <- ArrayToList(data, dim = 'month', level = 'list', names = month.name) +#'class(datalist) +#'class(datalist[[1]]) +#'str(datalist) +#'# Create a sublist: +#'datalist <- ArrayToList(data, dim = 'month', level = 'sublist', names = 'dots') +#'class(datalist) +#'class(datalist[[1]]) +#'class(datalist[[1]][[1]]) +#'str(datalist) +#'@seealso \link[s2dv]{PlotLayout} +#'@export +ArrayToList <- function(data, dim, level = 'list', names = NULL) { + if (is.null(dim(data))) { + stop("Parameter 'data' must be an array or matrix.") + } + if (length(dim) > 1) { + warning("The lenght of parameter 'dim' is greater than 1 and only the", + " first element is used.") + dim <- dim[1] + } + if (is.character(dim)) { + dim <- which(names(dim(data)) == dim) + } + if( !is.character(names)) { + stop("Parameter 'names' is not class character.") + } + datalist <- asplit(data, dim) + attributes(datalist) <- NULL + if (level == 'list') { + if (length(names) == 1) { + names <- rep(names, dim(data)[dim]) + } else { + if (length(names) != dim(data)[dim]) { + stop("The length of parameter names could be 1 or equal to the dimension to split.") + } + } + names(datalist) <- names + } else if (level == 'sublist') { + if (length(names) > 1) { + warning("Parameter 'names' should be a single character string to name the sublist.", + "Only the first element is used.") + names <- names[1] + } + datalist <- lapply(datalist, function(x) { + res <- list(x) + names(res) <- names + return(res)}) + } else { + stop("Parameter 'level' should be 'list' nor 'sublist'.") + } + return(datalist) +} + + diff --git a/man/ArrayToList.Rd b/man/ArrayToList.Rd new file mode 100644 index 0000000..a2fad12 --- /dev/null +++ b/man/ArrayToList.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ArrayToList.R +\name{ArrayToList} +\alias{ArrayToList} +\title{Split an array into list by a given array dimension} +\usage{ +ArrayToList(data, dim, level = "list", names = NULL) +} +\arguments{ +\item{data}{A multidimensional array.} + +\item{dim}{A character string indicating the name of the dimension to split or an integer indicating the position of the dimension.} + +\item{level}{A string character 'list' or 'sublist' indicating if it should be a list or a sublist. By default it creates a list.} + +\item{names}{A vector of character strings to name the list (if it is a single string, it would be reused) or a single character string to name the elements in the sublist.} +} +\value{ +A list of arrays of the length of the dimension set in parameter 'dim'. +} +\description{ +This function splits an array into a list as required by PlotLayout function from s2dv when 'special_args' parameter is used. The function ArrayToList allows to add names to the elements of the list in two different levels the 'list' or the 'sublist'. +} +\examples{ +data <- array(1:240, c(month = 12, member = 5, time = 4)) +# Create a list: +datalist <- ArrayToList(data, dim = 'month', level = 'list', names = month.name) +class(datalist) +class(datalist[[1]]) +str(datalist) +# Create a sublist: +datalist <- ArrayToList(data, dim = 'month', level = 'sublist', names = 'dots') +class(datalist) +class(datalist[[1]]) +class(datalist[[1]][[1]]) +str(datalist) +} +\seealso{ +\link[s2dv]{PlotLayout} +} + -- GitLab From c2c87049fb741a506f14e40911af877c172d232f Mon Sep 17 00:00:00 2001 From: aho Date: Fri, 29 Oct 2021 15:58:43 +0200 Subject: [PATCH 2/2] Add more parameter checks --- R/ArrayToList.R | 10 +++++++--- man/ArrayToList.Rd | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/R/ArrayToList.R b/R/ArrayToList.R index aee373e..e99855e 100644 --- a/R/ArrayToList.R +++ b/R/ArrayToList.R @@ -1,6 +1,6 @@ #' Split an array into list by a given array dimension #' -#'@description This function splits an array into a list as required by PlotLayout function from s2dv when 'special_args' parameter is used. The function ArrayToList allows to add names to the elements of the list in two different levels the 'list' or the 'sublist'. +#'@description This function splits an array into a list as required by PlotLayout function from package "s2dv" when parameter 'special_args' is used. The function ArrayToList allows to add names to the elements of the list in two different levels, the 'list' or the 'sublist'. #' #'@param data A multidimensional array. #'@param dim A character string indicating the name of the dimension to split or an integer indicating the position of the dimension. @@ -33,8 +33,14 @@ ArrayToList <- function(data, dim, level = 'list', names = NULL) { " first element is used.") dim <- dim[1] } + if (is.numeric(dim) & dim > length(dim(data))) { + stop("Parameter 'dim' cannot exceed the length of 'data' dimensions.") + } if (is.character(dim)) { dim <- which(names(dim(data)) == dim) + if (identical(dim, integer(0))) { + stop("Parameter 'dim' is not found in the dimension names.") + } } if( !is.character(names)) { stop("Parameter 'names' is not class character.") @@ -65,5 +71,3 @@ ArrayToList <- function(data, dim, level = 'list', names = NULL) { } return(datalist) } - - diff --git a/man/ArrayToList.Rd b/man/ArrayToList.Rd index a2fad12..9a7d181 100644 --- a/man/ArrayToList.Rd +++ b/man/ArrayToList.Rd @@ -19,7 +19,7 @@ ArrayToList(data, dim, level = "list", names = NULL) A list of arrays of the length of the dimension set in parameter 'dim'. } \description{ -This function splits an array into a list as required by PlotLayout function from s2dv when 'special_args' parameter is used. The function ArrayToList allows to add names to the elements of the list in two different levels the 'list' or the 'sublist'. +This function splits an array into a list as required by PlotLayout function from package "s2dv" when parameter 'special_args' is used. The function ArrayToList allows to add names to the elements of the list in two different levels, the 'list' or the 'sublist'. } \examples{ data <- array(1:240, c(month = 12, member = 5, time = 4)) -- GitLab