From 096cfddaf232cc6d37756ebb58265cf1ef378ffc Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Mon, 29 Jan 2024 12:24:55 +0100 Subject: [PATCH] [sparklines] improve sparklines (#920) * [sparklines] improve sparklines * [sparklines] fix logic and extend attributes --- R/helper-functions.R | 128 +++++++++++++++---------- man/comment_internal.Rd | 11 +++ man/create_sparklines.Rd | 68 +++++++++---- tests/testthat/test-helper-functions.R | 20 ++++ 4 files changed, 157 insertions(+), 70 deletions(-) diff --git a/R/helper-functions.R b/R/helper-functions.R index 9d9af964d..aa53887c6 100644 --- a/R/helper-functions.R +++ b/R/helper-functions.R @@ -361,22 +361,32 @@ hashPassword <- function(password) { #' @param sheet sheet #' @param dims Cell range of cells used to create the sparklines #' @param sqref Cell range of the destination of the sparklines. -#' @param type type +#' @param type Either `NULL`, `stacked` or `column` #' @param negative negative -#' @param displayEmptyCellsAs displayEmptyCellsAs +#' @param display_empty_cells_as Either `gap`, `span` or `zero` #' @param markers markers add marker to line #' @param high highlight highest value #' @param low highlight lowest value #' @param first highlight first value #' @param last highlight last value -#' @param colorSeries colorSeries -#' @param colorNegative colorNegative -#' @param colorAxis colorAxis -#' @param colorMarkers colorMarkers -#' @param colorFirst colorFirst -#' @param colorLast colorLast -#' @param colorHigh colorHigh -#' @param colorLow colorLow +#' @param color_series colorSeries +#' @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_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 @@ -397,63 +407,79 @@ hashPassword <- function(password) { #' #' @export create_sparklines <- function( - sheet = current_sheet(), + sheet = current_sheet(), dims, sqref, - type = NULL, - negative = NULL, - displayEmptyCellsAs = "gap", # "span", "zero" - markers = NULL, - high = NULL, - low = NULL, - first = NULL, - last = NULL, - colorSeries = wb_color(hex = "FF376092"), - colorNegative = wb_color(hex = "FFD00000"), - colorAxis = wb_color(hex = "FFD00000"), - colorMarkers = wb_color(hex = "FFD00000"), - colorFirst = wb_color(hex = "FFD00000"), - colorLast = wb_color(hex = "FFD00000"), - colorHigh = wb_color(hex = "FFD00000"), - colorLow = wb_color(hex = "FFD00000") + type = NULL, + negative = NULL, + display_empty_cells_as = "gap", # "span", "zero" + markers = NULL, + high = NULL, + low = NULL, + first = NULL, + last = NULL, + color_series = wb_color(hex = "FF376092"), + color_negative = wb_color(hex = "FFD00000"), + color_axis = wb_color(hex = "FFD00000"), + color_markers = wb_color(hex = "FFD00000"), + color_first = wb_color(hex = "FFD00000"), + 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, + ... ) { - # TODO change arguments to snake case? + + standardize_case_names(...) assert_class(dims, "character") assert_class(sqref, "character") - ## FIXME validate_color barks - # colorSeries <- validate_color(colorSeries) - 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, - displayEmptyCellsAs = displayEmptyCellsAs, - markers = markers, - high = high, - low = low, - first = first, - last = last, - negative = negative, - "xr2:uid" = sprintf("{6F57B887-24F1-C14A-942C-%s}", random_string(length = 12, pattern = "[A-F0-9]")) + type = type, + displayEmptyCellsAs = as_xml_attr(display_empty_cells_as), + markers = as_xml_attr(markers), + high = as_xml_attr(high), + low = as_xml_attr(low), + first = as_xml_attr(first), + last = as_xml_attr(last), + negative = as_xml_attr(negative), + 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 = colorSeries), - xml_node_create("x14:colorNegative", xml_attributes = colorNegative), - xml_node_create("x14:colorAxis", xml_attributes = colorAxis), - xml_node_create("x14:colorMarkers", xml_attributes = colorMarkers), - xml_node_create("x14:colorFirst", xml_attributes = colorFirst), - xml_node_create("x14:colorLast", xml_attributes = colorLast), - xml_node_create("x14:colorHigh", xml_attributes = colorHigh), - xml_node_create("x14:colorLow", xml_attributes = colorLow), + xml_node_create("x14:colorSeries", xml_attributes = color_series), + xml_node_create("x14:colorNegative", xml_attributes = color_negative), + xml_node_create("x14:colorAxis", xml_attributes = color_axis), + xml_node_create("x14:colorMarkers", xml_attributes = color_markers), + xml_node_create("x14:colorFirst", xml_attributes = color_first), + xml_node_create("x14:colorLast", xml_attributes = color_last), + xml_node_create("x14:colorHigh", xml_attributes = color_high), + xml_node_create("x14:colorLow", xml_attributes = color_low), xml_node_create( "x14:sparklines", xml_children = c( xml_node_create( diff --git a/man/comment_internal.Rd b/man/comment_internal.Rd index 6319049a8..6a767d4e4 100644 --- a/man/comment_internal.Rd +++ b/man/comment_internal.Rd @@ -63,5 +63,16 @@ wb$add_comment(sheet = 1, dims = "C10", comment = c2) # Works with formatted text also. formatted_text <- fmt_txt("bar", underline = TRUE) wb$add_comment(dims = "B5", comment = formatted_text) +# With background color +wb$add_comment(dims = "B7", comment = formatted_text, color = wb_color("green")) +# With background image. File extension must be png or jpeg, not jpg? +tmp <- tempfile(fileext = ".png") +png(file = tmp, bg = "transparent") +plot(1:10) +rect(1, 5, 3, 7, col = "white") +dev.off() + +c1 <- wb_comment(text = "this is a comment", author = "", visible = TRUE) +wb$add_comment(dims = "B12", comment = c1, file = tmp) } \keyword{internal} diff --git a/man/create_sparklines.Rd b/man/create_sparklines.Rd index 31bfc215b..4ab48cb0b 100644 --- a/man/create_sparklines.Rd +++ b/man/create_sparklines.Rd @@ -10,20 +10,30 @@ create_sparklines( sqref, type = NULL, negative = NULL, - displayEmptyCellsAs = "gap", + display_empty_cells_as = "gap", markers = NULL, high = NULL, low = NULL, first = NULL, last = NULL, - colorSeries = wb_color(hex = "FF376092"), - colorNegative = wb_color(hex = "FFD00000"), - colorAxis = wb_color(hex = "FFD00000"), - colorMarkers = wb_color(hex = "FFD00000"), - colorFirst = wb_color(hex = "FFD00000"), - colorLast = wb_color(hex = "FFD00000"), - colorHigh = wb_color(hex = "FFD00000"), - colorLow = wb_color(hex = "FFD00000") + color_series = wb_color(hex = "FF376092"), + color_negative = wb_color(hex = "FFD00000"), + color_axis = wb_color(hex = "FFD00000"), + color_markers = wb_color(hex = "FFD00000"), + color_first = wb_color(hex = "FFD00000"), + 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, + ... ) } \arguments{ @@ -33,11 +43,11 @@ create_sparklines( \item{sqref}{Cell range of the destination of the sparklines.} -\item{type}{type} +\item{type}{Either \code{NULL}, \code{stacked} or \code{column}} \item{negative}{negative} -\item{displayEmptyCellsAs}{displayEmptyCellsAs} +\item{display_empty_cells_as}{Either \code{gap}, \code{span} or \code{zero}} \item{markers}{markers add marker to line} @@ -49,21 +59,41 @@ create_sparklines( \item{last}{highlight last value} -\item{colorSeries}{colorSeries} +\item{color_series}{colorSeries} -\item{colorNegative}{colorNegative} +\item{color_negative}{colorNegative} -\item{colorAxis}{colorAxis} +\item{color_axis}{colorAxis} -\item{colorMarkers}{colorMarkers} +\item{color_markers}{colorMarkers} -\item{colorFirst}{colorFirst} +\item{color_first}{colorFirst} -\item{colorLast}{colorLast} +\item{color_last}{colorLast} -\item{colorHigh}{colorHigh} +\item{color_high}{colorHigh} -\item{colorLow}{colorLow} +\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{...}{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 50864b8e0..fc82f2c7d 100644 --- a/tests/testthat/test-helper-functions.R +++ b/tests/testthat/test-helper-functions.R @@ -204,6 +204,26 @@ test_that("add_sparklines", { }) +test_that("more sparkline tests", { + + 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" + got <- wb$worksheets[[1]]$extLst + expect_equal(exp, got) + +}) + test_that("distinct() works", { x <- c("London", "NYC", "NYC", "Berlin", "Madrid", "London", "BERLIN", "berlin")