Error while running the shiny app

I build random forest model for predicting the impact of tickets. Now I am trying to build the shiny app for predicting the impact. So in the shiny script I am uploading my file. But when I run the app it says "Warning: Error in [: object of type 'closure' is not subsettable
68: levels"
I don;t know whats going on with this code.
Please help me here. Thank you very much.
Lokks like I can't upload csv file of my dataset. However I have uploaded the jpg image.

**#ui.R**
library(tidyverse)

library(shiny)



shinyUI( pageWithSidebar(
  headerPanel( "Random Forest model applied to network tickets dataset"),
  sidebarPanel(
    fileInput("file_input", "Upload your dataset"),
    h4("Ticket's attributes:"),
    br(),
    selectInput( "cl", "Specifics", levels(df[1,6]), "Fault Management"),
    selectInput( "se", "Component", levels(df[1,5]), "Admin Event"),
    selectInput( "ag", "Category", levels(df[1,2]), "Security"),
    br(),
    h4("Random Forest prediction:"),
    br(),
    h5("Impact ?"),
    textOutput("result")
  ),
  mainPanel(
    h3("Network Ticket data set"),
    p("It provides information about the ticket raised"),
    h3("Random Forest prediction"),
    p("Based on this data set this ShinyApp uses a Random Forest prediction model and display the associated stats numbers based on ticket's attributes"),
    plotOutput('plot1')
  )
))

**#server.R**

library(tidyverse)

library(shiny)



mdata <- read_csv(input$file_input$datapath)
mod <- randomForest(Survived ~ .data = mdata)

shinyServer( function(input, output) {
  output$plot1 <- renderPlot({
    
    selectedData <- df[df$Specifics==input$cl & df$Component==input$se & df$Category==input$ag,5]
    bplt <- barplot(selectedData,
                    beside=TRUE, horiz=TRUE, xlim=c(0,50),
                    main="Impact stats based on selected tickets attributes",
                    ylab="Total",
                    col=c("black", "grey"),
                    legend = c("Service Impacting", "Non-Service Impacting")
    )
    text(x=selectedData+20,
         y=bplt,
         labels=as.character(selectedData),
         xpd=TRUE)
  })
  output$result <- renderText({ 
    r <- predict(mod, df[df$Specifics==input$cl & df$Component==input$se & df$Category==input$ag & df$Impact=="Yes",1:1])
    levels(r)[r]
  })
})


@sigh

Looking at output$result, there is a line of code of levels(r)[r]

Without the data, I can't reproduce it on my end, so I'll give my two ideas:

  1. r is a factor vector and levels(r) will return an unnamed character vector. My guess is that you only want levels(r).
  2. r is not a factor vector. Make sure you're returning the correct item from predict. To debug, add str(r) right after it is created. (Or even just return r directly.)
1 Like

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