Skip to content

Support as.Date() conversion within ggplot code #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 23, 2015
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions R/trace_generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@ layer2traces <- function(l, d, misc) {

## For non-numeric data on the axes, we should take the values from
## the original data.
for (axis.name in c("x", "y")){
if (!misc$is.continuous[[axis.name]]){
for (axis.name in c("x", "y")) {
if (!misc$is.continuous[[axis.name]]) {
aes.names <- paste0(axis.name, c("", "end", "min", "max"))
aes.used <- aes.names[aes.names %in% names(g$aes)]
for(a in aes.used) {
col.name <- g$aes[aes.used]
data.vec <- l$data[[col.name]]
dtemp <- l$data[[col.name]]
if (is.null(dtemp)) {
if (!inherits(g$data[[paste0(a, ".name")]], "NULL")) {
# Handle the case where as.Date() is passed in aes argument.
if (class(g$data[[a]]) != class(g$data[[paste0(a, ".name")]])) {
g$data[[a]] <- g$data[[paste0(a, ".name")]]
data.vec <- g$data[[a]]
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two if statements without an else. is something bad going to happen if data.vec isn't set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, well, if data.vec is not set, it's NULL and it's still okay because I try() in subsequent operations (https://github.com/ropensci/plotly/blob/master/R/trace_generation.R#L56, etc.). So the exceptions are handled.

My initial intention was to leave line 50

data.vec <- l$data[[col.name]]

and follow with

if (is.null(data.vec)) {
  if (!inherits(g$data[[paste0(a, ".name")]], "NULL") && (class(g$data[[a]]) != class(g$data[[paste0(a, ".name")]]))) {
    g$data[[a]] <- g$data[[paste0(a, ".name")]]
    data.vec <- g$data[[a]]
  }
}

The else being, well, leave it alone (it will remain NULL). Do you think this would convey more meaning?

The two ifs really mean one condition (i.e., the two columns are of different non-null classes) but I split them for readability, also to save one check if the first condition is not even met.

} else {
data.vec <- dtemp
}

# For some plot types, we overwrite `data` with `prestats.data`.
pdata.vec <- misc$prestats.data[[a]]
Expand Down Expand Up @@ -274,11 +285,11 @@ toBasic <- list(
g
}
},
path=function(g){
path=function(g) {
group2NA(g, "path")
},
line=function(g){
g$data <- g$data[order(g$data$x),]
line=function(g) {
g$data <- g$data[order(g$data$x), ]
group2NA(g, "path")
},
boxplot=function(g) {
Expand Down Expand Up @@ -352,14 +363,14 @@ toBasic <- list(
#' @export
#' @return list of geom info.
#' @author Toby Dylan Hocking
group2NA <- function(g, geom){
group2NA <- function(g, geom) {
poly.list <- split(g$data, g$data$group)
is.group <- names(g$data) == "group"
poly.na.df <- data.frame()
for(i in seq_along(poly.list)){
no.group <- poly.list[[i]][,!is.group,drop=FALSE]
na.row <- no.group[1,]
na.row[,c("x", "y")] <- NA
for (i in seq_along(poly.list)) {
no.group <- poly.list[[i]][, !is.group, drop=FALSE]
na.row <- no.group[1, ]
na.row[, c("x", "y")] <- NA
poly.na.df <- rbind(poly.na.df, no.group, na.row)
}
g$data <- poly.na.df
Expand All @@ -370,7 +381,7 @@ group2NA <- function(g, geom){

# Convert basic geoms to traces.
geom2trace <- list(
path=function(data, params){
path=function(data, params) {
list(x=data$x,
y=data$y,
name=params$name,
Expand Down