title: Random forest using caret not working in shiny
body: <p>I am trying random forest using caret package in shiny.....Was trying to print variable importance from random forest model output....following is the code below...</p>
  
  <pre><code>library(shiny)
  
  pacman::p_load(Amelia,broom,caret,cluster,clustertend,clValid,corrplot,dbscan,dplyr,DT,data.table,forecast,fpc,FPDclustering,fpp,GGally,ggfortify,ggraph,ggplot2,ggrepel,ggthemes,gmodels,googleVis,gridExtra,igraph,knitr,mice,missForest,NbClust,optCluster,pacman,plyr,purrr,qcc,randomForest,rCharts,reshape2,tibble,tidyr,tidyverse,TSA,tseries,vegan,VIM,zoo) 
  
  library(markdown)
  library(imputeTS)
  </code></pre>
  
  <p>Define UI for application      </p>
  
  <pre><code> ui &lt;- navbarPage(
  
    # Application title
    titlePanel("MS IIMA_paper ShinyApp "),
  
    # Sidebar layout with input and output definitions ----
    sidebarLayout(
  
      # Sidebar panel for inputs ----
      sidebarPanel(
  
        # Input: Select a file ----
        fileInput("dataset", "Choose CSV File",
                  multiple = TRUE,
                  accept = c("text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")),
        # Include clarifying text ----
        helpText("Note: First select the dataset of csv format only for the App to give any insight!!"),
        # Horizontal line ----
        tags$hr(),
  
        # Input: Checkbox if file has header ----
        checkboxInput("header", "Header", TRUE),
  
        # Input: Select separator ----
        radioButtons("sep", "Separator",
                     choices = c(Comma = ",",
                                 Semicolon = ";",
                                 Tab = "\t"),
                     selected = ","),
  
        # Horizontal line ----
        tags$hr(),
  
  
        # Input: actionButton() to defer the rendering of output ----
        # until the user explicitly clicks the button (rather than
        # doing it immediately when inputs change). This is useful if
        # the computations required to render output are inordinately
        # time-consuming.
        actionButton("update", "Update button", class = "btn-primary"),
        tags$hr()
        ),
  
        # Show a plot of the generated distribution
        mainPanel(
          tabsetPanel(
            navbarMenu("Random Forest",
                       tabPanel("Structure of Data",
                                h4("Input data str"),
                                verbatimTextOutput("summary_rf")),
                       tabPanel("VarImp-Gini (Table)",
                                h4("Variable Importance"),
                                verbatimTextOutput("varImp"))
  
            )
            )
  
           )
     )
  )
  </code></pre>
  
  <p>Define server logic required to draw a histogram</p>
  
  <pre><code> server &lt;- function(input, output) {
  
    datasetInput &lt;- eventReactive(input$update, {
      read.csv(input$dataset$datapath,
               header = input$header,
               sep = input$sep)
    }, ignoreNULL = FALSE)
  
    #Selecting only numeric variables
    MS.num&lt;- reactive({sapply(datasetInput(), is.numeric)})
    MS.DATA.IN.NUM &lt;- reactive({datasetInput()[ , MS.num()]})
    # imputing NAs by zeros
    df&lt;- reactive({imputeTS::na.replace(MS.DATA.IN.NUM(), 0)})
    # Keeping a sample of 10k for modeling
    sample_data &lt;-reactive({df()[1:10000,]})
  
  
    #### Kmeans
  
    opt.cluster=9
    set.seed(115)
    MS.DATA.KMEANS.Mdl &lt;- reactive({kmeans(scale(sample_data()),opt.cluster,nstart=25)})
  
    # appending clusters to the raw sample data
  
    x&lt;-reactive({
      cluster&lt;-MS.DATA.KMEANS.Mdl()$cluster
      cluster
    })
  
      add_to_df &lt;- reactive({
      sample_data1&lt;-cbind(sample_data(),x())
      sample_data1
  
    })
  
    ##### Random Forest on Kmeans data
    # cluster.means1&lt;-reactive({
    #   cluster1&lt;-MS.DATA.KMEANS.Mdl()$cluster
    #   cluster1
    # })
  
    MS.DATA_KMEANS&lt;-reactive({
      df&lt;-add_to_df() %&gt;% mutate_each(funs(as.factor),one_of("x()"))
      df
    }) 
  
    # Generate a summary of the dataset ----
    output$summary_rf &lt;- renderPrint({
      dataset &lt;- MS.DATA_KMEANS()
      str(dataset)
    })
  </code></pre>
  
  <p><strong>caret package</strong></p>
  
  <pre><code>  # for var imp using caret
  
    # create training and test sets (75:25 split) using 'caret' package
    set.seed(123)# for reproducibility
    inTrain &lt;- reactive({caret::createDataPartition(y = as.factor(MS.DATA_KMEANS()$cluster.kmeans()), p = 0.75, list = FALSE)}) 
  
    # subset
    training &lt;- reactive({MS.DATA_KMEANS()[inTrain(), ]}) 
    testing &lt;- reactive({MS.DATA_KMEANS()[-inTrain(), ]}) 
  
  
    # Random forest using caret package
    set.seed(122)
    modFit.rfcaret &lt;- reactive({caret::train(cluster.kmeans~ ., method = "rf",data =training(),trControl = trainControl(method = "cv"), number = 25)}) 
  
    output$varImp &lt;- renderPrint({
      rfImp=varImp(modFit.rfcaret(),scale = FALSE)
      dataset&lt;-setDT(rfImp$importance, keep.rownames = TRUE)[]
      dataset
    })
  
  
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>I am getting the following error:</p>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <pre><code>Warning: Error in caret::createDataPartition: attempt to apply non-function
  Stack trace (innermost first):
      130: caret::createDataPartition
      129: &lt;reactive:inTrain&gt; [C:\Users\ADMIN\Documents\shiny_test/app.R#155]
      118: inTrain
      117: [.data.frame
      116: [ [C:\Users\ADMIN\Documents\shiny_test/app.R#158]
      115: &lt;reactive:training&gt; [C:\Users\ADMIN\Documents\shiny_test/app.R#158]
      104: training
      103: eval
      102: eval
      101: eval.parent
      100: train.formula
       99: caret::train
       98: &lt;reactive:modFit.rfcaret&gt; [C:\Users\ADMIN\Documents\shiny_test/app.R#164]
       87: modFit.rfcaret
       86: varImp
       85: renderPrint [C:\Users\ADMIN\Documents\shiny_test/app.R#167]
       84: func
       83: eval
       82: eval
       81: withVisible
       80: evalVis
       79: utils::capture.output
       78: paste
       77: origRenderFunc
       76: output$varImp
        1: runApp
  </code></pre>
  
  <p>I am unable to debug where the problem is.</p>
  
  <pre><code>head(df) # after appending cluster.kmeans to raw data
  
  ##   mob.data mob.sms mob.voice prepaid.sms mob.pre.voice mob.data.upld
  ## 1      634      80      85.8           2           8.8            46
  ## 2       16      27     247.9          39           3.0             0
  ## 3      560     532       8.3           3          59.1           231
  ## 4     5582      23     157.4         942           2.6           385
  ## 5     7176      31      27.6         300           3.5             0
  ## 6      524      76      13.5          11           8.4           268
  ##   mob.data.dwnld mob.sms.amt mob.voice.amt mob.data.amt cluster.kmeans
  ## 1            628        1.39          34.8           35              5
  ## 2             18        0.52           1.2           20              9
  ## 3            660        2.61           2.6           37              2
  ## 4           5212        2.09           2.0           35              8
  ## 5           6817        8.70           1.0           31              8
  ## 6            476        0.17           2.6           35              8
  </code></pre>