首页 > 解决方案 > 进行方差测试时出错

问题描述

我正在尝试从用户那里获取两个不同的数据集,并让用户从两个数据集中选择两个变量并执行方差测试。当我尝试在 R Shiny 应用程序中执行方差测试时,它会引发错误:在应用程序中选择了未定义的列。如果有人能解决这个问题,那将是一个很大的帮助,TIA

用户界面

#install.packages("shiny")

library(shiny)



shinyUI(fluidPage(

titlePanel("Uploading Files"),

# Sidebar layout with input and output definitions ----
sidebarLayout(

# Sidebar panel for inputs ----
sidebarPanel(
  selectInput("type","Select the input type:",c("File"="file","Dataset"="ds","URL"="url")),
  conditionalPanel(condition="input.type==\"file\"",
                   fileInput("file1", "Choose CSV File",
                             accept = c(
                               "text/csv",
                               "text/comma-separated-values,text/plain",
                               ".csv")
                   ),
                   tags$hr(),
                   checkboxInput("header", "Header", TRUE),
                   radioButtons("sep", "Separator",
                                choices = c(Comma = ",",
                                            Semicolon = ";",
                                            Tab = "\t"),
                                selected = ","),
                   radioButtons("quote", "Quote",
                                choices = c(None = "",
                                            "Double Quote" = '"',
                                            "Single Quote" = "'"),
                                selected = '"')),
  conditionalPanel(condition="input.type==\"ds\"",
                   selectInput("ds","Select a Dataset",
                             choices=ls(package:datasets))),
  conditionalPanel(condition="input.type==\"url\"",
                  textInput("url1","Enter the url here",
                  "http://users.stat.ufl.edu/~winner/data/cardiacoutput_measure.csv")),
                  radioButtons("disp", "Display",
                                choices = c(Head = "head",
                                            All = "all"),
                                selected = "head"),
  selectInput("col1","Select the column",choices=NULL),

  selectInput("type2","Select the input type:",c("File"="file3","Dataset"="ds1","URL"="url2")),

  conditionalPanel(condition="input.type2==\"file3\"",
                   fileInput("file4", "Choose CSV File",
                             accept = c(
                               "text/csv",
                               "text/comma-separated-values,text/plain",
                               ".csv")
                   ),
                   tags$hr(),
                   checkboxInput("header", "Header", TRUE),
                   radioButtons("sep", "Separator",
                                choices = c(Comma = ",",
                                            Semicolon = ";",
                                            Tab = "\t"),
                                selected = ","),
                   radioButtons("quote", "Quote",
                                choices = c(None = "",
                                            "Double Quote" = '"',
                                            "Single Quote" = "'"),
                                selected = '"')),
  conditionalPanel(condition="input.type2==\"ds1\"",
                   selectInput("ds2","Select a Dataset",
                             choices=ls(package:datasets))),
  conditionalPanel(condition="input.type2==\"url2\"",
                  textInput("url3","Enter the url here",
                  "http://users.stat.ufl.edu/~winner/data/cardiacoutput_measure.csv")),
                  radioButtons("disp", "Display",
                                choices = c(Head = "head",
                                            All = "all"),
                                selected = "head"),
  selectInput("col2","Select the column",choices=NULL),
  selectInput("conmodel", "Select Model", 
                               choices = c("Normal" = "normal", 
                                           "Exponential" = "exponential", 
                                           "Uniform" = "uniform"), 
                               selected = "exponential" 

                   ),
                   conditionalPanel( 

                     condition = "input.conmodel == 'uniform'", 

                     numericInput("a", "parameter a in Normal" , value = -2),  

                     numericInput("b", "parameter b in Normal" , value = 0.8) 

                   ),
                   conditionalPanel( 

                     condition = "input.conmodel == 'normal'", 

                     numericInput("mu", "parameter mu in Normal" , value = 0),  

                     numericInput("sigma", "parameter sigma in Normal" , value = 1) 

                   ),         
                   sliderInput("s", "number of simulated data" ,min=1, max=1000, value = 10),

selectInput("ttest","Select the test:",c("Variance Test"="vart",
              "1 sample test"="ost",
              "2 sample test"="tst",
              "paired t test"="ptt"),selected=""),                  


                conditionalPanel(condition="input.ttest=='ost'",
                   numericInput("c","Please enter the mean value",value=0)))
  #conditionalPanel((condition="input.ttest=='tst'",                   
  , mainPanel(

   tabsetPanel(type = "tabs",
               tabPanel("Summary_1", verbatimTextOutput("summary1")),
               tabPanel("Summary_2", verbatimTextOutput("summary2")),
               tabPanel("Dataset_1", tableOutput("table1")),
               tabPanel("Dataset_2", tableOutput("table2")),
               tabPanel("Plot",plotOutput("myhist")),
               tabPanel("Prob",tableOutput('prob')),
               tabPanel("Variance Test",verbatimTextOutput('var')))
   ))))

