From be118e2de8da712f119f5a00fd651ab40881f807 Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Mon, 29 Jan 2024 11:27:56 +0100 Subject: [PATCH] [sparklines] fix logic and extend attributes --- R/helper-functions.R | 41 +++++++++++++++++++++----- man/create_sparklines.Rd | 33 +++++++++++++++++++-- tests/testthat/test-helper-functions.R | 10 +++---- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/R/helper-functions.R b/R/helper-functions.R index 73c3e3841..aa53887c6 100644 --- a/R/helper-functions.R +++ b/R/helper-functions.R @@ -370,13 +370,23 @@ hashPassword <- function(password) { #' @param first highlight first value #' @param last highlight last value #' @param color_series colorSeries -#' @param color_segative colorNegative +#' @param color_negative colorNegative #' @param color_axis colorAxis #' @param color_markers colorMarkers #' @param color_first colorFirst #' @param color_last colorLast #' @param color_high colorHigh -#' @param color_how colorLow +#' @param color_low colorLow +#' @param manual_max manualMax +#' @param manual_min manualMin +#' @param line_weight lineWeight +#' @param date_axis dateAxis +#' @param display_x_axis displayXAxis +#' @param display_hidden displayHidden +#' @param min_axis_type minAxisType +#' @param max_axis_type maxAxisType +#' @param right_to_left rightToLeft +#' @param ... additional arguments #' @return A string containing XML code #' @examples #' # create sparklineGroup @@ -416,6 +426,15 @@ create_sparklines <- function( color_last = wb_color(hex = "FFD00000"), color_high = wb_color(hex = "FFD00000"), color_low = wb_color(hex = "FFD00000"), + manual_max = NULL, + manual_min = NULL, + line_weight = NULL, + date_axis = NULL, + display_x_axis = NULL, + display_hidden = NULL, + min_axis_type = NULL, + max_axis_type = NULL, + right_to_left = NULL, ... ) { @@ -427,14 +446,13 @@ create_sparklines <- function( if (!is.null(type) && !type %in% c("stacked", "column")) stop("type must be NULL, stacked or column") - if (!is.null(markers) && is.null(type)) - stop("markers only work with stacked or column") - + if (!is.null(markers) && as_xml_attr(markers) == "" && !is.null(type) && type %in% c("stacked", "column")) + stop("markers only affect lines `type = NULL`, not stacked or column") sparklineGroup <- xml_node_create( "x14:sparklineGroup", xml_attributes = c( - type = type, + type = type, displayEmptyCellsAs = as_xml_attr(display_empty_cells_as), markers = as_xml_attr(markers), high = as_xml_attr(high), @@ -442,7 +460,16 @@ create_sparklines <- function( first = as_xml_attr(first), last = as_xml_attr(last), negative = as_xml_attr(negative), - "xr2:uid" = sprintf("{6F57B887-24F1-C14A-942C-%s}", random_string(length = 12, pattern = "[A-F0-9]")) + manualMin = as_xml_attr(manual_min), + manualMax = as_xml_attr(manual_max), + lineWeight = as_xml_attr(line_weight), + dateAxis = as_xml_attr(date_axis), + displayXAxis = as_xml_attr(display_x_axis), + displayHidden = as_xml_attr(display_hidden), + minAxisType = as_xml_attr(min_axis_type), + maxAxisType = as_xml_attr(max_axis_type), + rightToLeft = as_xml_attr(right_to_left), + "xr2:uid" = sprintf("{6F57B887-24F1-C14A-942C-%s}", random_string(length = 12, pattern = "[A-F0-9]")) ), xml_children = c( xml_node_create("x14:colorSeries", xml_attributes = color_series), diff --git a/man/create_sparklines.Rd b/man/create_sparklines.Rd index 122a66639..4ab48cb0b 100644 --- a/man/create_sparklines.Rd +++ b/man/create_sparklines.Rd @@ -24,6 +24,15 @@ create_sparklines( color_last = wb_color(hex = "FFD00000"), color_high = wb_color(hex = "FFD00000"), color_low = wb_color(hex = "FFD00000"), + manual_max = NULL, + manual_min = NULL, + line_weight = NULL, + date_axis = NULL, + display_x_axis = NULL, + display_hidden = NULL, + min_axis_type = NULL, + max_axis_type = NULL, + right_to_left = NULL, ... ) } @@ -52,6 +61,8 @@ create_sparklines( \item{color_series}{colorSeries} +\item{color_negative}{colorNegative} + \item{color_axis}{colorAxis} \item{color_markers}{colorMarkers} @@ -62,9 +73,27 @@ create_sparklines( \item{color_high}{colorHigh} -\item{color_segative}{colorNegative} +\item{color_low}{colorLow} + +\item{manual_max}{manualMax} + +\item{manual_min}{manualMin} + +\item{line_weight}{lineWeight} + +\item{date_axis}{dateAxis} + +\item{display_x_axis}{displayXAxis} + +\item{display_hidden}{displayHidden} + +\item{min_axis_type}{minAxisType} + +\item{max_axis_type}{maxAxisType} + +\item{right_to_left}{rightToLeft} -\item{color_how}{colorLow} +\item{...}{additional arguments} } \value{ A string containing XML code diff --git a/tests/testthat/test-helper-functions.R b/tests/testthat/test-helper-functions.R index 5e1e0b432..fc82f2c7d 100644 --- a/tests/testthat/test-helper-functions.R +++ b/tests/testthat/test-helper-functions.R @@ -206,19 +206,19 @@ test_that("add_sparklines", { test_that("more sparkline tests", { - sl1 <- create_sparklines("Sheet 1", "A3:K3", "L3") - sl2 <- create_sparklines("Sheet 1", "A4:K4", "L4", type = "column", markers = TRUE, high = TRUE, low = TRUE) - sl3 <- create_sparklines("Sheet 1", "A5:K5", "L5", type = "stacked", markers = TRUE, display_empty_cells_as = 0) - set.seed(123) # sparklines has a random uri string options("openxlsx2_seed" = NULL) + sl1 <- create_sparklines("Sheet 1", "A3:K3", "L3") + sl2 <- create_sparklines("Sheet 1", "A4:K4", "L4", type = "column", high = TRUE, low = TRUE) + sl3 <- create_sparklines("Sheet 1", "A5:K5", "L5", type = "stacked", display_empty_cells_as = 0) + wb <- wb_workbook() %>% wb_add_worksheet() %>% wb_add_data(x = mtcars) %>% wb_add_sparklines(sparklines = c(sl1, sl2, sl3)) - exp <- "'Sheet 1'!A3:K3L3'Sheet 1'!A4:K4L4'Sheet 1'!A5:K5L5" + exp <- "'Sheet 1'!A3:K3L3'Sheet 1'!A4:K4L4'Sheet 1'!A5:K5L5" got <- wb$worksheets[[1]]$extLst expect_equal(exp, got)