This is old, I dont think I need any of this relative to 
04b-new 





# 04b googleVis - actual graphing.r

#    ----------------------------------------------
#
#    M <- gvisMotionChart(Fruits, idvar="Fruit", timevar="Year")
#    M2 <- gvisMotionChart(Fruits, idvar="Fruit", timevar="Year")
#    
#    fb[, Cid_Age_Gend := paste(CampaignID, Age, Gender, AdID, sep="_")]
#    Fg.s <- gvisMotionChart(fb, idvar="Cid_Age_Gend", timevar="Date")
#    plot(Fg.s)
#    
#    ----------------------------------------------
#    
#    Idea: 
#      (1) Create Several aggregates
#         - by Campaign  (agg over Age & Gender  ... & Ad)
#         - by Age       (agg over Gender & Campaign  ... & Ad)
#         - by Gender    (agg over Age & Campaign  ... & Ad)
#      (2) Add Calculations to each
#         
#    ----------------------------------------------

# ---------------------------------------------------- #
  findDTnm <- function(findThese, namesParsed=get("namesParsed", envir=parent.frame())) {
  ## Given a list of column names, returns the name of the DT that has those values.  
  ##
  ##  Alternatively, we could recreate the name using makeAggName(findThese)
  ##
    ## TODO / NOTE: If sometimes need Date, sometimes not, then use the following 
    # dateNeeded=FALSE
    # NN <- if (dateNeeded) identity else Negate(identity)
    # namesParsed[ ... ] [ NN(hasDate), ...]

    ## Match `findThese` to the actual values in namesParsed$aggdBy
    aggdByAll <- attr(namesParsed, "unique.aggdBy")
    if(is.null(aggdByAll))
      aggdByAll <- namesParsed[, removeNA(unique(aggdBy))]
    
    findThese <- aggdByAll[match(tolower(findThese), tolower(attr(namesParsed, "unique.aggdBy")))]

    namesParsed[.(findThese)][, .N, by=DT.nm][N==length(findThese), DT.nm]    
  }
DT <- produceDT(byCols=byCols, AddTotalFor=AddTotalFor, useDate=useDate, DTnmsList=fbDTnms)
# ---------------------------------------------------- #

~~~~~~~~~~~~~~~~~~~~

### GOAL: Iterate over every set of aggdBy in namesParsed and for each one 
##          create the appropriate graph object (saved to G[[.]] )
##        Addl 1:  Add the TOTAL values for specific fields
##        Addl 2:  Clean the Gender column
##       

## Create namesParsed
keepOnlyThoseWithDates <- TRUE
namesParsed <- createNamesParsed(fbDTnms, keepOnlyThoseWithDates=keepOnlyThoseWithDates)

# -   ## If needed, can use
# -   namesParsed[, list(list(aggdBy)), by=DT.nm]

## Collect all of the unique aggdBy values
aggdByVlues <- attr(namesParsed, "unique.aggdBy")

## Create index list out of all the possible combinations
categ.cols.using        <- createIndsMatrix(aggdByVlues)
names(categ.cols.using) <- lapply(categ.cols.using, makeAggName)

## POSSIBLE TODO:   I might need to create a new table for each combination of "TOTALS"
ALL.DTs <- lapply(categ.cols.using, getDTwithTotals, removeUnknownGend=TRUE, addGVisID=TRUE)

dimCompare(ALL.DTs, sort="Cr")
stopifnot(all(sapply(ALL.DTs, function(x) "GVisID" %in% names(x) )))

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Params
idvar   = "GVisID"
timevar = "Date"

GVisPlots <- list()
chartids  <- list()
seconds   <- any(duplicated(names(ALL.DTs))) # the time stamp needs seconds only if there are duplicated values. Otherwise, minutes is fine. 
for(nm in names(ALL.DTs)[1:2] ) {
  DT <- ALL.DTs[[nm]]
  chartid <- timeStamp(nm, seconds=seconds) 

  ## The javascript goes wacky if there are dots ('.') in variable names. 
  ##  They should already be removed, but just in case, we make sure they are. 
  chartid <- noDot(chartid)
  setnames(DT, noDot(names(DT)))

  GVisPlots[[nm]] <- gvisMotionChart(DT
        , idvar=idvar, timevar=timevar, chartid=chartid )

  # bank the chartid
  chartids[[nm]] <- chartid
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~








~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

## Search for values
findThese <- c("campaign", "age")
DT.nms <- findDTnm(findThese, namesParsed)

DT <- produceDT(byCols=byCols, AddTotalFor=AddTotalFor, useDate=useDate, DTnmsList=fbDTnms)





~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

useDate = TRUE
byCols  = c("Gender")
AddTotalFor = NULL # "Gender"

idvar   <- ".GVisID"
timevar <- "Date"

byCols <- ifelse(byCols=="Campaign", "CampaignID", byCols)
byCols <- ifelse(byCols=="Ad", "AdID", byCols)

## Grab / Create the DT
DT <- produceDT(byCols=byCols, AddTotalFor=AddTotalFor, useDate=useDate, DTnmsList=fbDTnms)

## Extract some Attributes
aggvars <- attributes(DT)$aggInfo$aggdBy.matched
chartid <- attributes(DT)$aggInfo$DTname
chartid <- timeStamp(chartid, seconds=is.null(chartid))

# Error check
if (length(idvar) != 1)
  stop("`idvar` does not have length of 1. The values are:\n\t", paste(idvar, collapse="\t") )

## Convert to DF
DF <- as.data.frame(DT)

## TODO: Once I have a dict of colnames, this is not needed
setattr(DF, "names", noDot(names(DF)))

## Not sure if this is needed or not? 
DF <- data.frame(DF)
# ... shouldnt be needed

# Ensure no dots in the var values
idvar   <- noDot(idvar)
timevar <- noDot(timevar)
chartid <- noDot(chartid)

## TODO: List
Fg.s <- gvisMotionChart(DF
        , idvar=idvar, timevar="Date", chartid=chartid)
plot(Fg.s)

f.out <- as.path(outDir, chartid, ext="html")
print(G, file=f.out)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

listOfDTnms <- grep("date", fbDTnms, ignore.case=TRUE, value=TRUE)[1:4] 
idvar <- "GVisID"
dateVar <- "Date"
G <- list()
for (nm in listOfDTnms) {
  DT <- get(nm)
  chartid <- timeStamp(nm, seconds=FALSE)
  DT <- get(nm)
  DF <- as.data.frame(DT)

  chartid <- noDot(chartid)
  newnms <- noDot(names(DT))
  setnames(DT, newnms)
  DF <- as.data.frame(DT)
  DF <- data.frame(DF)


  G <- gvisMotionChart(DF, idvar=idvar, timevar=timevar, chartid=chartid)
  plot(G)
}


