1

The app is asking the user to input predictor & dependent variables. For that I am using renderUI & uiOutput functions in server.R & ui.R files respectively. I am storing these inputs in predvar & depvar variables. Then i am using these variables in my reactive part of the code. This is where i think the problem of connection is between reactive code & user input variables. I have tried using caret::creatdatapartition instead of just createdatapartition. server.R code

model <- reactive ({
        prop = input$prop
        predictor = input$predvar
        dependent = input$depvar
        if(length(predictor)==0){return("Select atleast one predictor")} 
        if(input$ex==TRUE){data <- datasets::iris}
        else{file1 <- input$file
        data =  read.table(file =  file1$datapath,sep =",",header = TRUE)
        data = as.data.frame(data)}
        set.seed(69)
        inTrain <- createDataPartition(y=data$dependent,p=prop,list = FALSE) ## this line throws error
        train <- data[inTrain,]
        train <- train %>% select(predictor,dependent)
    train(dependent~.,data=data,method = "rpart")
    })

    output$model <- renderPrint({
      model()
)}
output$dependent <- renderUI({
      if(input$ex==TRUE){
        data = datasets::iris
        dependents <- select_if(data,is.factor)
        selectInput("depvar","Select the dependent variable",choices = colnames(dependents))
      }
      else{
        file1 <- input$file
        data =  read.table(file =  file1$datapath,sep =",",header = TRUE)
        dependents <- select_if(data,is.factor)
        selectInput("depvar","Select the dependent variable",choices = colnames(dependents))
      }
    })
    output$predictor <- renderUI({
      if(input$ex==TRUE){
        data = datasets::iris
        dependents <- select_if(data,is.numeric)
        checkboxGroupInput("predvar","Select the predictor variables",choices = colnames(dependents))
      }
      else{
        file1 <- input$file
        data =  read.table(file =  file1$datapath,sep =",",header = TRUE)
        dependents <- select_if(data,is.numeric)
        checkboxGroupInput("predvar","Select the predictor variables",choices = colnames(dependents))
      }
    })

concerning ui.R code

           checkboxInput("ex","Uncheck for using your own file",value = TRUE),
           fileInput("file", "Upload the *.csv file with headers"),
           uiOutput("dependent"),
           uiOutput("predictor"),
           sliderInput("prop",
                       "Enter the training data ratio",
                       min = .5,
                       max = 1,
                       value = .6,step = .05)
       )

Shiny app output image link

1 Answers1

0

You haven't given us a simple self contained example, so we can't give you a tested answer. But I think I can see at least two problems with your server code.

