title: r Shiny Decision Tree Data Input
body: <p>I'm making a shiny application that determines a decision tree model and then comes up with predictions based on user inputs.</p>
  
  <p>The model is developed like so:</p>
  
  <pre><code>tr&lt;-prune.tree(tree(Y ~ ., dataset,split="gini"),best=4)
  </code></pre>
  
  <p>There are a total of 20 variables, and only 5 are actually used in the tree. So, I would like to create my app such that the user only needs to input these 5 variables, rather than all of them. Here's what I tried (only showing the important code):</p>
  
  <p>In server.r:</p>
  
  <pre><code> output$treepred&lt;-renderText({
  predict(tr,data.frame(PREVIOUS=input$numeric6,NR_EMPLOYED=input$numeric1,CAMPAIGN=input$numeric7,EMP_VAR_RATE=input$numeric8,CONS_PRICE_IDX=input$numeric9),type="class")
  })
  </code></pre>
  
  <p>In ui.r:</p>
  
  <pre><code>box(textOutput("treepred"))
  </code></pre>
  
  <p>Simply running this returns me an error "object 'AGE' not found" (where AGE is an <em>unused</em> variable). If I reconstruct the tree using only the variables that were found to be relevant, I get a totally different (and far worse) tree. (I've also tried including those irrelevant variables but setting them to NULL or NA, but that didn't work either.)</p>
  
  <p>In essence, the issue is that r wishes for the user to input all of the data, when they really needn't. Does anyone know how to get around this?</p>
  
  <p>EDIT:</p>
  
  <p>A small example:</p>
  
  <pre><code>    dir &lt;- "Your directory"
      dataset &lt;- read.csv(paste(dir, "Data.csv",sep = ""))
      dataset[, c(1, 11:13)] &lt;- lapply(dataset[, c(1, 11:13)],as.integer)
      dataset[, c(2:10, 14, 20)] &lt;- lapply(dataset[, c(2:10,14,20)], as.factor)
      dataset[, c(15:19)] &lt;- lapply(dataset[, c(15:19)],as.numeric)
      dataset$PDAYS[dataset$PDAYS == 999] &lt;- NA #this is NA by the definition of the data
  
      library(tree)
  
      tree&lt;-prune.tree(tree(Y ~ ., dataset,split="gini"),best=4)
  
      plot(tree, type="uniform")
      text(tree, pretty=0)
  
      predict(tree,newdata=dataset,type="class")
      #The above all works perfectly.
  
      summary(tree)
      #This tells us which variables are relevant
  
      predict(tree,newdata=data.frame(PREVIOUS=1,CAMPAIGN=1,EMP_VAR_RATE=50,CONS_PRICE_IDX=100,NR_EMPLOYED=5000))
      #returns error: object 'AGE' not found.
  
      #Retraining the tree with only relevant variables:
      tree2&lt;-prune.tree(tree(Y ~ PREVIOUS+NR_EMPLOYED+CAMPAIGN+EMP_VAR_RATE+CONS_PRICE_IDX, dataset,split="gini"),best=4)
  
      plot(tree2, type="uniform")
      text(tree2, pretty=0)
  
      #This tree is completely different and only ever predicts "no"
  </code></pre>
  
  <p>Here is the data:</p>
  
  <p><a href="https://www.dropbox.com/s/d11tc9d23mw64s5/Data.csv?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/d11tc9d23mw64s5/Data.csv?dl=0</a></p>