ululalbab / google-motion-charts-with-r

Automatically exported from code.google.com/p/google-motion-charts-with-r
0 stars 0 forks source link

Invalid handling in gvisMotionChart of varname parameters #30

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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
            }
        }
    }

Original issue reported on code.google.com by dant...@gmail.com on 8 Jan 2014 at 10:51

GoogleCodeExporter commented 9 years ago
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

Original comment by markus.g...@googlemail.com on 11 Jan 2014 at 6:30

GoogleCodeExporter commented 9 years ago
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

Original comment by dant...@gmail.com on 27 Jan 2014 at 5:13