Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error publishing shiny app: app.R did not return a shiny.appobj object

Tags:

r

shiny

I'm creating an app with a simple ML model, the work flow is as follow:

1) Read a file. 2) Create a model 3) Plot the prediction and variable importance

Localy, the app is working fine:

enter image description here

But when I try to publish the app, I get following error:

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

The error is not telling to me, this is the complete code:

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)

And a sample of the input data:

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

like image 650
Luis Ramon Ramirez Rodriguez Avatar asked Sep 06 '25 10:09

Luis Ramon Ramirez Rodriguez


1 Answers

Just remember to include this line at the bottom of app.R:

shinyApp(ui = ui, server = server)

Minimal Reproducible Example

Here's a minimal shiny app (assembled directly from the code in the shiny tutorial).

If the last line, shinyApp(ui = ui, server = server), is removed, the app will give the error:

runApp()
Error in shinyApp(ui = ui, server = server) : object 'server' not found

But once shinyApp(ui = ui, server = server) is included it works as expected.

like image 93
stevec Avatar answered Sep 08 '25 01:09

stevec