  ## in term: 
  #    R -e "shiny::runApp('~/git/orch/src/MktingReport/FBShiny_app')"
  #    sudo R -e "shiny::runApp('/mnt/data/rprojs/FBShiny_app')"

  # install.packages("data.table", repos="http://lib.stat.cmu.edu/R/CRAN/")

  suppressPackageStartupMessages({
    library(googleVis)
    library(data.table)
    library(shiny)
  })


  ## TODO: Import function mutualToRows & dependencies

  source("funcsForShiny.r")
  source("global.r", local=FALSE)
#   browser()

  # ----------------------- #

  qtl = 0.99   # quantile
  howManyTopBot <- 3

  ## If qtls was not manually set
  qtls <- sort(c(qtl, 1-qtl))

  # ~~~~ 
  categ = "Label"   #  <~~~~ THIS SHOULD BE INCOMING

  getExtremesInfo(categ, qtls, howManyTopBot)

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

#  key(C.DT)
#  C.DT[]


#    Q[.(Tops)]
#    merge(Tops, Q, by=key(Tops))
#    DT[, keyby=key(C.DT)][.(Tops)]
#    C.DT[.(Tops)]



## Calculate 

if (FALSE)  {
## THIS WILL COME IN HANDY: 
       shinyServer(function(input, output) {
         currentFib         <- reactive({ fib(as.numeric(input$n)) })
     
         output$nthValue    <- renderText({ currentFib() })
         output$nthValueInv <- renderText({ 1 / currentFib() })
       })

   # : WILL NOT WORK 
   # : -------------------------------------------
      shinyServer(function(input, output) {
        # Will give error
        currentFib      <- fib(as.numeric(input$n))
        output$nthValue <- renderText({ currentFib })
      })
   # : 
   # : WILL WORK, but does not save execution time. 
   # : -------------------------------------------  
   # : On the other hand, if currentFib is a function that accesses 
   # :     a reactive value, and that function is called within the
   # :     reactive world, then it will work:
   # : 
      shinyServer(function(input, output) {
        # OK, as long as this is called from the reactive world:
        currentFib <- function() {
          fib(as.numeric(input$n))
        }
      
        output$nthValue <- renderText({ currentFib() })
      })

}


 # server.R
 shinyServer(function(input, output) {
  campSelected <- reactive({
    input$CID
  })
  datasetInput <- reactive({
    cat("This is what i got: \n", input$dataset, "\n")
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })
    output$GVis <- renderGvis({
#      gvisScatterChart(datasetInput())
      cat("\nHello there, I am using: \t", input$dataset, "    ~~~~~~~~~ \n")
      cat(" is in: ", input$dataset %in% names(GPlots), "\n")
       GPlots[[input$dataset]]
  })
    output$CInfo <- renderText({
#      "hello world"
      if (is.null(input$CID))
        return ()
      outputCampaign(C.DT, as.character(input$CID))
  })

  output$InspectThese <- renderText({
      notUsing <- c("Gender", "Age")
      using <- setdiff(input$dataset, notUsing)

      if (!length(using))
           return(NULL)
      getExtremesInfo(using, qtls, howManyTopBot)
  })
    
})

# runShiny("FBShiny_app")





# CID <- 6007006510053
# 
# CID <- as.character(CID)
# C.DT[.(CID)]
# 
# D <- C.DT[.(CID)]
