apexcharter/R/utils.R

104 lines
1.9 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
}
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-03-03 19:34:07 +01:00
ax$x$ax_opts[[name]] <- utils::modifyList(
x = ax$x$ax_opts[[name]],
val = list(...),
keep.null = TRUE
)
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
#'
#' @noRd
.ax_opt2 <- function(ax, name, l) {
if (is.null(ax$x$ax_opts[[name]])) {
ax$x$ax_opts[[name]] <- l
} else {
2020-03-03 19:34:07 +01:00
ax$x$ax_opts[[name]] <- utils::modifyList(
x = ax$x$ax_opts[[name]],
val = l,
keep.null = TRUE
)
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]]
}
2018-07-31 22:56:51 +02:00