I'm trying to build a Shiny app that does a 1-way ANOVA test on variables the user can select. I've created a dummy dataset to illustrate the error with just 1 continuous and 1 categorical variable.
There error I get is: contrasts can be applied only to factors with 2 or more levels
When I run aov(contVars ~ factorVars, testtable) on it's own, I get the expected output. However, when this is put into the Shiny app, it's as if the factor vector type is not being recognized.
#load libraries
library(shiny)
testtable <- data.frame(externalVars = round(runif(20)*10,0), factorVars = gl(5,4))
#functions here
ui <- fluidPage(
column(3,
uiOutput("selectfactorVars"),
uiOutput("selectcontVars")
), # close column
column(9,
h3("ANOVA results"),
uiOutput("displayOutput")
) # close column
) # close fluidPage
server <- function(input, output, session){
output$selectfactorVars <- renderUI({
selectInput(
inputId="factorVars",
label="Select continuous variables",
choices=names(testtable)
) # close selectInput
}) # close renderUI
output$selectcontVars <- renderUI({
selectInput(
inputId="contVars",
label="Select continuous variables",
choices=names(testtable)
) # close selectInput
}) # close renderUI
render.react <- reactive({
# this is where the error occurs, works fine if I just print the variables
fit <- stats::aov(input$contVars ~ input$factorVars, testtable)
}) # close reactive
output$displayOutput <- renderPrint({
render.react()
}) # close renderPrint
} # close server
shinyApp(ui=ui, server=server)