Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Cleaner handling of var names
Browse files Browse the repository at this point in the history
This commit separates the conversion of an expression (a name or call)
to a string, and the escaping of '.' for safe vega vars. This makes it
possible to use correct titles for axes legends when the data column
has a '.' in it.
  • Loading branch information
wch committed Aug 26, 2014
1 parent 1cb1672 commit 645d6d7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions R/flatten.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ combine_data_props <- function(mark) {

# Remove duplicates, and props that don't appear in the data
lapply(props_by_id, function(props) {
names <- vapply(props, prop_label, character(1))
names <- safe_vega_var(vapply(props, prop_label, character(1)))
ok <- !duplicated(names) & names != ""

setNames(props[ok], names[ok])
Expand Down Expand Up @@ -62,7 +62,7 @@ apply_props <- function(data, props) {
#' @export
apply_props.data.frame <- function(data, props) {
cols <- lapply(props, prop_value, data = data)
names(cols) <- vapply(props, prop_label, character(1))
names(cols) <- safe_vega_var(vapply(props, prop_label, character(1)))
quickdf(cols)
}

Expand Down
2 changes: 1 addition & 1 deletion R/ggvis.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ register_scales_from_props <- function(vis, props) {
add_scale_from_prop <- function(vis, prop) {
# Automatically add label, unless it's blank or has a trailing '_'
label <- prop_label(prop)
if (label == "" || grepl("_$", prop_label(prop))) {
if (label == "" || grepl("_$", label)) {
label <- NULL
}

Expand Down
8 changes: 4 additions & 4 deletions R/prop.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ prop_label <- function(x) UseMethod("prop_label")
#' @export
prop_label.prop_constant <- function(x) ""
#' @export
prop_label.prop_variable <- function(x) safe_vega_var(x$value)
prop_label.prop_variable <- function(x) as_char(x$value)
#' @export
prop_label.prop_reactive <- function(x) safe_vega_var(reactive_id(x$value))
prop_label.prop_reactive <- function(x) as_char(reactive_id(x$value))

# Reports whether this is a scaled prop
prop_is_scaled <- function(prop) !is.null(prop$scale)
Expand All @@ -246,7 +246,7 @@ prop_vega.prop_constant <- function(x, default_scale) {
prop_vega.prop_variable <- function(x, default_scale) {
compact(list(
scale = if (prop_is_scaled(x)) x$scale,
field = paste0("data.", prop_label(x)),
field = paste0("data.", safe_vega_var(prop_label(x))),
mult = x$mult,
offset = x$offset
))
Expand All @@ -269,7 +269,7 @@ prop_domain.prop_constant <- function(x, data) {
prop_domain.prop_variable <- function(x, data) {
list(
data = data,
field = paste0("data.", prop_label(x))
field = paste0("data.", safe_vega_var(prop_label(x)))
)
}
#' @export
Expand Down
15 changes: 9 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,18 @@ param_string <- function(x, collapse = TRUE) {
paste0("(", paste0(names(x), " = ", values, collapse = ", "), ")")
}

# Convert various objects to char strings.
as_char <- function(x) UseMethod("as_char")
#' @export
as_char.name <- function(x) as.character(x)
#' @export
as_char.call <- function(x) deparse2(x)
#' @export
as_char.default <- function(x) stop("Don't know how to convert to string.")

# Given a string, return a string that is safe as a vega variable.
# Replaces . with \.
safe_vega_var <- function(x) {
if (is.name(x)) {
x <- as.character(x)
} else if (is.quoted(x)) {
x <- deparse2(x)
}

gsub(".", "\\.", x, fixed = TRUE)
}

Expand Down
2 changes: 1 addition & 1 deletion R/vega.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ as.vega.mark <- function(mark, in_group = FALSE) {
}

if (!is.null(key)) {
m$key <- paste0("data.", prop_label(key))
m$key <- paste0("data.", safe_vega_var(prop_label(key)))
}
m
}
Expand Down

0 comments on commit 645d6d7

Please sign in to comment.