0

Recently, I try to learning shinyapp, and I try to do a shinyapp for 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"),
                        ))
    })
    
    })

As you can see, I can print dff()[input$gro] in output$sum, but I can't run in output$tr.

when I run the code, I get the Erorr: Error in t.test.formula: grouping factor must have exactly 2 levels.

What's the problem. Thank you so much.

1 Answers1

0

You can try changing your output$tr code to :

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)
  }
})
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • still get the erorr: "Error in .subset2: attempt to select less than one element in integerOneIndex", when I input a dataset. – shun peng Jan 14 '21 at 12:05
  • Can you provide some example data for us to test our answer? – Ronak Shah Jan 14 '21 at 13:56
  • gender age 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 – shun peng Jan 15 '21 at 14:47
  • @shunpeng Please update your post with data that we can copy. Data is is not clear in comments. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jan 15 '21 at 14:52
  • Thanks Shah, the data code is below: df <- data.frame( gender = rep(1:2, each = 25, time = 2), x1 = sample(1:5, 100, replace = TRUE), x2 = sample(0:4, 100, replace = TRUE), x3 = sample(1:7, 100, replace = TRUE) ) – shun peng Jan 15 '21 at 23:28
  • When I use this data the app doesn't run for me using your code from the above post. – Ronak Shah Jan 17 '21 at 08:02