apexcharter/R/utils.R

140 lines
3.0 KiB
R
Raw Normal View History

2018-07-31 22:56:51 +02:00
2020-04-03 16:15:18 +02:00
null_or_empty <- function(x) {
is.null(x) || length(x) == 0
}
dropNullsOrEmpty <- function(x) {
x[!vapply(x, null_or_empty, FUN.VALUE = logical(1))]
}
2018-07-31 22:56:51 +02:00
dropNulls <- function(x) {
x[!vapply(x, is.null, FUN.VALUE = logical(1))]
}
`%||%` <- function(x, y) {
if (!is.null(x)) x else y
}
2019-02-14 15:50:58 +01:00
formatNoSci <- function(x) {
if (is.null(x)) return(NULL)
format(x, scientific = FALSE, digits = 15)
}
2018-07-31 22:56:51 +02:00
2020-03-17 17:52:30 +01:00
ununlist <- function(x) {
2020-03-17 18:05:41 +01:00
if (is.null(x))
return(x)
2020-03-17 17:52:30 +01:00
n <- names(x)
if (!is.null(n) && all(nzchar(n))) {
lapply(x, ununlist)
} else {
unlist(x)
}
}
to_posix <- function(x) {
if (!is.null(x)) {
x <- as.POSIXct(x/1000, origin = "1970-01-01", tz = "UTC")
}
x
}
2021-01-08 10:12:23 +01:00
to_hyphen <- function(x) {
tolower(gsub("([A-Z])", "-\\1", x))
}
make_styles <- function(styles) {
styles <- dropNulls(styles)
if (length(styles) < 1)
return(NULL)
2021-01-08 14:17:17 +01:00
styles <- sprintf("%s: %s;", to_hyphen(names(styles)), unlist(styles, use.names = FALSE))
paste(styles, collapse = " ")
2021-01-08 10:12:23 +01:00
}
2020-03-17 17:52:30 +01:00
2018-07-31 22:56:51 +02:00
#' Utility function to create ApexChart parameters JSON
#'
#' @template ax-default
2018-07-31 22:56:51 +02:00
#' @param name Slot's name to edit
#' @param ... Arguments for the slot
#'
#' @importFrom utils modifyList
#'
#' @noRd
.ax_opt <- function(ax, name, ...) {
2021-01-08 10:12:23 +01:00
2018-07-31 22:56:51 +02:00
if (is.null(ax$x$ax_opts[[name]])) {
ax$x$ax_opts[[name]] <- list(...)
} else {
2020-12-03 17:31:04 +01:00
ax$x$ax_opts[[name]] <- modifyList(
2021-01-08 10:12:23 +01:00
x = ax$x$ax_opts[[name]],
val = list(...),
2020-03-03 19:34:07 +01:00
keep.null = TRUE
)
2018-07-31 22:56:51 +02:00
}
2020-12-03 17:31:04 +01:00
ax$x$ax_opts[[name]] <- dropNullsOrEmpty(ax$x$ax_opts[[name]])
2018-07-31 22:56:51 +02:00
return(ax)
}
#' Utility function to create ApexChart parameters JSON
#'
#' @template ax-default
2018-07-31 22:56:51 +02:00
#' @param name Slot's name to edit
#' @param l List of arguments for the slot
#'
2020-12-03 17:31:04 +01:00
#' @importFrom utils modifyList
#'
2018-07-31 22:56:51 +02:00
#' @noRd
.ax_opt2 <- function(ax, name, l) {
2021-01-08 10:12:23 +01:00
2018-07-31 22:56:51 +02:00
if (is.null(ax$x$ax_opts[[name]])) {
ax$x$ax_opts[[name]] <- l
} else {
2020-12-03 17:31:04 +01:00
ax$x$ax_opts[[name]] <- modifyList(
2021-01-08 10:12:23 +01:00
x = ax$x$ax_opts[[name]],
val = l,
2020-03-03 19:34:07 +01:00
keep.null = TRUE
)
2018-07-31 22:56:51 +02:00
}
2020-12-03 17:31:04 +01:00
ax$x$ax_opts[[name]] <- dropNullsOrEmpty(ax$x$ax_opts[[name]])
2018-07-31 22:56:51 +02:00
return(ax)
}
2019-07-15 12:04:45 +02:00
# Get parameters from an \code{apexcharts} \code{htmlwidget} object.
.get_ax_opt <- function(ax, name) {
ax$x$ax_opts[[name]]
}
2020-12-03 11:03:59 +01:00
# From vignette('knit_print', package = 'knitr')
# and https://github.com/rstudio/htmltools/pull/108/files
2018-07-31 22:56:51 +02:00
2020-12-03 11:03:59 +01:00
register_s3_method <- function(pkg, generic, class, fun = NULL) { # nocov start
stopifnot(is.character(pkg), length(pkg) == 1)
stopifnot(is.character(generic), length(generic) == 1)
stopifnot(is.character(class), length(class) == 1)
2021-01-08 10:12:23 +01:00
2020-12-03 11:03:59 +01:00
if (is.null(fun)) {
fun <- get(paste0(generic, ".", class), envir = parent.frame())
} else {
stopifnot(is.function(fun))
}
2021-01-08 10:12:23 +01:00
2020-12-03 11:03:59 +01:00
if (pkg %in% loadedNamespaces()) {
registerS3method(generic, class, fun, envir = asNamespace(pkg))
}
2021-01-08 10:12:23 +01:00
2020-12-03 11:03:59 +01:00
# Always register hook in case package is later unloaded & reloaded
setHook(
packageEvent(pkg, "onLoad"),
function(...) {
registerS3method(generic, class, fun, envir = asNamespace(pkg))
}
)
} # nocov end