Shinyapp 中的错误:分组因子必须正好有 2 个级别

问题描述

最近,我尝试学习shinyapp,并尝试为t-test做一个shinyapp:

library(shiny)

shinyUI(fluidPage(

    # Application title
    titlePanel("t test shinyapp"),# Sidebar set
    sidebarLayout(
        sidebarPanel(
            
            h4(fileInput("file","Upload the file"),helpText("Default max. file size is 5MB"),# Horizontal line ----
            tags$hr(),uIoUtput("vx1"),br(),uIoUtput("vx2"),uIoUtput("gg"),selectInput("method","Select t test type",c("One-sample t test","Independent two-sample t test","Paired sample t test"),selected = "Independent two-sample t test"),selectInput("alt","Select alternative",c("two.sided","greater","less"),selected = "two.sided"),textInput("mu","Enter population mean for one-sample t test",""),radioButtons("ptype","Select the file type",choices = list("png","pdf")))),mainPanel(
            uIoUtput("tb")
        )
    )))

shinyServer(function(input,output) {
    
    dff <- reactive({
        file1 <- input$file
        if(is.null(file1)){return()} 
        read.csv(file=file1$datapath,header = TRUE)
        
    })
       
    output$vx1 <- renderUI({
        selectInput("var1","Select variable names",choices = names(dff()))
    })
    
    output$vx2 <- renderUI({
        selectInput("var2",choices = names(dff()))
    })
    

    output$gg <- renderUI({
        selectInput("gro","Select group name",choices = names(dff()))
    })

    
    output$sum <- renderTable({

        if(is.null(dff())){return ()}
        
        print(c(paste0("You choice to do: ",input$method),paste0("The way you use to test is: ",input$alt),paste0("The variable you choice is: ",input$var1),paste0("The group variable you selected is: ",input$gro),paste0("the levels of group variable is:",levels(as.factor(dff()[input$gro])))),sep = "\n")
        
         print(dff()[input$gro])
    })
    
   
    
    output$tr <- renderTable({
        
        if(is.null(dff())){return ()}
        
        if(input$method == "One-sample t test"){
            t.test(dff1[input$var1],mu = as.numeric(input$mu),alternative = input$alt)
            
        } else if(input$method == "Independent two-sample t test"){
            t.test(input$var1 ~ input$gro,data = dff(),alternative = input$alt)
            
        }else{
            t.test(dff()[input$var1],dff()[input$var2],paired=TRUE,alternative = input$alt)
        }
    })
    
    output$tplot <- renderPlot({
        
    })
    
    downloadHandler(
        filename = function(){
            paste(input$method,input$ptype,sep = ".")
        },content = function(){
            if(input$ptype == "png")
                png(file)
            else
                pdf(file)
            
            dev.off()
        }
    )
    
    output$tb <- renderUI({
        if(is.null(data()))
            h5("Powered by",tags$img(src='qrcode.jpg'))
        else
            tabsetPanel(tabPanel("Summary",tableOutput("sum")),tabPanel("t test result",tableOutput("tr")),tabPanel("Plot",downloadButton("download","download the plot"),))
    })
    
    })

如您所见,我可以在 output$sum 中打印 dff()[input$gro],但无法在 output$tr 中运行。

当我运行代码时,出现错误:t.test.formula 中的错误:分组因子必须恰好有 2 个级别。

有什么问题。非常感谢。

解决方法

您可以尝试将 output$tr 代码更改为:

output$tr <- renderTable({
  
  if(is.null(dff())){return ()}
  
  if(input$method == "One-sample t test"){
    t.test(dff1[[input$var1]],mu = as.numeric(input$mu),alternative = input$alt)
  } else if(input$method == "Independent two-sample t test"){
    t.test(reformulate(input$gro,input$var1),data = dff(),alternative = input$alt)
  }else{
    t.test(dff()[[input$var1]],dff()[[input$var2]],paired=TRUE,alternative = input$alt)
  }
})