【问题标题】:Trying to get calculate chi-square in R shiny with drop down menu inputs尝试使用下拉菜单输入计算 R 闪亮中的卡方
【发布时间】:2022-01-28 04:11:53
【问题描述】:

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

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

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

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)

【问题讨论】:

  • 你可以试试df %&gt;% {table(.[[input$cat1]], .[[input$cat2]])}

标签: r shiny shinydashboard chi-squared


【解决方案1】:

这行得通吗?

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)

【讨论】:

    猜你喜欢
    • 2019-07-05
    • 2021-05-01
    • 2019-11-26
    • 1970-01-01
    • 2018-11-18
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多