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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#' Coerce an ggvis object to a vega list.
#'
#' This generic function powers the coercion of ggvis objects to vega
#' compatible data structures.
#'
#' @param x an object to convert to vega
#' @return a list. When converted to JSON, will be the type of structure
#' that vega expects.
#' @keywords internal
as.vega <- function(x, ...) {
UseMethod("as.vega", x)
}
#' @method as.vega ggvis
#' @export
#' @rdname as.vega
#' @param session a session object from shiny
#' @param dynamic whether to generate dynamic or static spec
as.vega.ggvis <- function(x, session = NULL, dynamic = FALSE, ...) {
# Any changes to default should happen at top-level
x$cur_vis <- NULL
if (length(x$marks) == 0) {
x <- layer_guess(x)
}
data_props <- combine_data_props(x$marks)
data_ids <- names(data_props)
data_table <- x$data[data_ids]
# Collapse each list of scale objects into one scale object.
x <- collapse_scales(x)
scale_data_table <- scale_domain_data(x)
# Wrap each of the reactive data objects in another reactive which returns
# only the columns that are actually used, and adds any calculated columns
# that are used in the props.
data_table <- active_props(data_table, data_props)
# From an environment containing data_table objects, get static data for the
# specified ids.
static_datasets <- function(data_table, ids) {
datasets <- lapply(ids, function(id) {
data <- shiny::isolate(data_table[[id]]())
as.vega(data, id)
})
unlist(datasets, recursive = FALSE)
}
datasets <- static_datasets(data_table, data_ids)
scale_datasets <- static_datasets(scale_data_table, names(scale_data_table))
check_scales_complete(x)
# Each of these operations results in a more completely specified (and still
# valid) ggvis object
x <- add_missing_axes(x)
x <- apply_axes_defaults(x)
x <- add_missing_legends(x)
x <- fortify_legends(x)
x <- apply_legends_defaults(x)
x <- add_default_options(x)
spec <- list(
data = c(datasets, scale_datasets),
scales = lapply(unname(x$scales), as.vega),
marks = lapply(x$marks, as.vega),
legends = compact(lapply(x$legends, as.vega)),
axes = compact(lapply(x$axes, as.vega)),
padding = as.vega(x$options$padding),
ggvis_opts = x$options,
handlers = if (dynamic) x$handlers
)
structure(
spec,
data_table = data_table,
scale_data_table = scale_data_table,
controls = x$controls,
connectors = x$connectors
)
}
gather_scales <- function(x) {
groups <- Filter(is.mark_group, x$marks)
c(x$scales, unlist(pluck(groups, "scales"), recursive = FALSE))
}
#' @export
as.vega.mark_group <- function(x, ...) {
this_scales <- vpluck(x$scales, "name", character(1))
list(
type = "group",
properties = as.vega(x$props),
from = list(data = data_id(x$data)),
marks = lapply(x$marks, as.vega, in_group = TRUE),
scales = lapply(unname(x$scales), as.vega),
legends = lapply(x$legends, as.vega),
axes = lapply(x$axes, as.vega)
)
}
# Given a ggvis mark object, output a vega mark object
#' @export
as.vega.mark <- function(mark, in_group = FALSE) {
data_id <- data_id(mark$data)
# Pull out key from props, if present
key <- mark$props$key
mark$props$key <- NULL
# Add the custom ggvis properties set for storing ggvis-specific information
# in the Vega spec.
properties <- as.vega(mark$props)
properties$ggvis <- list()
properties$ggvis$data <- list(value = data_id)
group_vars <- dplyr::groups(shiny::isolate(mark$data()))
if (!in_group && !is.null(group_vars)) {
# FIXME: probably should go away and just use subvis
# String representation of groups
group_vars <- vapply(group_vars, deparse, character(1))
m <- list(
type = "group",
from = list(data = data_id),
marks = list(
list(
type = mark$type,
properties = properties
)
)
)
} else {
m <- list(
type = mark$type,
properties = properties
)
if (!in_group) {
# If mark inside group, inherits data from parent.
m$from <- list(data = data_id)
}
}
if (!is.null(key)) {
m$key <- paste0("data.", safe_vega_var(prop_label(key)))
}
m
}
#' @export
as.vega.ggvis_props <- function(x, default_scales = NULL) {
x <- prop_event_sets(x)
# Given a list of property sets (enter, update, etc.), return appropriate
# vega property set.
vega_prop_set <- function(x) {
if (empty(x)) return(NULL)
props <- trim_prop_event(names(x))
default_scales <- default_scales %||% propname_to_scale(props)
Map(prop_vega, x, default_scales)
}
lapply(x, vega_prop_set)
}
#' @export
as.vega.ggvis_axis <- function(x) {
if (isTRUE(x$hide)) return(NULL)
if (empty(x$properties)) {
x$properties <- NULL
} else {
x$properties <- as.vega(x$properties)
}
unclass(x)
}
#' @export
as.vega.ggvis_legend <- as.vega.ggvis_axis
#' @export
as.vega.data.frame <- function(x, name, ...) {
# Figure out correct vega parsers for non-string columns
parsers <- drop_nulls(lapply(x, vega_data_parser))
list(list(
name = name,
format = list(
type = "csv",
parse = parsers
),
values = to_csv(x)
))
}
#' @export
as.vega.grouped_df <- function(x, name, ...) {
# Create a flat data set and add a transform-facet data set which uses the
# flat data as a source.
group_vars <- vapply(dplyr::groups(x), deparse, character(1))
res <- as.vega(dplyr::ungroup(x), paste0(name, "_flat"), ...)
res[[length(res) + 1]] <- list(
name = name,
source = paste0(name, "_flat"),
transform = list(list(
type = "treefacet",
keys = as.list(paste0("data.", safe_vega_var(group_vars)))
))
)
res
}
|