服务器

library(BatchGetSymbols)
library(shiny)
server <- function(input, output,session) {


  x <- reactive({
  a<- switch(input$type,
       file=
         if(is.null(input$file1))     return(NULL) else
         read.csv(input$file1$datapath,
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote),

       ds=get(input$ds, "package:datasets"),
       url= if(is.null(input$url1))     return(NULL) else
       read.csv(url(input$url1))
       )

if(input$disp == "head") {
  return(head(a))
}
else {
  return(a)
}
})

   y <- reactive({
   b<- switch(input$type2,
           file3=
             if(is.null(input$file4))     return(NULL) else
               read.csv(input$file4$datapath,
                        header = input$header,
                        sep = input$sep,
                        quote = input$quote),
           ds1=get(input$ds2, "package:datasets"),
           url2= if(is.null(input$url3))     return(NULL) else
             read.csv(url(input$url3))
 )

if(input$disp == "head") {
  return(head(b))
}
else {
  return(b)
}
 })
   observe({ 
  updateSelectInput(session, "col1", 
                  choices = colnames(x()))})


 observe({ 
  updateSelectInput(session, "col2", 
                  choices = colnames(y()))})


 output$myhist<-renderPlot({
 a1 <- as.data.frame(x())
 a2 <- as.data.frame(y())
if (!is.null(x()) && (is.null(y()))) {   
 hist(a1[,input$col1],breaks=5,col="red",xlab =input$col1 ,main="")}
else if 
 (is.null(x()) && (!is.null(y())))
 {
 hist(a2[,input$col2],breaks=5,col="red",xlab =input$col2 ,main="")}
else 
 {plot(a1[,input$col1],a2[,input$col2]) }

})

output$prob <- renderPrint({ 
print(paste('Selected Column :',input$col1)) 
if(is.null(x()))     return(NULL) else
df <- x() 
xdata <- df[,input$col1] 
if (input$conmodel == 'normal')
{  
  if(is.null(xdata))     return(NULL) else
  print(paste('Predicted Normal Value :',mean(rnorm(input$s,mean(xdata), 
  sd(xdata))))) 

} 


if (input$conmodel == 'exponential')  
{ 
  if(is.null(xdata))     return(NULL) else
  print(paste('Predicted Exponential Value 
 :',mean(rexp(input$s,1/mean(xdata))))) 

} 
if (input$conmodel == 'uniform') { 
  if(is.null(xdata))     return(NULL) else
  print("changes to be done")
} 
})

output$var <- renderTable({
a1 <- as.data.frame(x())
a2 <- as.data.frame(y())
col3 <- as.integer(input$col1)
col4 <- as.integer(input$col2)
if(is.null(x()) &&  (is.null(y())))    return(NULL) else

  var.test(a1[,col3],a2[,col4])
})


output$table1 <- renderTable({
if(is.null(x()))     return(NULL) else
a <- x()
a <- subset(a, select = input$col1) #subsetting takes place here
  })
output$table2 <- renderTable({
if(is.null(y()))     return(NULL) else
  b <- y()
b <- subset(b, select = input$col2) #subsetting takes place here
})

 output$summary1 <- renderPrint({
 if(is.null(x()))     return(NULL) else
 a <- x()
 summary(a)})

 output$summary2 <- renderPrint({
 if(is.null(y()))     return(NULL) else
   b <- y()
 summary(b)})

 }

标签: rshinyundefinedhypothesis-test

解决方案


推荐阅读