SeriesAggreg.list.R 5.27 KiB
SeriesAggreg.list <- function(x,
                              Format,
                              ConvertFun,
                              NewTimeFormat = NULL,
                              simplify = FALSE,
                              except = NULL,
                              recursive = TRUE,
                              ...) {
  if (missing(Format)) {
    Format <- getSeriesAggregFormat(NewTimeFormat)
  } else if (!is.null(NewTimeFormat)) {
    warning("deprecated 'NewTimeFormat' argument: 'Format' argument is used instead",
            call. = FALSE)
  # Determination of DatesR
  if (!is.null(x$DatesR)) {
    if (!inherits(x$DatesR, "POSIXt")) {
      stop("'x$DatesR' should be of class 'POSIXt'")
    DatesR <- x$DatesR
  } else {
    # Auto-detection of POSIXt item in Tabseries
    itemPOSIXt <-
      which(sapply(x, function(x) {
        inherits(x, "POSIXt")
      }, simplify = TRUE))[1]
    if (is.na(itemPOSIXt)) {
      stop("At least one item of argument 'x' should be of class 'POSIXt'")
    warning("Item 'DatesR' not found in 'x' argument: the item ",
            names(x)[itemPOSIXt],
            " has been automatically chosen")
    DatesR <- x[[names(x)[itemPOSIXt]]]
  # Selection of numeric items for aggregation
  numericCols <- names(which(sapply(x, inherits, "numeric")))
  arrayCols <- names(which(sapply(x, inherits, "array")))
  numericCols <- setdiff(numericCols, arrayCols)
  if (!is.null(except)) {
    if (!inherits(except, "character")) {
      stop("Argument 'except' should be a 'character'")
    numericCols <- setdiff(numericCols, except)
  cols <- x[numericCols]
  lengthCols <- sapply(cols, length, simplify = TRUE)
  if (any(lengthCols != length(DatesR))) {
    sErr <- paste0(names(lengthCols)[lengthCols != length(DatesR)],
                   " (", lengthCols[lengthCols != length(DatesR)], ")",
                   collapse = ", ")
    warning("The length of the following `numeric` items in 'x' ",
            "is different than the length of 'DatesR (",
            length(DatesR),
            ")': they will be ignored in the aggregation: ",
            sErr)
    cols <- cols[lengthCols == length(DatesR)]
  dfOut <- NULL
  if (length(cols)) {
    ConvertFun2 <- getAggregConvertFun(names(cols))
    if (is.null(recursive)) {
      if (missing(ConvertFun)) {
        stop("'ConvertFun' argument should provided if 'recursive = NULL'")
      } else if (!is.na(ConvertFun)) {
        ConvertFun2 <- rep(ConvertFun, length(cols))
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
dfOut <- SeriesAggreg(cbind(DatesR, as.data.frame(cols)), Format, ..., ConvertFun = ConvertFun2) } if (simplify) { # Returns data.frame of numeric found in the first level of the list return(dfOut) } else { res <- list() # Convert aggregated data.frame into list if (!is.null(dfOut)) { res <- as.list(dfOut) ## To be consistent with InputsModel class and because plot.OutputsModel use the POSIXlt class res$DatesR <- as.POSIXlt(res$DatesR) } # Exploration of embedded lists and data.frames if (is.null(recursive) || recursive) { listCols <- x[sapply(x, inherits, "list")] dfCols <- x[sapply(x, inherits, "data.frame")] dfCols <- c(dfCols, x[sapply(x, inherits, "matrix")]) listCols <- listCols[setdiff(names(listCols), names(dfCols))] if (length(listCols) > 0) { # Check for predefined ConvertFun for all sub-elements ConvertFun <- getAggregConvertFun(names(listCols)) # Run SeriesAggreg for each embedded list listRes <- lapply(names(listCols), function(x) { listCols[[x]]$DatesR <- DatesR SeriesAggreg(listCols[[x]], Format = Format, except = except, ConvertFun = ConvertFun[x], recursive = NULL, ...) }) names(listRes) <- names(listCols) if (is.null(res$DatesR)) { # Copy DatesR in top level list res$DatesR <- listRes[[1]]$DatesR } # Remove DatesR in embedded lists lapply(names(listRes), function(x) { listRes[[x]]$DatesR <<- NULL }) res <- c(res, listRes) } if (length(dfCols) > 0) { # Processing matrix and dataframes for (i in length(dfCols)) { key <- names(dfCols)[i] m <- dfCols[[i]] if (nrow(m) != length(DatesR)) { warning( "The number of rows of the 'matrix' item ", key, " (", nrow(m), ") is different than the length of 'DatesR ('", length(DatesR), "), it will be ignored in the aggregation" ) } else { ConvertFun <- rep(getAggregConvertFun(key), ncol(m)) res[[key]] <- SeriesAggreg(data.frame(DatesR, m), Format = Format, ConvertFun = ConvertFun) } } } }
141142143144145146147148149150151152153154
# Store elements that are not aggregated res <- c(res, x[setdiff(names(x), names(res))]) class(res) <- gsub("hourly|daily|monthly|yearly", getSeriesAggregClass(Format), class(x)) return(res) } }