emerson
emerson

Reputation: 55

R shiny basic ANOVA on imported data producing this error: Error in model.frame.default: invalid type (NULL) for variable 'input$var3'

I am very very new to R and R shiny, and I am currently working on a basic shinyapp that will allow the user to import a dataset, then run an anova on the data using selectInputs to determine the independent and dependent variables. However, whenever I try to perform the anova function, I receive this error:

Warning: Error in model.frame.default: invalid type (NULL) for variable 'input$var3'

I have also received this error:

Warning: Error in contrasts<-: contrasts can be applied only to factors with 2 or more levels

I can't figure out why I'm getting this error and most explanations refer to this error in R alone, not R shiny. Again, I am a beginner, so my code may be quite ugly. Any help is very much appreciated!

ui <- fluidPage(theme = shinytheme("cosmo"),

titlePanel("Stats App"),

sidebarLayout(
    sidebarPanel(
        tabsetPanel(type = "tab", 
                    tabPanel("SCI",
                             
                        fileInput("file1", "Insert File", multiple = TRUE, accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),
                            
                        selectInput("statChoice", "Choose Stats", c("None" = "None", "ANOVA 0 w/in 1 btw" = "A1btw", "ANOVA 0 w/in 2 btw" = "A2btw")),
                        
                            conditionalPanel("input.statChoice == 'A2btw'",
                                uiOutput("ind1"),
                                uiOutput("ind2"),
                                uiOutput("dep1")),
                        
            )
        )
    ),
   
    mainPanel(
       tabsetPanel(type = "tab",
                   tabPanel("Data", 
                            tableOutput("fileData")),
                   tabPanel("Summary Statistics",
                            verbatimTextOutput("stats")),
                   tabPanel("Graphs"))
    )
)
)


server <- function(input, output) {

fileData <- eventReactive(input$file1,{
    read.csv(input$file1$datapath, header = TRUE, sep = ",", dec = ".", comment.char = "")
})

output$fileData <- renderTable(
    fileData()
)

vars <- reactive({
    names(fileData())
})

output$ind1 <- renderUI({
    selectInput("var1", "Independent 1", choices = vars())
})

output$ind2 <- renderUI({
    selectInput("var2", "Independent 2", choices = vars())
})

output$dep1 <- renderUI({
    selectInput("var3", "Dependent 1", choices = vars())
})

output$stats <- renderText({
    summary(aov(input$var3 ~ as.factor(input$var1) + as.factor(input$var2), data = fileData()))
})
}


shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 174

Answers (1)

stefan
stefan

Reputation: 125108

There are several issues in your app.

  1. As long as your input vars have not been initialized, i.e. when you start your app and before you choose a "Stats" they are NULL. That's why you get the warning Warning: Error in model.frame.default: invalid type (NULL) for variable 'input$var3'. To prevent this you can add e.g. req(input$var3), ...

  2. After you choose a "Stats" your input variables gets initalized. However, your input vars are simply the names of your vars, i.e. they are character vectors. What you are trying to do is e.g. aov("mpg" ~ as.factor("mpg") + as.factor("mpg"), data = mtcars). As you can check in the R console this will not work. To make this work you have to convert your input vars to "symbols". My approach below simply sets up the formula as a string which I convert to a formula via as.formula.

  3. renderText will not work with model output. To this end use renderPrint.

Try this:

  output$stats <- renderPrint({
    req(input$var1)
    req(input$var2)
    req(input$var3)
    fmla <- paste(input$var3, "~",
                  "as.factor(", input$var1, ")", "+",
                  "as.factor(", input$var2, ")")
    summary(aov(as.formula(fmla), data = fileData()))
  })

Upvotes: 1

Related Questions