Issue 30: Invalid handling in gvisMotionChart of varname parameters
Status:  Fixed
Owner: ----
Closed:  Jan 2014
Reported by dant...@gmail.com, Jan 8, 2014
What steps will reproduce the problem?

library(googleVis)
data(Fruits)
Fruits2 <- Fruits[,c(1,2,4)]

# OK results - sets XVAR and YVAR to remaining column "Sales"
M <- gvisMotionChart(Fruits2, idvar="Fruit", timevar="Year")
plot(M)

# Mishandle data.frame columms - sets both XVAR and YVAR to "Year",
# & "Sales" is not avail for y-axis in the motion chart
M <- gvisMotionChart(Fruits2, idvar="Fruit", timevar="Year", xvar="Year")
plot(M)

# Mishandles data.frame columns & produces ERROR
M <- gvisMotionChart(Fruits2, idvar="Fruit", timevar="Year", xvar="Year", yvar="Sales")
# plot(M) # DO NOT plot, since error result


What is the expected output? What do you see instead?

Error in `[.data.frame`(data, , swap.cols) : undefined columns selected

What version of the product are you using? On what operating system?

> versionInfo() # RStudio
$version
[1] ‘0.97.551’

$mode
[1] "server"

> version # R
               _                           
platform       x86_64-redhat-linux-gnu     
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          3                           
minor          0.1                         
year           2013                        
month          05                          
day            16                          
svn rev        62743                       
language       R                           
version.string R version 3.0.1 (2013-05-16)
nickname       Good Sport

bash-4.1$ uname --kernel-release
2.6.32-279.1.1.el6.x86_64
bash-4.1$ uname --operating-system
GNU/Linux


Please provide any additional information below.

Code excerpt below is from gvisMotionChart(). This approach of cycling through var names specified in parameters cannot work whenever a data.frame col-name is re-used. Such re-use is completely legitimate interactively in the Motion Chart (e.g., for axes, color, size). However, in the R function code, this match will always mix up the columns:


    my.type <- "MotionChart"
    dataName <- deparse(substitute(data))
    other.vars <- c(idvar, timevar, xvar, yvar, colorvar, sizevar)
    pos <- rep(NA, 6)
    for (i in 1:6) {
        if (other.vars[i] != "") {
            pos[i] <- match(other.vars[i], names(data))
            if (is.na(pos[i])) 
                stop(paste("Column", other.vars[i], "does not exist."))
            if (pos[i] != i) {
                swap.cols <- c(pos[i], i)
                nms <- names(data[, swap.cols])
                data[, rev(swap.cols)] <- data[, swap.cols]
                names(data)[rev(swap.cols)] <- nms
            }
        }
    }
Jan 11, 2014
Project Member #1 markus.g...@googlemail.com
Thanks for the issue report. I believe I fixed this by replacing the for loop with:

  ## Bring data frame in the right column order
  vars <- c(idvar, timevar, xvar, yvar, colorvar, sizevar)  
  vars.pos <- na.omit(match(vars, names(data)))
  nm <- c(names(data)[vars.pos], names(data)[-vars.pos])
  data <- cbind(data[, vars.pos], data[,-vars.pos])
  names(data) <- nm
  
See also https://code.google.com/p/google-motion-charts-with-r/source/detail?r=384
Status: Fixed
Jan 27, 2014
#2 dant...@gmail.com
Thanks for the preview. Looks like that should force expected col order, and simply bind unreferenced columns on the right. Looking at the resulting "data" data.frame, it looks promising. Looking forward to this release, so I can use this without my group.
ddt