From fe4bb3e1d442b1040bc6c43d43605be4fd8c1612 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Tue, 5 Sep 2023 14:32:17 +0200 Subject: [PATCH 1/4] Add print method for 's2dv_cube' --- NAMESPACE | 1 + R/print.s2dv_cube.R | 61 ++++++++++++++++++++++++++++++++++++++++++ man/print.s2dv_cube.Rd | 16 +++++++++++ 3 files changed, 78 insertions(+) create mode 100644 R/print.s2dv_cube.R create mode 100644 man/print.s2dv_cube.Rd diff --git a/NAMESPACE b/NAMESPACE index a2df146d..75f0312a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method(print,s2dv_cube) export(AdamontAnalog) export(AdamontQQCorr) export(Analogs) diff --git a/R/print.s2dv_cube.R b/R/print.s2dv_cube.R new file mode 100644 index 00000000..e59db208 --- /dev/null +++ b/R/print.s2dv_cube.R @@ -0,0 +1,61 @@ +#'Print method for s2dv_cube objects +#' +#'This is an S3 method of the generic 'print' for the class 's2dv_cube'. When +#'you will call 'print' on an 's2dv_cube' object, this method will display the +#'content of the object in a clear and informative way. +#' +#'@param x An 's2dv_cube' object +#'@export +print.s2dv_cube <- function(x) { + cat("'s2dv_cube'\n") + cat("Data ", "[" , paste0(x$data[1:5], collapse = ", "), '...', "]", "\n") + cat("Dimensions ", "(", paste(names(x$dims), x$dims, sep = " = ", collapse = ', '), ")", "\n") + cat("Coordinates \n") + for (coord in names(x$coords)) { + cat(" ", coord, ":", paste(x$coords[[coord]], collapse = ", "), "\n") + } + cat("Attributes \n") + for (attr_name in names(x$attrs)) { + if (attr_name == "Variable") { + cat(" ", "varName :", x$attrs$Variable$varName, "\n") + cat(" ", "metadata : ", "\n") + for (metadata_i in names(x$attrs$Variable$metadata)) { + cat(" ", " ", metadata_i, "\n") + for (info_i in names(x$attrs$Variable$metadata[[metadata_i]])) { + if (info_i == 'units') { + cat(" ", " ", " units :", x$attrs$Variable$metadata[[metadata_i]][[info_i]], "\n") + } else if (info_i %in% c('longname', 'long_name')) { + cat(" ", " ", " long name :", x$attrs$Variable$metadata[[metadata_i]][[info_i]], "\n") + } + } + } + } else { + cat(" ", attr_name, " : ") + .print_beginning(x$attrs[[attr_name]]) + } + } +} + + +## Auxiliary function for the print method +.print_beginning <- function(x, n = 5) { + if (inherits(x, 'numeric') | inherits(x, 'POSIXct') | inherits(x, 'Date')) { + if (length(x) <= n) { + cat(as.character(x), "\n") + } else { + cat(paste0(as.character(x[seq_len(n)])), "...", "\n") + } + } else if (inherits(x, 'list')) { + cat("\n") + for (param in names(x)) { + cat(" ", "(", param,")", " : ") + cat(paste0(names(x[[param]]), collapse = ", "), "\n") + } + } else { + if (length(x) > 1) { + cat(x[[1]], "...", "\n") + } else { + cat(x[[1]], "\n") + } + } +} diff --git a/man/print.s2dv_cube.Rd b/man/print.s2dv_cube.Rd new file mode 100644 index 00000000..9540d215 --- /dev/null +++ b/man/print.s2dv_cube.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/print.s2dv_cube.R +\name{print.s2dv_cube} +\alias{print.s2dv_cube} +\title{Print method for s2dv_cube objects} +\usage{ +\method{print}{s2dv_cube}(x) +} +\arguments{ +\item{x}{An 's2dv_cube' object} +} +\description{ +This is an S3 method of the generic 'print' for the class 's2dv_cube'. When +you will call 'print' on an 's2dv_cube' object, this method will display the +content of the object in a clear and informative way. +} -- GitLab From 1bbdec65f6bba73f0e2f69889116bfd8a013d091 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Tue, 5 Sep 2023 17:54:42 +0200 Subject: [PATCH 2/4] Improve print method for s2dv_cube --- R/print.s2dv_cube.R | 116 +++++++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 27 deletions(-) diff --git a/R/print.s2dv_cube.R b/R/print.s2dv_cube.R index e59db208..a17192c5 100644 --- a/R/print.s2dv_cube.R +++ b/R/print.s2dv_cube.R @@ -7,36 +7,34 @@ #'@param x An 's2dv_cube' object #'@export print.s2dv_cube <- function(x) { - cat("'s2dv_cube'\n") - cat("Data ", "[" , paste0(x$data[1:5], collapse = ", "), '...', "]", "\n") - cat("Dimensions ", "(", paste(names(x$dims), x$dims, sep = " = ", collapse = ', '), ")", "\n") - cat("Coordinates \n") - for (coord in names(x$coords)) { - cat(" ", coord, ":", paste(x$coords[[coord]], collapse = ", "), "\n") - } - cat("Attributes \n") - for (attr_name in names(x$attrs)) { - if (attr_name == "Variable") { - cat(" ", "varName :", x$attrs$Variable$varName, "\n") - cat(" ", "metadata : ", "\n") - for (metadata_i in names(x$attrs$Variable$metadata)) { - cat(" ", " ", metadata_i, "\n") - for (info_i in names(x$attrs$Variable$metadata[[metadata_i]])) { - if (info_i == 'units') { - cat(" ", " ", " units :", x$attrs$Variable$metadata[[metadata_i]][[info_i]], "\n") - } else if (info_i %in% c('longname', 'long_name')) { - cat(" ", " ", " long name :", x$attrs$Variable$metadata[[metadata_i]][[info_i]], "\n") - } - } + if (is.atomic(x)) { + cat(x, "\n") + } else { + cat("'s2dv_cube'\n") + cat("Data ", "[" , paste0(x$data[1:5], collapse = ", "), '...', "]", "\n") + cat("Dimensions ", "(", paste(names(x$dims), x$dims, sep = " = ", collapse = ', '), ")", "\n") + cat("Coordinates \n") + for (coord in names(x$coords)) { + cat(" ", coord, ":", paste(x$coords[[coord]], collapse = ", "), "\n") + } + cat("Attributes \n") + for (attr_name in names(x$attrs)) { + if (attr_name == "Variable") { + cat(" ", "varName :", x$attrs$Variable$varName, "\n") + cat(" ", "metadata : ", "\n") + for (metadata_i in names(x$attrs$Variable$metadata)) { + cat(" ", " ", metadata_i, "\n") + .print_metadata(x$attrs$Variable$metadata[[metadata_i]]) + } + } else { + cat(" ", attr_name, " : ") + .print_beginning(x$attrs[[attr_name]]) } - } else { - cat(" ", attr_name, " : ") - .print_beginning(x$attrs[[attr_name]]) } } + } - ## Auxiliary function for the print method .print_beginning <- function(x, n = 5) { if (inherits(x, 'numeric') | inherits(x, 'POSIXct') | inherits(x, 'Date')) { @@ -47,9 +45,26 @@ print.s2dv_cube <- function(x) { } } else if (inherits(x, 'list')) { cat("\n") + k = 1 + j = 3 for (param in names(x)) { - cat(" ", "(", param,")", " : ") - cat(paste0(names(x[[param]]), collapse = ", "), "\n") + k = k + 1 + param_i <- x[[param]] + if (!is.null(param_i)) { + param_i <- lapply(param_i, function(x) {if (length(x[[1]]) > 1) { + x[[1]] <- paste0(x[[1]][1],' ...') + } else { + x + }}) + cat(" ", "(", param,")", " : ") + cat(paste0(names(unlist(param_i)), " = ", unlist(param_i), collapse = ', '), "\n") + } else { + j = j + 1 + } + if (k > j) { + cat(" ", "...", "\n") + break + } } } else { if (length(x) > 1) { @@ -59,3 +74,50 @@ print.s2dv_cube <- function(x) { } } } + +## Auxiliary function for the print method +.print_metadata <- function(x) { + if (inherits(x, 'list')) { + info_names <- NULL + for (info_i in names(x)) { + if (info_i == 'units') { + cat(" ", " ", " units :", x[[info_i]], "\n") + } else if (info_i %in% c('longname', 'long_name')) { + cat(" ", " ", " long name :", x[[info_i]], "\n") + } else { + info_names <- c(info_names, info_i) + } + } + cat(" ", " ", " other info :", paste0(info_names, collapse = ', '), "\n") + } else if (!is.null(attributes(x))) { + if ('variables' %in% names(attributes(x))) { + info_names <- NULL + attrs <- attributes(x)[['variables']] + for (attrs_i in names(attrs)) { + for (info_i in names(attrs[[attrs_i]])) { + if (!inherits(attrs[[attrs_i]][[info_i]], 'list')) { + if (info_i == 'units') { + cat(" ", " ", " units :", attrs[[attrs_i]][[info_i]], "\n") + } else if (info_i %in% c('longname', 'long_name')) { + cat(" ", " ", " long name :", attrs[[attrs_i]][[info_i]], "\n") + } else { + info_names <- c(info_names, info_i) + } + } + } + } + cat(" ", " ", " other info :", paste0(info_names, collapse = ', '), "\n") + } else { + attrs <- attributes(x) + info_names <- NULL + for (info_i in names(attrs)) { + if (info_i == 'cdo_grid_name') { + cat(" ", " ", " cdo_grid_name :", attrs[[info_i]], "\n") + } else { + info_names <- c(info_names, info_i) + } + } + cat(" ", " ", " other info :", paste0(info_names, collapse = ', '), "\n") + } + } +} \ No newline at end of file -- GitLab From ac4c5f39b87fc055d22d88dbb5448864e84f6088 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Tue, 12 Sep 2023 10:22:33 +0200 Subject: [PATCH 3/4] Final improvements in the print method --- R/print.s2dv_cube.R | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/R/print.s2dv_cube.R b/R/print.s2dv_cube.R index a17192c5..74e1d8b6 100644 --- a/R/print.s2dv_cube.R +++ b/R/print.s2dv_cube.R @@ -11,11 +11,19 @@ print.s2dv_cube <- function(x) { cat(x, "\n") } else { cat("'s2dv_cube'\n") - cat("Data ", "[" , paste0(x$data[1:5], collapse = ", "), '...', "]", "\n") + cat("Data ", "[" , paste0(x$data[1:8], collapse = ", "), '...', "]", "\n") cat("Dimensions ", "(", paste(names(x$dims), x$dims, sep = " = ", collapse = ', '), ")", "\n") cat("Coordinates \n") for (coord in names(x$coords)) { - cat(" ", coord, ":", paste(x$coords[[coord]], collapse = ", "), "\n") + if (!is.null(attr(x$coords[[coord]], 'indices'))) { + if (attr(x$coords[[coord]], 'indices')) { + cat(" ", coord, ":", paste(x$coords[[coord]], collapse = ", "), "\n") + } else { + cat(" *", coord, ":", paste(x$coords[[coord]], collapse = ", "), "\n") + } + } else { + cat(" *", coord, ":", paste(x$coords[[coord]], collapse = ", "), "\n") + } } cat("Attributes \n") for (attr_name in names(x$attrs)) { @@ -36,7 +44,7 @@ print.s2dv_cube <- function(x) { } ## Auxiliary function for the print method -.print_beginning <- function(x, n = 5) { +.print_beginning <- function(x, n = 5, j = 2) { if (inherits(x, 'numeric') | inherits(x, 'POSIXct') | inherits(x, 'Date')) { if (length(x) <= n) { cat(as.character(x), "\n") @@ -46,7 +54,6 @@ print.s2dv_cube <- function(x) { } else if (inherits(x, 'list')) { cat("\n") k = 1 - j = 3 for (param in names(x)) { k = k + 1 param_i <- x[[param]] @@ -88,7 +95,7 @@ print.s2dv_cube <- function(x) { info_names <- c(info_names, info_i) } } - cat(" ", " ", " other info :", paste0(info_names, collapse = ', '), "\n") + cat(" ", " ", " other :", paste0(info_names, collapse = ', '), "\n") } else if (!is.null(attributes(x))) { if ('variables' %in% names(attributes(x))) { info_names <- NULL @@ -106,7 +113,7 @@ print.s2dv_cube <- function(x) { } } } - cat(" ", " ", " other info :", paste0(info_names, collapse = ', '), "\n") + cat(" ", " ", " other :", paste0(info_names, collapse = ', '), "\n") } else { attrs <- attributes(x) info_names <- NULL @@ -117,7 +124,7 @@ print.s2dv_cube <- function(x) { info_names <- c(info_names, info_i) } } - cat(" ", " ", " other info :", paste0(info_names, collapse = ', '), "\n") + cat(" ", " ", " other :", paste0(info_names, collapse = ', '), "\n") } } } \ No newline at end of file -- GitLab From 35ecbc913d0b50be8c93d9fbbc87d9b4cf6ab90d Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Tue, 12 Sep 2023 10:41:39 +0200 Subject: [PATCH 4/4] Update print method documentation --- R/print.s2dv_cube.R | 11 ++++++++++- man/print.s2dv_cube.Rd | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/R/print.s2dv_cube.R b/R/print.s2dv_cube.R index 74e1d8b6..2ad4186d 100644 --- a/R/print.s2dv_cube.R +++ b/R/print.s2dv_cube.R @@ -4,6 +4,15 @@ #'you will call 'print' on an 's2dv_cube' object, this method will display the #'content of the object in a clear and informative way. #' +#'The object will be displayed following 's2dv_cube' class conventions. The +#'top-level elements are: 'Data', a multidimensional array containing the +#'object's data; 'Dimensions', the dimensions of the array; 'Coordinates', the +#'array coordinates that match its dimensions, explicit coordinates have an +#'asterisk (*) at the beginning while index coordinates do not; and +#''Attributes', which contains all the metadata of the object. For more +#'information about the 's2dv_cube', see \code{s2dv_cube()} and +#'\code{as.s2dv_cube()} functions. +#' #'@param x An 's2dv_cube' object #'@export print.s2dv_cube <- function(x) { @@ -44,7 +53,7 @@ print.s2dv_cube <- function(x) { } ## Auxiliary function for the print method -.print_beginning <- function(x, n = 5, j = 2) { +.print_beginning <- function(x, n = 5, j = 1) { if (inherits(x, 'numeric') | inherits(x, 'POSIXct') | inherits(x, 'Date')) { if (length(x) <= n) { cat(as.character(x), "\n") diff --git a/man/print.s2dv_cube.Rd b/man/print.s2dv_cube.Rd index 9540d215..3baca724 100644 --- a/man/print.s2dv_cube.Rd +++ b/man/print.s2dv_cube.Rd @@ -14,3 +14,13 @@ This is an S3 method of the generic 'print' for the class 's2dv_cube'. When you will call 'print' on an 's2dv_cube' object, this method will display the content of the object in a clear and informative way. } +\details{ +The object will be displayed following 's2dv_cube' class conventions. The +top-level elements are: 'Data', a multidimensional array containing the +object's data; 'Dimensions', the dimensions of the array; 'Coordinates', the +array coordinates that match its dimensions, explicit coordinates have an +asterisk (*) at the beginning while index coordinates do not; and +'Attributes', which contains all the metadata of the object. For more +information about the 's2dv_cube', see \code{s2dv_cube()} and +\code{as.s2dv_cube()} functions. +} -- GitLab