1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
range_prop <- function(x, name) {
if (is.null(x)) return(list())
# Character vector always left as is
if (is.character(x)) {
return(named_list(name, x))
}
assert_that(is.numeric(x), length(x) <= 2)
n_miss <- sum(is.na(x))
if (n_miss == 0) {
named_list(name, x)
} else if (n_miss == 1) {
if (is.na(x[1])) {
named_list(paste0(name, "Max"), x[2])
} else {
named_list(paste0(name, "Min"), x[1])
}
} else if (n_miss == 2) {
list()
}
}
named_list <- function(names, ...) {
stats::setNames(list(...), names)
}
#' Convert the name of a property to the name of its default scale.
#'
#' This is mainly used to ensure that similar properties share the same
#' scale by default - e.g. \code{x} and \code{x2} should use the same
#' scale.
#'
#' @param prop character vector of property names. Any unrecognised names
#' are left unchanged.
#' @return character vector of default scale names.
#' @keywords internal
#' @export
#' @examples
#' propname_to_scale(c("x", "x2"))
#' propname_to_scale(c("foo", "bar"))
#' propname_to_scale(c("opacity", "fillOpacity", "strokeOpacity"))
propname_to_scale <- function(prop) {
simplify <- c(
"x2" = "x",
"width" = "x",
"y2" = "y",
"height" = "y",
"fillOpacity" = "opacity",
"strokeOpacity" = "opacity",
"innerRadius" = "radius",
"outerRadius" = "radius",
"startAngle" = "angle",
"endAngle" = "angle"
)
matches <- match(prop, names(simplify))
prop[!is.na(matches)] <- simplify[prop[!is.na(matches)]]
prop
}
#' Given the type of a ggvis scale, get the name of its corresponding vega scale
#'
#' @param type property type: numeric, ordinal, nominal, logical or datetime.
#' @keywords internal
scaletype_to_vega_scaletype <- function(type) {
unname(c(
"numeric" = "quantitative",
"ordinal" = "ordinal",
"nominal" = "ordinal",
"logical" = "ordinal",
"datetime" = "time"
)[type])
}
|