
options(rgl.useNULL=TRUE)

# server.R
 palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
  "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
 options(rgl.useNULL=TRUE)
  ## Names of libraries to install and load
 pckg = c("UsingR","rgl","devtools","shiny","shinyRGL","rglwidget") 
 # Function that install a packege if this not exist
 is.installed <- function(mypkg){
   is.element(mypkg, installed.packages()[,1])
 } 
 for(i in 1:length(pckg)) {
   if (!is.installed(pckg[i])){
     install.packages(pckg[i])
   }
 }
 # Load a library if this is not loeaded
 for(i in 1:length(pckg)) {
   require(package = pckg[i], character.only = TRUE)
 }

# load data
 numVar = 3
 nameX = matrix(0,numVar,1)
 for (j in 1:numVar) {
   nameX[j,1] = paste(c("X ",as.character(j)), collapse = '')
 } 

 xdata = mvrnorm(n = 2000,c(-5,-4,-3),matrix(c(2,1.9,1.5,1.9,7,0.9,1.5,0.9,6),3,3))
 

 library(UsingR)
 library(rgl)
 library(devtools)
 library(shiny)
 library(shinyRGL)

 shinyServer(
   function(input, output, session) {
     #output$text1 <- renderText({x()                   })
     #output$text2 <- renderText({x() + as.numeric(input$text2)})
     #output$xmean <- renderText({paste(input$xvar, " mean: ",
     #                                  mean(mtcars[ ,input$xvar]))})
     #output$ymean <- renderText({paste(input$yvar, " mean: ",
     #                                  mean(mtcars[ ,input$yvar]))})
     #output$zmean <- renderText({paste(input$zvar, " mean: ",
     #                                  mean(mtcars[ ,input$zvar]))})
     output$Documentation <- renderUI({
       str0 <- paste("Documentation: How to proceed")
       str1 <- paste("- Select variables to render")
       str2 <- paste("- Rotate (orbit) the axis dragging with your mouse")
       str3 <- paste("- Zoom in/out scrolling with your mouse or zooming with trackpads")
       str4 <- paste("- Choose whether to show the car model labels or not (checkbox)")
       HTML(paste(str0, str1, str2, str3, sep= "<br/>"))
     })
     
     selectedData <- reactive({
       ## Generate random samples from num Gaussians specified by the user
       prova = input$Sample
       aux = seq(1:length(nameX))
       sel = aux[nameX==prova]
       selectedData = data.frame(X()[[sel,1]])
     })
     
     output$My3DPlot <- renderWebGL({
       #output$names <- names(mtcars)
       aux = seq(1:length(nameX))
       xvar <- xdata[,aux[nameX==input$xvar]]
       yvar <- xdata[,aux[nameX==input$yvar]]
       zvar <- xdata[,aux[nameX==input$zvar]]
       points3d(xvar,
                yvar,
                zvar,
                col = "blue", radius = 6, lwd=0, top=T)
       axes3d()
       #mean point
       points3d(xvar,
                yvar,
                zvar,
                col = "dark green", radius = 12, lwd=0, top=T)
       text3d(mean(xvar),
              mean(yvar),
              mean(zvar),
              c("mean","mean", "mean"),
              cex = 0.5, adj = 0.5, col="dark green")
       
       title3d("Drag to orbit. Scroll to zoom!")
#        aspect3d(1,1,1)
#        if(input$labels==TRUE){
#          text3d(mtcars[ , xvar],
#                 mtcars[ , yvar],
#                 mtcars[ , zvar],
#                 c(rownames(mtcars),
#                   rownames(mtcars),
#                   rownames(mtcars)), cex = 0.5, adj = 0.5)
#        } else {
#          
#        }
     })
   }
 )
 
 
# function(input, output) {
# 
# 
#   # Combine the selected variables into a new data frame
#     selectedData <- reactive({
#     ## Generate random samples from num Gaussians specified by the user
#     prova = input$Sample
#     aux = seq(1:length(nameX))
#     sel = aux[nameX==prova]
#     selectedData = data.frame(X()[[sel,1]])
#   })
# 
#   clusters <- reactive({
#     Mclust(selectedData())
#   })
#   
#   output$plot2 <- renderPlot({
#     plot(selectedData(), pch = 20, cex = 3,main = paste("2D points from ", as.character(input$NumGaussians)," groups", sep = ""))
#   })
#   output$plot1 <- renderPlot({
#     plot(selectedData(), col=clusters()$classification, pch = 20, cex = 3,
#          main = "Automatic clustering solution")
#     points(t(clusters()$parameters$mean), pch = 4, cex = 4, lwd = 4)
#   })
# }