From ebe314a9f8f1587ec390d9551979caf4ff700676 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 20 Feb 2023 11:46:09 -0500 Subject: [PATCH 1/2] Prevent crash from dual axis if data is one sided For instance, observed on the left and simulated on the right --- R/observed-data-mapping.R | 3 --- R/timeprofile-datamapping.R | 3 --- 2 files changed, 6 deletions(-) diff --git a/R/observed-data-mapping.R b/R/observed-data-mapping.R index 4cdba0a8..73b0bf3b 100644 --- a/R/observed-data-mapping.R +++ b/R/observed-data-mapping.R @@ -146,9 +146,6 @@ ObservedDataMapping <- R6::R6Class( #' @param data A data.frame #' @return A data.frame to be plotted in right axis getRightAxis = function(data) { - if(!self$requireDualAxis(data)){ - return(NULL) - } # Ensure NAs in that data don't mess up the selection selectedRows <- as.logical(data[, self$y2Axis]) %in% FALSE if (isIncluded(self$ymax, names(data))) { diff --git a/R/timeprofile-datamapping.R b/R/timeprofile-datamapping.R index 7d913276..e3a4027c 100644 --- a/R/timeprofile-datamapping.R +++ b/R/timeprofile-datamapping.R @@ -101,9 +101,6 @@ TimeProfileDataMapping <- R6::R6Class( #' @param data A data.frame #' @return A data.frame to be plotted in right axis getRightAxis = function(data) { - if (!self$requireDualAxis(data)) { - return(NULL) - } # Ensure NAs in that data don't mess up the selection selectedRows <- as.logical(data[, self$y2Axis]) %in% FALSE if (isIncluded(self$ymax, names(data))) { From b431da837828c7aef1602b1e5c88fccfdf61e657 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 20 Feb 2023 11:55:30 -0500 Subject: [PATCH 2/2] Fixes #406 dataMapping objects keep factor levels in same position --- R/utilities-mapping.R | 70 ++++++++++++++++++-------------- man/dot-asLegendCaptionSubset.Rd | 17 ++++++++ man/getDefaultCaptions.Rd | 7 +++- 3 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 man/dot-asLegendCaptionSubset.Rd diff --git a/R/utilities-mapping.R b/R/utilities-mapping.R index 7b0cdb1e..bbe059b1 100644 --- a/R/utilities-mapping.R +++ b/R/utilities-mapping.R @@ -97,44 +97,52 @@ #' #' # Get captions separating variables witha space (character " ") #' getDefaultCaptions(data, metaData, sep = " ") -getDefaultCaptions <- function(data, metaData, variableList = colnames(data), sep = "-") { +getDefaultCaptions <- function(data, metaData = NULL, variableList = colnames(data), sep = "-") { # Check that the grouping is in the list of data variables - stopifnot(variableList %in% colnames(data)) - - groupingVariable <- .asLegendCaptionSubset( - data[, variableList[1]], - metaData[[variableList[1]]] - ) - - # Loop on the variableList except first one - # pasting as a single data.frame column the association of names in all selected variables - for (variable in utils::tail(variableList, -1)) { - groupingVariable <- paste( - groupingVariable, + validateIsIncluded(variableList, colnames(data)) + + captions <- NULL + for(variableName in variableList){ + if(is.null(captions)){ + captions <- .asLegendCaptionSubset( + data[, variableName], + metaData[[variableName]]$unit + ) + next + } + captions <- paste( + captions, .asLegendCaptionSubset( - data[, variable], - metaData[[variable]] - ), - sep = sep + data[, variableName], + metaData[[variableName]]$unit + ), + sep = sep ) + } + + if (isEmpty(captions)) { + return(factor("")) } - - if (length(groupingVariable) == 0) { - groupingVariable <- 1 - } - groupingVariable <- as.factor(groupingVariable) - return(groupingVariable) + return(as.factor(captions)) } - -.asLegendCaptionSubset <- function(data, metaData) { - captionSubset <- as.character(data) - - # If numeric create a character as rounded numeric + unit from metadata - if ("numeric" %in% class(data)) { - captionSubset <- paste(as.character(round(data)), metaData$unit, sep = "") +#' @title .asLegendCaptionSubset +#' @param labels +#' @param unit A character added as unit to label +#' @description +#' Creates default legend captions subset +#' @keywords internal +.asLegendCaptionSubset <- function(labels, unit = NULL) { + # Keep ordering of labels as is if factor + captionLevels <- sort(unique(labels)) + if(isOfType(labels, "factor")){ + captionLevels <- levels(labels) } - + captionSubset <- factor( + getLabelWithUnit(labels, unit = unit), + levels = getLabelWithUnit(captionLevels, unit = unit) + ) + return(captionSubset) } diff --git a/man/dot-asLegendCaptionSubset.Rd b/man/dot-asLegendCaptionSubset.Rd new file mode 100644 index 00000000..be7e2f63 --- /dev/null +++ b/man/dot-asLegendCaptionSubset.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities-mapping.R +\name{.asLegendCaptionSubset} +\alias{.asLegendCaptionSubset} +\title{.asLegendCaptionSubset} +\usage{ +.asLegendCaptionSubset(labels, unit = NULL) +} +\arguments{ +\item{labels}{} + +\item{unit}{A character added as unit to label} +} +\description{ +Creates default legend captions subset +} +\keyword{internal} diff --git a/man/getDefaultCaptions.Rd b/man/getDefaultCaptions.Rd index af7de7c1..f6adb325 100644 --- a/man/getDefaultCaptions.Rd +++ b/man/getDefaultCaptions.Rd @@ -4,7 +4,12 @@ \alias{getDefaultCaptions} \title{getDefaultCaptions} \usage{ -getDefaultCaptions(data, metaData, variableList = colnames(data), sep = "-") +getDefaultCaptions( + data, + metaData = NULL, + variableList = colnames(data), + sep = "-" +) } \arguments{ \item{data}{data.frame used for legend caption}