首页 > 解决方案 > 如何让我的 train() 函数在闪亮状态下运行以评估模型性能?

问题描述

我正在做一个项目来创建一个闪亮的应用程序,允许用户进行一些建模、选择一个模型并进行一些预测。

当我在我的数据上运行 r 控制台中的 train() 函数时,它工作正常。当我在闪亮的应用程序中运行它时,我一直收到一个错误:对比只能应用于具有 2 个或更多级别的因子。我的数据框没有任何因素。无法弄清楚为什么它在控制台中有效但不闪亮。任何帮助,将不胜感激!

这是我的代码:

library(shiny)

shinyUI(fluidPage(

    # Application title
    titlePanel("Variables Impacting Hate Crime Rates"),

    # define main panel layout
        mainPanel(
            tabsetPanel(type = "tabs",
                        tabPanel("Information",
                                 textOutput("introduction")),
                        tabPanel("Data Exploration",
                                 textOutput("data"),
                                 selectInput("x","Explanatory variable:",
                                             choices = colnames(crimes),
                                             selected = "median_household_income"),
                                 selectInput("y","Response variable:",
                                             selected = "median_household_income",
                                            choices = colnames(crimes)),
                                 plotOutput("scatterplot"),
                                 selectInput("y2", "Variable to compare:",
                                             selected = "median_household_income",
                                             choices = colnames(crimes)),
                                 plotOutput("boxplot"),
                                 selectInput("var","Summary variable:",
                                             choices = colnames(crimes),
                                             selected = "median_household_income"),
                                 verbatimTextOutput("summary"))
                        ,
                        tabPanel("Unsupervised Learning",
                                 selectInput("k","Number of clusters:",
                                             choices = c(1,2,3),
                                             selected = 1),
                                 plotOutput("cluster"),
                                 selectInput("dmethod","Distance method:",
                                             choices = c("euclidian","binary","minkowski",
                                                         "canberra","manhattan","maximum"),
                                             selected = "euclidian"),
                                 selectInput("cmethod", "Cluster method:",
                                             choices = c("single","complete"),
                                             selected = "single"),
                                 plotOutput("tree")),
                        tabPanel("Modeling",
                                 selectInput("xvar", "x variable:",
                                            choices = colnames(crimes),
                                            selected = "gini_index"),
                                 selectInput("yvar", "y variable:",
                                             choices = colnames(crimes),
                                             selected = "median_household_income"),
                                 verbatimTextOutput("model")),
                        tabPanel("Data",
                                 DT::dataTableOutput("mytable1"))
        )
    )
))


source("C:/Users/W447075/Documents/ST558/Comora_final/helpers.R")

shinyServer(function(input, output,session){
     output$introduction <- renderText({
        "This is my introduction"
    })

     output$data <- renderText({
       "This page allows you to create some basic graphical
       and numeric summaries including a scatter plot, boxplot,
       and statistical summary for any of the variables in the 
       'crimes' dataset."
     })

    selectedData <- reactive({
      crimes %>% filter(region ==input$region)
    })
    selectData2 <- reactive({
      crimes[ , c("region",input$y)]
    })

    selectData3 <- reactive({
      crimes[ , c("region", input$y2)]
    })

    mydata <- reactive({
      crimes[ ,input$var]
    })

    mycluster <- reactive({
      kmeans(df_scale, centers = input$k, nstart = 25)
    })

    d <- reactive({
      dist(df, method = input$dmethod)
    })

    clusterplot <- reactive({
      plot(hclust(d(), method = input$cmethod))
    })

    model <- reactive({
      train(input$yvar ~ input$xvar, crimes,
            method = "lm",
            trControl = trainControl(
              method = "cv", number = 10,
              verboseIter = TRUE))
    })

    #render a barplot
    output$scatterplot <- renderPlot({
      ggplot(crimes, aes_string(x = input$x,
                                        y = input$y)) + 
        geom_point(size = 3) + 
        theme(axis.text.x = element_blank(),
              axis.ticks.x = element_blank(),
              axis.text.y = element_blank())
    })

    output$boxplot <- renderPlot({
      ggplot(selectData3(), aes(x = region, y = selectData3()[ ,input$y2])) + 
        geom_boxplot(aes(fill = region)) + ylab(input$y2)+
        theme(axis.text.x = element_blank(),
              axis.ticks.x = element_blank(),
              axis.text.y = element_blank())
    })

    output$summary <- renderPrint({
      dataset <- na.omit(mydata())
      summary(dataset)
    })

    output$cluster <- renderPlot({
      fviz_cluster(mycluster(),data = df)
    })

    output$tree <- renderPlot({
      clusterplot()
    })

    output$mytable1 <- DT::renderDataTable({
      DT::datatable(df)
    })

    output$model <- renderText({
      model()
    })
    })


标签: rshiny

解决方案


推荐阅读