运行闪亮的应用程序时生成错误

时间:2018-11-30 00:53:31

标签: r shiny

编辑:谁能告诉我如何在光泽中添加随机森林模型。

    library(shiny)

    library(ggplot2)
    library(dplyr)

ntshiny <- read.csv("NT4.csv", stringsAsFactors = FALSE)

ui <- fluidPage(
  titlePanel("Random Forest Model prediction of impact of Tickets raised"),
  sidebarLayout(
    sidebarPanel(
      uiOutput("SpecificsOutput"),
      uiOutput("ComponentOutput"),
      uiOutput("CategoryOutput")
    ),
    mainPanel(
      plotOutput("coolplot"),
      br(),
      br(),
      tableOutput("results")
      )
  )
)

server <- function(input, output) {
    output$SpecificsOutput <- renderUI({
      selectInput("SpecificsInput", "Specifics",
                  sort(unique(ntshiny$Specifics)),
                  selected = "Fault Management")
    })
    output$ComponentOutput <- renderUI({
      selectInput("ComponentInput", "Component",
                  sort(unique(ntshiny$Component)),
                  selected = "Admin Event") 
    })
    output$CategoryOutput <- renderUI({
      selectInput("CategoryInput", "Category",
                  sort(unique(ntshiny$Category)),
                  selected = "Security")
    })

    filtered <- reactive({
      if (is.null(input$SpecificsInput)) {
        return(NULL)
      }
      if (is.null(input$ComponentInput)) {
        return(NULL)
      }
      if (is.null(input$CategoryInput)) {
        return(NULL)
      }

    ntshiny %>%
      filter(
        Specifics == input$SpecificsInput,
        Component == input$ComponentInput,
        Category == input$Category
      )
    })

    output$coolplot <- renderPlot({
      if (is.null(filtered())) {
        return()
      }
      ggplot(filtered(), aes(Impact)) +
        geom_histogram()
    })

    output$results <- renderTable({
      filtered()
    })
  }

shinyApp(ui = ui, server = server)

我建立了随机森林模型来预测门票的影响。现在,我正在尝试构建闪亮的应用程序以预测影响。因此,在闪亮的脚本中,我正在上传文件。但是当我运行该应用程序时,它会说

Warning: Error in [: object of type 'closure' is not subsettable 68: levels

我不知道这段代码是怎么回事。 请在这里帮助我。非常感谢。

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')
  )
))


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]
  })
})

0 个答案:

没有答案