First, the model reactive looks like it will run then the server function is first called, before your predvar and depvar inputs have been populated. That's going to case a problem, but it's easy to fix: just put req(input$depvar, input$predvar at the start of the reactive. That will make sure the rest of the code in the reactive runs only once you've got values for both these inputs.

Second, the line you identified,

inTrain <- createDataPartition(y=data$dependent,p=prop,list = FALSE)

Says "create a data partition and assign the parameter y the contents of the column named 'dependent' in the data.frame data. What you want to say is "... using the contents of the column whose name is given by the value of my local variable dependent...".

So try

inTrain <- createDataPartition(y=data[[dependent]],p=prop,list = FALSE)

instead.

You may have other issues as well, but they're the two I spotted from what you've posted so far.

Based on our discussion below, here is a MWE:

library(shiny) 
library(dplyr) 
library(datasets) 

ui <- shinyUI(
  fluidPage( 
    titlePanel("Classification tree model on iris dataset."), 
    sidebarLayout( 
      sidebarPanel( 
        uiOutput("dependent"), 
        uiOutput("predictor"), 
        sliderInput("prop", "Enter the training data ratio", min = .5, max = 1, value = .6,step = .05) ), 
      mainPanel( 
        verbatimTextOutput("model")
      ) 
    ) 
  )
)

server <- function(input, output) { 
  output$dependent <- renderUI({
    data = datasets::iris
    dependents <- select_if(data,is.factor)
    selectInput("dependent","Select the dependent variable",choices = colnames(dependents))
  })

  output$predictor <- renderUI({ 
    data = datasets::iris 
    predictors <- select_if(data,is.numeric) 
    checkboxGroupInput("predvar","Select the predictor variables", choices = colnames(predictors)) 
  }) 
}

shinyApp(ui, server)

You had selectInput("depvar", ... rather than selectInput("dependent", ... in your output$dependent. That's all that was wrong.

A couple of points to note:

  • Your simple self-contained example (SSE)wasn't bad, but everything to do with model was irrelevant as far as I could see, so could be removed. There are also far easier ways of preenting the code to us than in multiple comments! ;)
  • In your SSE, I don't think there's a need for uiOutput and renderUI. You could present your checkBoxGroup and selectInput directly in the fluidPage and then use updateSelectInput() and updateCheckBoxGroupInput in an observe or observeEevent (the latter depending on data) reactive. That removes one level of indirection and might make things simpler whoever maintains your code. [NB: if you do this, you will need to change server <- function(input, output) {...} to server <- function(input, output, session) {...}.
  • Next time, rather than saying "I tried, and it didn't work" (I'm paraphrasing: I can't see your comments whilst writing an answer), say "I tried, but I got the following error [Give the error text] at line number nnn".

Good luck!

Limey
  • 10,234
  • 2
  • 12
  • 32
  • thanks for your time. I tried both solutions but they are not working – Anirudh Jain May 23 '20 at 13:26
  • thanks for your time. I tried both solutions but they are not working @Limey please refer this link for the entire code [link](https://rpubs.com/anirudh_bond/618609) – Anirudh Jain May 23 '20 at 13:34
  • With respect, Anirudh, we don't want your full code. That obsures the problem and wastes our time. We need a simple, self-contained example that allows us to focus on the issue. Please read [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and amend your question accordingly. If you expend some effort on the question, you are far more likely to get useful and informative answers. – Limey May 23 '20 at 14:46
  • @shiny understood – Anirudh Jain May 24 '20 at 07:19
  • @shiny here's the reproducible code ui.R `library(shiny) shinyUI(fluidPage( titlePanel("Classification tree model on iris dataset."), sidebarLayout( sidebarPanel( uiOutput("dependent"), uiOutput("predictor"), sliderInput("prop", "Enter the training data ratio", min = .5, max = 1, value = .6,step = .05) ), mainPanel( verbatimTextOutput("model")) ) ) )` – Anirudh Jain May 24 '20 at 07:21
  • @shiny **server.R** **code part1** `library(shiny) library(rattle) library(dplyr) library(datasets) library(caret) library(e1071) shinyServer(function(input, output) { output$dependent <- renderUI({ data = datasets::iris dependents <- select_if(data,is.factor) selectInput("depvar","Select the dependent variable",choices = colnames(dependents)) })` – Anirudh Jain May 24 '20 at 07:23
  • **server.R** **code part 2** `output$predictor <- renderUI({ data = datasets::iris predictors <- select_if(data,is.numeric) checkboxGroupInput("predvar","Select the predictor variables",choices = colnames(predictors)) }) model <- reactive ({ req(input$depvar, input$predvar) prop = input$prop predictor = input$predvar dependent = input$depvar if(length(predictor)==0){return("Select atleast one predictor")} data <- datasets::iris` @Limey – Anirudh Jain May 24 '20 at 07:29
  • **server.R code part 3** `set.seed(69) inTrain <- createDataPartition(y=data[[dependent]],p=prop,list = FALSE) train <- data[inTrain,] train <- train %>% select(predictor,dependent) train(dependent~.,data=data,method = "rpart") }) output$model <- renderPrint({ model() }) })` @Limey – Anirudh Jain May 24 '20 at 07:32
  • Nevermind @Limey this solves the problem `data <- select(data, dependent, predictor) index <- createDataPartition(data[,1],p = prop, list = FALSE) train <- data[index,] train(x = train[,-1],y = train[,1],method = "rpart")` – Anirudh Jain May 25 '20 at 12:56