【问题标题】:Error in Shinyapp :grouping factor must have exactly 2 levelsShinyapp 中的错误:分组因子必须恰好有 2 个级别
【发布时间】:2021-01-14 02:57:49
【问题描述】:

最近在尝试学习shinyapp,并尝试做一个shinyapp进行t-test:

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"),
            br(),
            
            uiOutput("gg"),
            br(),
            
            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"),
            br(),
            
            selectInput("alt", "Select alternative",
                        c("two.sided", 
                          "greater",
                          "less"),
                        selected = "two.sided"),
            br(),
            
            textInput("mu", "Enter population mean for one-sample t test", ""),
            br(),
            
            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", "Select variable names", 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 个级别。

有什么问题。非常感谢。

【问题讨论】:

  • 你有dff1[input$var1]dff1 对象定义了吗?

标签: r shiny t-test


【解决方案1】:

您可以尝试将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)
  }
})

【讨论】:

  • 当我输入数据集时仍然得到错误:“.subset2 中的错误:尝试在 integerOneIndex 中选择少于一个元素”。
  • 您能否提供一些示例数据供我们测试我们的答案?
  • 性别年龄 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 0 13 1 3 4 3 3 2 2 2 2 2 0 13 1 3 4 3 3 2 2 2 2 2 1 13 3 2 2 4 3 3 2 2 3 4 1 13 3 2 2 4 3 3 2 2 3 4 1 13 1 2 3 4 3 3 3 4 1 1 1 13 1 2 3 4 3 3 3 4 1 1 1 13 2 3 2 1 2 2 3 3 3 3 1 13 3 2 2 1 2 3 3 4 2 3 1 13 3 3 2 1 1 3 1 3 3 3 1 13 1 1 1 1 1 4 1 2 1 2 0 13 1 3 1 4 1 3 1 1 1 1 1 13 2 3 2 1 2 2 3 3 3 3 1 13 3 2 2 1 2 3 3 4 2 3 1 13 3 3 2 1 1 3 1 3 3 3 1 13 1 1 1 1 1 4 1 2 1 2
  • @shunpeng 请使用我们可以复制的数据更新您的帖子。 cmets 中的数据尚不清楚。阅读how to give a reproducible example
  • 谢谢Shah,数据代码如下:df
猜你喜欢
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
相关资源
最近更新 更多