Plot Tidymodel Decision-Tree in Shiny

,

I have been trying numerous options to render a decision tree from rpart, c50 or partykit tidymodels into my shiny application without success. Here's a reprex which consumes a tidymodel which renders fine in my rstudio IDE. Any one know how to render this in shiny?

I've tried renderUI and ggplot2 without success. The error I get with the code below is:

Warning: Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).

when in fact there is data in my m object.

library(shiny)
library(rpart)
library(tidymodels)
library(plotly)
library(ggplot2)

ui <- fluidPage(
        mainPanel(
          plotlyOutput("m9_tree", height = "550px") %>% withSpinner(color = "steelblue") 
        )
)

server <- function(input, output) {

    output$m9_tree <- renderPlotly({
      m <- readRDS("path to a fit model")
      m <- m %>% extract_fit_engine(m)
      print(str(m))
      p <- rpart.plot(m)
    })
}

shinyApp(ui = ui, server = server)

Since we don't have data to hand to run the reprex, could you provide any error messages or description of what you're seeing to help with diagnosis?
Your Shiny code doesn't load the rpart, plotly and parsnip packages. Please confirm your app loads these.

thanks, I forgot to provide these and have edited my reprex. It's odd because the code makes an output tree if simply run in rstudio without the shiny ui or server wrappers.

renderPlotly expects a plotly object (output of plot_ly() or ggplotly()) not a rpart.plot.

Try using renderPlot and plotOutput instead.

rpart.plot is a method to return a decision tree object. This works fine in Rstudio as:

  m <- readRDS("path to fit tidy model")
  m <- m %>% extract_fit_engine(m)
  p <- rpart.plot(m)

I'm unable to find a structure which renders the object in Shiny.

using renderPlotly returns an empty x-y axis but no data with warning Warning: Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).

using renderPlot on the object p returns an error Warning: Error in : Graphics API version mismatch

using ggplotly on the object p returns an error Warning: Error in UseMethod: no applicable method for 'ggplotly' applied to an object of class "list"

using plot_ly on the object p returns an error Warning: Error in : First argument, data, must be a data frame or shared data.

Heres a working example :

library(rpart)
library(rpart.plot)
library(shiny)
data(ptitanic)


ui <- fluidPage(
  plotOutput("p1")
)

server <- function(input, output, session) {
  
  output$p1 <- renderPlot({
    binary.model <- rpart(survived ~ ., data = ptitanic, cp = .02)
    rpart.plot(binary.model,
               main = "titanic survived\n(binary response)")
  })
}

shinyApp(ui, server)
1 Like

thanks, I can try that approach but you can see I'm working with a fitted tidymodel which is not structured as in your example. My resulting models which work well within my application (25,000 lines of code) are represented in a tidymodel structure which is why I use extract_fit_engine to pull out the object to plot which works fine in RStudio. Maybe this is more of a tidymodel expert question, you'd think this use case would be valuable to others trying to display a decision tree based on rpart in shiny but using tidymodel workflows. Thanks

this is a demonstration that renderPlot is the correct render* function to use on the shiny side.
This should work exactly for your example assuming that your m can be passed to rpart.plot as it seems it should be...
You didnt provide me a way to use an example m of yours, so I found a substitute.

If you are stuck or need more support feel free to ask for it... its not clear at this point whether you need further support or not.

thanks, I think what I have to figure out is how do I pass the correct tidymodel object (it's a list) into the example you provided. I don't have this resolved yet but am thinking I can compare the structure of your example object to the tidymodel.

however I still get this error Warning: Error in : Graphics API version mismatch when running your example but that's a separate issue I need to resolve

ok, this resolved my issue with both yours and my reprex:

options(shiny.useragg = FALSE)

thanks for giving this some attention

it may be that you could use ragg, if you update its version ?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.