library(shiny)
shinyServer(function(input, output, session) {
#Allows user to upload a CSV File
getdf <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath)
})
#Creates variables for the columns selected
observe({
updateSelectInput(session, "inSelect", choices = names(getdf()), selected = names(getdf())[1])
updateSelectInput(session, "inSelect2", choices = names(getdf()), selected = names(getdf())[2])
})
#This creates the "Create" button
plot1 <- eventReactive(input$createg, {
splitData1()
splitData2()
})
#We store the CSV file into a dataframe
dataInput <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
else return(data <- read.csv(inFile$datapath))
})
#We create smaller dataframes here
splitData1 <- reactive({
#We check to see if a dataframe was submitted, otherwise ERROR
if(is.null(input$file1)) return(NULL)
#We make dat our dataframe
dat <- dataInput()
#We create a smaller dataframe with only the two columns selected
dat1 <- dat[,c(input$inSelect2, input$inSelect), drop = FALSE]
#We create an even smaller datafram that takes when the grouping variable's numeric value is equal to the first instance
#eg if the grouping variable is gender and the other variable is seed
#This would make the gender variable numeric and just take which variable is equal to 1
#Following our example, this would create a new dataframe with seed and when gender == 1
dat1 <- subset(dat1, as.numeric(dat1[[1]]) == 1)
dat1 <- droplevels(dat1)
#This is the dataframe we return
#This return the column that the user wanted to see with the grouping variable
#eg if the grouping variable is gender and the other variable is seed
#This would just return seed column
dat1 <- dat1[,c(input$inSelect), drop = FALSE]
})
#Second function that creates smaller dataframes
splitData2 <- reactive({
if(is.null(input$file1)) return(NULL)
dat <- dataInput()
dat1 <- dat[,c(input$inSelect2, input$inSelect), drop = FALSE]
#This is the only line that has changed from SplitData2
dat1 <- subset(dat1, as.numeric(dat1[[1]]) == 2)
dat1 <- droplevels(dat1)
dat1 <- dat1[,c(input$inSelect), drop = FALSE]
})
#This creates our plots
output$plot1 <- renderPlot({
plot1() #displays the graph after the create button
#density graphs are not working
#error: argument 'x' must be numeric
browser()
dens <- density(as.numeric(splitData1()[,1]))
plot(dens, col = "red")
dens2 <- density(as.numeric(splitData2()[,1]))
lines(dens, col = "blue")
#only histograms and lines seem to be working
### plot(splitData1(), col = "red") #creates histograms of the first grouping variable
### lines(splitData2(), col = "blue") #creates histograms of the second grouping variable
})
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.