user7353167
user7353167

Reputation:

Dynamic anova in Shiny app, is my input wrong?

data(mtcars)
library(stats)
library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
            selectizeInput("mtcarsid", "Nehme eine MT Cars category.", choices = colnames(mtcars), selected = colnames(mtcars)[2], multiple = FALSE)
      ),
      # Show a plot of the generated distribution
      mainPanel(
         tableOutput("model"),
         textOutput("text123")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$text123 <- renderText({

    })


    output$model <- renderTable ({

        z <- factor(input$mtcarsid)
        # #print(mtcars[[z]])
        # 
        # print(length(mtcars$mpg))
        # 
        # print(length(mtcars[[z]]))

        x <- aov(mpg ~ factor(mtcars[[z]]), data=mtcars) 
        x <- TukeyHSD(x) 
        print(x)
        x <- as.data.frame(x[[1]][,4] > 0.05)
        x
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

This is my App and based on the input of my input$mtcarsid I want to perform an anova incl. a post-hoc test. However, my model seems to totally wrong input with my code. Can anybody tell me why?

aov(mpg ~ factor(cyl), data = mtcars)

This code works fine. But when i just use factor(input$mtcarsid) i get an error that length of input differs (1 vs 32).

Upvotes: 2

Views: 269

Answers (1)

Travis Hinkelman
Travis Hinkelman

Reputation: 164

One solution is to convert the selected variable to a factor outside of the call to aov().

  output$model <- renderTable ({
    mtcars[["selected"]] = factor(mtcars[[input$mtcarsid]])
    x <- aov(mpg ~ selected, data=mtcars) 
    x <- TukeyHSD(x) 
    print(x)
    x <- as.data.frame(x[[1]][,4] > 0.05)
    x
  })

Upvotes: 1

Related Questions