creating dynamic classification trees in r using dplyr, rpart & shiny
September 23, 2016
The following R Markdown code block will produce an R Markdown html document the app on Shiny with an embedded Shiny app that allows the user to chose any combination of attributes from a dataset and immediately see the resulting tree plotted using the rpart.plot package.
Once loaded, the default application will generate an rpart tree diagram using the attributes “plas” & “mass” from the pima indians Pima Indians Diabetes Dataset and look like this:
Different attributes can be utilized by selecting / de-selecting them in the attribute input box to the left of the plot. In the graphic below, the tree has been automatically redrawn using the selected attributes: “plas”, “mass”, “skin”, “press” & “preg”.
Note that not all attributes will force the underlying CART algorithm to generate a new split.
To reproduce this application, open a new R Markdown document in RStudio:
Select “Shiny” from the options on the left, create a title for your document and select “OK”:
Clear all the contents from the file that opens and copy the entire code block (included at the bottom of this post) below into the blank R Markdown document and save the file. Note the file exension should be .Rmd. Select “Run Document”:
An R Markdown document with comments, code and the embedded Shiny classification tree app should launch: