library("shiny")
library("threejs")
library("MASS")

## Run shiny application
#shiny::runApp(launch.browser = TRUE)

# Create variables to perform PCA
set.seed(1)
n = 1500
if(!exists("xdata")) {xdata <- cbind(mvrnorm(n = n,c(5,4,-3),matrix(c(2,1.9,1.5,1.9,7,0.9,1.5,0.9,6),3,3)),mvrnorm(n = n,c(-0.6,2),matrix(c(5,0,0,3),2,2)))
xdata <- cbind(xdata,xdata[,1]*9+xdata[,2]*2+rnorm(n,0,7),-xdata[,3]*2+xdata[,5]*6+rnorm(n,0,9),xdata[,2]*7+xdata[,4]*10+rnorm(n,0,10))}

# Create name for variables
numVar = ncol(xdata)
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(
	h3("Plot controls"),
	# Number of ticks for axis
      numericInput("nticks", "Number of ticks", 6, min = 0, max = 10, step = 1),
	# Num of Colors	to generate vector of colors
	  numericInput("colors", "Number of colors", 1, min = 1, max = 8, step = 1),
	# Size for Canvas representation
      numericInput("sizes", "Number of sizes (only affects canvas renderings)", 1, min = 1, max = 3, step = 1),
      selectInput("renderer", label="Rendering method",
         choices = list("Auto"="auto", "Canvas"="canvas", "WebGL"="webgl"),
         selected = "canvas"),
      checkboxInput("grid", label = "Grid", value = TRUE),
      p("Use the mouse zoom to zoom in/out."),
	  
	# Select data to perform principal component analysis (PCA)
	h3("Data Selection"),
	  selectInput("xvar",
                  label ="Choose variable x",
                  choices = nameX,
                  selected = "X 2"),
      selectInput("yvar",
                  label ="Choose variable x",
                  choices = nameX,
                  selected = "X 4"),
      selectInput("zvar",
                  label ="Choose variable x",
                  choices = nameX,
                  selected = "X 8"),
	p("'",strong("Principal component analysis (PCA)"),"is a statistical procedure that uses an orthogonal 
	  transformation to convert a set of observations of possibly correlated variables into a set of values 
	  of linearly uncorrelated variables called principal components. The number of principal components is 
	  less than or equal to the number of original variables. This transformation is defined in such a way 
	  that the first principal component has the largest possible variance (that is, accounts for as much of 
	  the variability in the data as possible), and each succeeding component in turn has the highest variance 
	  possible under the constraint that it is orthogonal to the preceding components. The resulting vectors are 
	  an uncorrelated orthogonal basis set. The principal components are orthogonal because they are the eigenvectors 
	  of the covariance matrix, which is symmetric. PCA is sensitive to the relative scaling of the original variables.","'")
	),
    mainPanel(
        scatterplotThreeOutput("scatterplot"),
        scatterplotThreeOutput("scatterplotPCA"),
        h4("Rotation matrix"),
        tableOutput("view")
    )
  )
))
