diff --git a/DESCRIPTION b/DESCRIPTION index 30e06abd..aa40b934 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -53,6 +53,7 @@ Collate: 'chart.R' 'chartData.R' 'chartDisagg.R' + 'chartErrorBar.R' 'chartId.R' 'chartInclude.R' 'chartInfo.R' diff --git a/NAMESPACE b/NAMESPACE index 08e4a175..902d1e2a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -26,6 +26,7 @@ export(backup) export(chart) export(chartData) export(chartDisagg) +export(chartErrorBar) export(chartId) export(chartInclude) export(chartInfo) diff --git a/R/chartErrorBar.R b/R/chartErrorBar.R new file mode 100644 index 00000000..c7c71455 --- /dev/null +++ b/R/chartErrorBar.R @@ -0,0 +1,83 @@ +# Copyright (c) 2024 Apex Resource Management Solution Ltd. (ApexRMS). All rights reserved. +# MIT License +#' @include AAAClassDefinitions.R +NULL + +#' Modify the error bars of a \code{\link{Chart}} +#' +#' Set the type and properties of the error bars of a \code{\link{Chart}}. +#' +#' @param chart \code{\link{Chart}} object +#' @param type character. Type of error bar. Values can be "percentile", +#' "minmax", or "none". Default is "none". +#' @param lower float. If the error bar type is set to "percentile", then +#' sets the minimum percentile for the lower range of the error bar. Default is +#' \code{NULL}. +#' @param upper float. If the error bar type is set to "percentile", then +#' sets the maximum percentile for the upper range of the error bar. Default is +#' \code{NULL}. +#' +#' @return +#' A \code{Chart} object representing a SyncroSim chart +#' +#' @examples +#' \dontrun{ +#' # Open a chart object +#' myChart <- chart(myProject, chart = "My Chart") +#' +#' # Set the chart error bars to display the minimum/maximum of the data +#' myChart <- chartErrorBar(myChart, type = "minmax") +#' +#' # Disable the chart error bars +#' myChart <- chartErrorBar(myChart, type = "none") +#' +#' # Set the chart error bars to display the 95th percentile error bars +#' myChart <- chartErrorBar(myChart, type = "percentile", lower = 2.5, +#' upper = 97.5) +#' } +#' +#' @export +setGeneric("chartErrorBar", function(chart, type = "none", lower = NULL, + upper = NULL) standardGeneric("chartErrorBar")) + +#' @rdname chartErrorBar +setMethod("chartErrorBar", signature(chart = "Chart"), + function(chart, type, lower, upper) { + + # Grab project and chart ID from chart + proj <- .project(chart) + chartCID <- .chartId(chart) + chartDSName <- "core_Chart" + + # Load chart configuration datasheet + ds <- .datasheet(proj, name = chartDSName, optional = T, + returnInvisible = T, includeKey = T) + + # Set error bar type + if (type == "none"){ + ds[ds$ChartId == chartCID,]$ErrorBarType <- "No Ranges" + } else if (type == "minmax"){ + ds[ds$ChartId == chartCID,]$ErrorBarType <- "Min/Max" + } else if (type == "percentile"){ + ds[ds$ChartId == chartCID,]$ErrorBarType <- "Percentile" + } else { + stop(paste("Invalid error bar type:", type)) + } + + # Set min / max percentiles + if (is.numeric(lower)){ + ds[ds$ChartId == chartCID,]$ErrorBarMinPercentile <- lower + } else { + stop("Invalid lower percentile value:", lower) + } + + if (is.numeric(upper)){ + ds[ds$ChartId == chartCID,]$ErrorBarMaxPercentile <- upper + } else { + stop("Invalid upper percentile value:", upper) + } + + saveDatasheet(proj, ds, name = chartDSName, append = FALSE, force = TRUE) + + return(chart) + }) diff --git a/man/chartErrorBar.Rd b/man/chartErrorBar.Rd new file mode 100644 index 00000000..bf668f73 --- /dev/null +++ b/man/chartErrorBar.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/chartErrorBar.R +\name{chartErrorBar} +\alias{chartErrorBar} +\alias{chartErrorBar,Chart-method} +\title{Modify the error bars of a \code{\link{Chart}}} +\usage{ +chartErrorBar(chart, type = "none", lower = NULL, upper = NULL) + +\S4method{chartErrorBar}{Chart}(chart, type = "none", lower = NULL, upper = NULL) +} +\arguments{ +\item{chart}{\code{\link{Chart}} object} + +\item{type}{character. Type of error bar. Values can be "percentile", +"minmax", or "none". Default is "none".} + +\item{lower}{float. If the error bar type is set to "percentile", then +sets the minimum percentile for the lower range of the error bar. Default is +\code{NULL}.} + +\item{upper}{float. If the error bar type is set to "percentile", then +sets the maximum percentile for the upper range of the error bar. Default is +\code{NULL}.} +} +\value{ +A \code{Chart} object representing a SyncroSim chart +} +\description{ +Set the type and properties of the error bars of a \code{\link{Chart}}. +} +\examples{ +\dontrun{ +# Open a chart object +myChart <- chart(myProject, chart = "My Chart") + +# Set the chart error bars to display the minimum/maximum of the data +myChart <- chartErrorBar(myChart, type = "minmax") + +# Disable the chart error bars +myChart <- chartErrorBar(myChart, type = "none") + +# Set the chart error bars to display the 95th percentile error bars +myChart <- chartErrorBar(myChart, type = "percentile", lower = 2.5, + upper = 97.5) +} + +}