0

I tried to achieve when with a chosen split percentage, it returns the train set and then with a sampling method to resample train set and calculate its class freq and perc.

The error I got: object 'split.df' not found when I choose check box 'over'.

Should I use eventReactive or other syntax to achieve? The final return the table with either freq or perc should be dependent on 'split', 'sample' and dropdown 'freq' or 'perc'.

Here is portion that relates in ui:

sidebarLayout(
        sidebarPanel(
          h3("Train/test set"),
          tags$br(),
          selectInput(
            "trainset",
            "Select train component",
            choices = list('freq'='freq', 'percentage'='perc'),
            
          ),
sliderInput(
            "split",
            label = "split percentage",
            min = 0,
            max = 1,
            value = 0,
            step = 0.1
          ),
          h3("resampling train set"),
          checkboxGroupInput('sample', label = "sampling method",
                             choices = list('original'='original','over'='over', 'under'='under', 'both'='both','ROSE'='ROSE'),
                             selected = list('original'='original'))
        ),

Here is a code relates for server:

  split.df <- reactive({
    index <- createDataPartition(df$class, p=input$split, list=FALSE)
    Training_Data <- df[index,]
    return(Training_Data)
  })
  
  train_set <- reactive({
    if(input$sample == 'original')
      Training_Data_class <- data.frame(class = split.df()$class)
    return(Training_Data_class)
  })
  
  over_train_set <- reactive({
    split.df <- split.df()
    if(input$sample == 'over'){
    over <- ovun.sample(class~., data = split.df, method = 'over')$data
    Training_Data_class_over <- data.frame(class = over$class)
    return(Training_Data_class_over)}
  })
  
   trainset_df <- reactive({
     freq.df.train <- data.frame(table(train_set()))
     colnames(freq.df.train) <- c('class', 'freq')
     perc.df.train.=data.frame(prop.table(table(train_set()))*100)
     colnames(perc.df.train) <- c('class','perc')
     if(input$trainset == 'freq')
       return(freq.df.train)
      if(input$trainset == 'perc')
        return(perc.df.train)
   })
  
   over_trainset_df <- reactive({
     freq.df.train.over <- data.frame(table(over_train_set()))
     colnames(freq.df.train.over) <- c('class', 'freq')
     perc.df.train.over=data.frame(prop.table(table(over_train_set()))*100)
     colnames(perc.df.train.over) <- c('class','perc')
     if(input$trainset == 'freq')
       return(freq.df.train.over)
     if(input$trainset == 'perc')
       return(perc.df.train.over)
   })
   
  output$trainsetdistr <- DT::renderDataTable({
    if(input$sample == 'over'){
      return(over_trainset_df())
    }
    if(input$sample == 'original'){
      return(trainset_df())
    }
  }
    
  )
  • It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Where does the `ovun.sample` function come from? – MrFlick Jul 10 '22 at 18:55
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jul 11 '22 at 08:29

0 Answers0