尝试使用下拉菜单输入在 R shiny 中计算卡方

Trying to get calculate chi-square in R shiny with drop down menu inputs

我正在尝试根据用户从数据框中选择的列来计算卡方,但我不断收到以下错误:

-xtfrm.data.frame(x) 中的警告:无法 xtfrm 数据帧

-table 中的错误:所有参数必须具有相同的长度。

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)

这个有用吗?

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)