发布闪亮的应用程序时出错:app.R没有返回shiny.appobj对象

时间:2019-01-03 00:47:48

标签: r shiny

我正在使用简单的ML模型创建一个应用,其工作流程如下:

1)读取文件。 2)建立模型 3)绘制预测和变量重要性

本地化,该应用正常运行:

enter image description here

但是当我尝试发布应用程序时,出现以下错误:

Error in value[[3L]](cond) : app.R did not return a shiny.appobj object.
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Ejecución interrumpida

enter image description here

错误没有告诉我,这是完整的代码:

library(shiny)
library(readxl)
library(tidyverse)
library(xgboost)
library(caret)
library(iml)


#### UI


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
      plotOutput("plot2", click = "plot_brush"),
      plotOutput("plot1", click = "plot_brush")
    )
  )
)

server <- function(input, output) {
  # create mydata as a reactiveVal so that it can be edited everywhere
  mydata = reactiveVal()
  model <- reactiveValues()


  # reactive block is changed with an observe that allows mydata to be updated
  # on change of data
  observe({
    req(input$file1, input$header, file.exists(input$file1$datapath))
    data = read.csv(input$file1$datapath, header = input$header)
    mydata(data)
  })


  output$contents <- renderTable({
    req(mydata())
    #mydata()
  })


  ### test
  xgb_trcontrol = trainControl(
    method = "cv",
    number = 5,
    allowParallel = TRUE,
    verboseIter = FALSE,
    returnData = FALSE
  )


  xgbGrid <- expand.grid(nrounds = c(10,14),  # this is n_estimators in the python code above
                         max_depth = c(10, 15, 20, 25),
                         colsample_bytree = seq(0.5, 0.9, length.out = 5),
                         ## The values below are default values in the sklearn-api.
                         eta = 0.1,
                         gamma=0,
                         min_child_weight = 1,
                         subsample = 1
  )




  observe({

    if ('data.frame' %in% class(mydata()) & !'predicted' %in% names(mydata())){
      set.seed(0)
      xgb_model = train(
        select(mydata(),"LotArea","YrSold"), as.vector(t(mydata()["SalePrice"])),
        trControl = xgb_trcontrol,
        tuneGrid = xgbGrid,
        method = "xgbTree"
      )

      predicted = predict(xgb_model, select(mydata(),"LotArea","YrSold"))
      data = mydata()
      data["predicted"] = predicted
      mydata(data)
    }


    #xgb_model



  })

  output$plot1 <- renderPlot({
    data = mydata()
    # this is here to prevent premature triggering of this ggplot.
    # otherwise you'll get the "object not found" error
    if('predicted' %in% names(data)){
      ggplot(mydata(), aes(x=predicted, y=SalePrice)) + geom_point()
    }
  })

  output$plot2 <- renderPlot({
    data = mydata()
    # this is here to prevent premature triggering of this ggplot.
    # otherwise you'll get the "object not found" error
    if('predicted' %in% names(data)){

      xgb_model = train(
        select(mydata(),"LotArea","YrSold"), as.vector(t(mydata()["SalePrice"])),
        trControl = xgb_trcontrol,
        tuneGrid = xgbGrid,
        method = "xgbTree"
      )

      predictor = Predictor$new(xgb_model, data = select(mydata(),"LotArea","YrSold"), y = mydata()["SalePrice"])
      shapley = Shapley$new(predictor, x.interest = select(mydata(),"LotArea","YrSold")[1,])
      shapley$plot()      
    }
  })


}

shinyApp(ui, server)

以及输入数据的样本:

https://drive.google.com/file/d/1R8GA0fW0pOgG8Cpykc8mAThvKOCRCVl0/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

成功了。 Here我的Shinyapp.io帐户上的应用。上传和运行只花了一段时间。

也许您必须检查应用程序版本。 Here's我拥有的软件包版本。我正在使用R版本3.5.2(2018-12-20)和RStudio 1.1.463。

shiny app upload