I am trying to calculate chi-square based on the user selecting columns from the data frame but I keep getting the following errors:
-Warning in xtfrm.data.frame(x) : cannot xtfrm data frames
-Error in table: all arguments must have the same length.
library(shinydashboard)
library(shiny)
library(dplyr)
library(DT)
df<-data.frame(HairEyeColor)
Cat1.Variables <- c('Sex','Eye','Hair','Freq')
Cat2.Variables <- c('Eye','Hair','Sex','Freq')
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput('cat1', choices = Cat1.Variables, label = 'Select Category options:'),
selectInput('cat2', choices = Cat2.Variables, label = 'Select Category2 options:')
),
mainPanel(
tableOutput('results')
)
)
)
server=shinyServer(function(input, output) {
datachisquare <- reactive({
req(input$cat1,input$cat2)
df %>% table(list(.data[[input$cat1]]),list(.data[[input$cat2]]))
})
output$results <- renderPrint({
cat(sprintf("\nThe results Chisquare Test are\n"))
print(chisq.test(datachisquare()))})
})
shinyApp(ui, server)
CodePudding user response:
Does this work?
library(shinydashboard)
library(shiny)
library(dplyr)
library(DT)
df <- data.frame(HairEyeColor)
Cat1.Variables <- c("Sex", "Eye", "Hair", "Freq")
Cat2.Variables <- c("Eye", "Hair", "Sex", "Freq")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("cat1", choices = Cat1.Variables, label = "Select Category options:"),
selectInput("cat2", choices = Cat2.Variables, label = "Select Category2 options:")
),
mainPanel(
tableOutput("results")
)
)
)
server <- shinyServer(function(input, output) {
datachisquare <- reactive({
req(input$cat1, input$cat2)
df %>%
{
table(.[[input$cat1]], .[[input$cat2]])
}
})
output$results <- renderPrint({
cat(sprintf("\nThe results Chisquare Test are\n"))
print(chisq.test(datachisquare()))
})
})
shinyApp(ui, server)
