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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-31 22:56:51 +02:00
|
|
|
#' Utility function to create ApexChart parameters JSON
|
|
|
|
#'
|
2019-02-14 09:22:57 +01:00
|
|
|
#' @param ax A \code{apexcharts} \code{htmlwidget} object.
|
2018-07-31 22:56:51 +02:00
|
|
|
#' @param name Slot's name to edit
|
|
|
|
#' @param ... Arguments for the slot
|
|
|
|
#'
|
2019-02-14 09:22:57 +01:00
|
|
|
#' @return A \code{apexcharts} \code{htmlwidget} object.
|
2018-07-31 22:56:51 +02:00
|
|
|
#'
|
|
|
|
#' @importFrom utils modifyList
|
|
|
|
#'
|
|
|
|
#' @noRd
|
|
|
|
.ax_opt <- function(ax, name, ...) {
|
|
|
|
|
|
|
|
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(
|
2020-03-03 19:34:07 +01:00
|
|
|
x = ax$x$ax_opts[[name]],
|
|
|
|
val = list(...),
|
|
|
|
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
|
|
|
|
#'
|
2019-02-14 09:22:57 +01:00
|
|
|
#' @param ax A \code{apexcharts} \code{htmlwidget} object.
|
2018-07-31 22:56:51 +02:00
|
|
|
#' @param name Slot's name to edit
|
|
|
|
#' @param l List of arguments for the slot
|
|
|
|
#'
|
2019-02-14 09:22:57 +01:00
|
|
|
#' @return A \code{apexcharts} \code{htmlwidget} object.
|
2018-07-31 22:56:51 +02:00
|
|
|
#'
|
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) {
|
|
|
|
|
|
|
|
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(
|
2020-03-03 19:34:07 +01:00
|
|
|
x = ax$x$ax_opts[[name]],
|
|
|
|
val = l,
|
|
|
|
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)
|
|
|
|
|
|
|
|
if (is.null(fun)) {
|
|
|
|
fun <- get(paste0(generic, ".", class), envir = parent.frame())
|
|
|
|
} else {
|
|
|
|
stopifnot(is.function(fun))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkg %in% loadedNamespaces()) {
|
|
|
|
registerS3method(generic, class, fun, envir = asNamespace(pkg))
|
|
|
|
}
|
|
|
|
|
|
|
|
# Always register hook in case package is later unloaded & reloaded
|
|
|
|
setHook(
|
|
|
|
packageEvent(pkg, "onLoad"),
|
|
|
|
function(...) {
|
|
|
|
registerS3method(generic, class, fun, envir = asNamespace(pkg))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} # nocov end
|