Skip to content

Commit

Permalink
Create function for setting chart error bars
Browse files Browse the repository at this point in the history
  • Loading branch information
katieb1 committed Apr 25, 2024
1 parent 7f825e5 commit 3a33200
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Collate:
'chart.R'
'chartData.R'
'chartDisagg.R'
'chartErrorBar.R'
'chartId.R'
'chartInclude.R'
'chartInfo.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export(backup)
export(chart)
export(chartData)
export(chartDisagg)
export(chartErrorBar)
export(chartId)
export(chartInclude)
export(chartInfo)
Expand Down
83 changes: 83 additions & 0 deletions R/chartErrorBar.R
Original file line number Diff line number Diff line change
@@ -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)
})
48 changes: 48 additions & 0 deletions man/chartErrorBar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3a33200

Please sign in to comment.