/
ggvega.R
81 lines (69 loc) · 1.92 KB
/
ggvega.R
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
#' Translate ggplot2 object to vegaspec
#'
#' @details
#' To return a single-view spec, rather than a single-layer spec, use
#' `single_view = TRUE`. Of course, this is effective only for ggplot objects
#' that have exactly one layer.
#'
#' @inheritParams vegawidget::as_vegaspec
#' @inheritParams spec2vl
#'
#' @inherit vegawidget::as_vegaspec return
#'
#' @export
#'
as_vegaspec.gg <- function(spec, single_view = FALSE, ...) {
# convert to ggspec
ggspec <- gg2spec(spec)
# convert to vegaspec
spec2vl(ggspec, single_view)
}
#' Translate ggplot2 object into a ggspec
#'
#' @param plt A ggplot2 object.
#'
#' @return `list`, a ggspec list.
#' @export
#'
#' @examples
#' library(ggplot2)
#' p <- ggplot(data = iris)
#' p <- p + geom_point(aes(x = Petal.Width, y = Petal.Length))
#' gg2spec(p)
gg2spec <- function(plt){
int_data <- data_int(plt$data, plt$layers)
int_map <- mapping_spc(plt$mapping)
list(
data = data_spc(int_data),
layers = layer_spc(plt$layers, int_data, int_map),
scales = scale_spc(plt$scales$scales),
labels = plt$labels,
coordinates = coordinates(plt$coordinates),
facet = facet(plt$facet)
)
}
#' Translate ggspec object to vegaspec
#'
#' @param ggspec `list`, a ggspec object
#' @param single_view `boolean`, indicates to collapse an unfaceted
#' single-layer spec to a single-view spec
#'
#' @inherit vegawidget::as_vegaspec return
#'
#' @export
#' @keywords internal
#'
spec2vl <- function(ggspec, single_view = FALSE) {
ct <- V8::v8()
ct$source(system.file("js", "ggvega.js", package = "ggvega"))
ct$assign("ggspec", jsonlite::toJSON(ggspec, auto_unbox = TRUE, null = "null"))
ct$assign("ggspec", V8::JS("JSON.parse(ggspec)"))
singleView <- ifelse(single_view, "true", "false")
vlspec <-
ct$get(
V8::JS(glue::glue("ggvega.spec2vl(ggspec, {singleView})")),
simplifyVector = FALSE,
simplifyDataFrame = FALSE
)
vegawidget::as_vegaspec(vlspec)
}