options(rgl.useNULL=TRUE)

pckg = c("shiny","shinyRGL") 

# 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)
}


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



shinyUI(fluidPage(
  titlePanel("Principal Component Analysis (PCA)"), 
  sidebarLayout(
    sidebarPanel(
      selectInput("xvar",
                  label ="Choose variable x",
                  choices = nameX,
                  selected = nameX[1]),
      selectInput("yvar",
                  label ="Choose variable y",
                  choices = nameX,
                  selected = nameX[2]),
      selectInput("zvar",
                  label ="Choose variable z",
                  choices = nameX,
                  selected = nameX[3]),
      checkboxInput("labels",
                    label = "Draw Principal Component vectors",
                    value = TRUE),
      helpText("You can rotate the 3D plot in whichever direction,",
               " and also zoom in/out with your scrolling interface,",
               " such as a mouse or a trackpad.")
    ),
    mainPanel(
      webGLOutput('My3DPlot')
      )
    )
  )
)

