Shiny app crashes when deployed. Has to do with python xgboost package

...Looking for a solution. I'm deploying a shiny app using python code with virtualenv and reticulate. The app is running fine locally on my mac, but it sometimes works when deployed but oftentime crashes. When I check the shiny app logs it seems to be killed right after loading xgboost. When I remove xgboost from the packages to be loaded to shiny, it works fine and doesn't crash.

Please share Repex so that the community can assist

cheers

Here is the server.R and ui.R shiny code

# Import R packages needed for the app here:
library(shiny)
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable
library(RColorBrewer)

options(shiny.trace = FALSE)
# Define any Python packages needed for the app here:
PYTHON_DEPENDENCIES = c('pip', 'numpy', 'pandas', 'sklearn',
                        'matplotlib','xgboost','datetime')

# Begin app server
shinyServer(function(input, output) {
  filedata <- reactive({
    infile <- input$file1
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath)
  })
  # ------------------ App virtualenv setup (Do not edit) ------------------- #
  
  virtualenv_dir = Sys.getenv('VIRTUALENV_NAME')
  python_path = Sys.getenv('PYTHON_PATH')
  
  # Create virtual env and install dependencies
  reticulate::virtualenv_create(envname = virtualenv_dir, python = python_path)
  reticulate::virtualenv_install(virtualenv_dir, packages = PYTHON_DEPENDENCIES, ignore_installed=TRUE)
  reticulate::use_virtualenv(virtualenv_dir, required = T)
  
  # ------------------ App server logic (Edit anything below) --------------- #
  
  #plot_cols <- brewer.pal(7, 'BrBG')
  
  # Import python functions to R
  reticulate::source_python('python_functions.py')
  
  # Generate the requested distribution
  #d <- reactive({
    #dist <- switch(input$dist,
                   #norm = rnorm,
                   #unif = runif,
                   #lnorm = rlnorm,
                   #exp = rexp,
                   #rnorm)
    
    #return(dist(input$n))
  #})
  
  #display table on main panel
  output$contents <- renderTable({
    df <- filedata()
    df <- na.omit(df)
    if (is.null(df)) return(NULL)
    
    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }
    
  })
  
  output$outcomeVar <- renderUI({
    df <- filedata()
    df <- na.omit(df)
    if (is.null(df)) return(NULL)
    if(apply(df,2, is.character) == TRUE){
      createAlert(session, "alert", "exampleAlert", title = "CAUTION",
                  content = "You must use only numeric or integer variables in your model. No strings, please.",
                  append = FALSE)}
    
    
    items=names(df)
    #items[1] = "id"
    names(items)=items
    selectInput("outcomeVar","Select ONE variable as dependent variable from:",items)
  })
  
  output$independents <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    #items[1] = "id"
    checkboxGroupInput('independents','Select the regressors (exclude dependent/outcome variable)', choices = items)
  })
  
  output$treat <- renderUI({
    df <- filedata()
    df <- na.omit(df)
    if (is.null(df)) return(NULL)
    if(apply(df,2, is.character) == TRUE){
      createAlert(session, "alert", "exampleAlert", title = "CAUTION",
                  content = "You must use only numeric or integer variables in your model. No strings, please.",
                  append = FALSE)}
    
    items=names(df)
    #items[1] = "id"
    names(items)=items
    selectInput("treat","Select ONE variable as treatment variable from:",items)
  })
  
  model4 <-  eventReactive (input$random,{
    data4    <- filedata()
    data4    <- na.omit(data4)
    data4    <- na.omit(data4)
    varNames <- colnames(data4)
    n        =  nrow(data4)
    
    outVar = input$outcomeVar
    data4 <- data4[,c(unlist(input$independents),outVar)]
    colnames(data4)[length(data4)] = "Y"
    data5 <- as.data.frame(data4)
    formula <- as.formula(paste("Y~", paste(input$independents,collapse = "+")))
    lmMod  <- lm(formula, data=data5)
    
    if(length(input$independents)<2){
      print("Please choose more than 1 regressor variable!")
      return()}
    
    summary(lmMod)
    #print(newFormula)
})

diagMod <- eventReactive(input$random,{
  logRegFunc(filedata(),input$outcomeVar,input$independents,input$treat)
  })

plotMod <- eventReactive(input$random,{
  plotFunc(filedata(),input$outcomeVar,input$independents,input$treat)
  })


output$linearModel <- renderPrint({
    if (is.null(df)) return(NULL)
    model4()
    })
  
output$testFunc <- renderPrint({
    if(is.null(df)) return(NULL)
    diagMod()
    })



#output$matPlot <- renderPlot({
#  if(is.null(df)) return(NULL)
#  plotMod()
#  #img(src = 'myplot.png')
#})

output$matPlot <- renderImage({
  if(is.null(df)) return(NULL)
  plotMod()
  list(src = 'myplot3.png')
})

output$down <- downloadHandler (
  filename = function(){
    paste("myplot3", "png", sep=".")
  },
  content = function(file){
    file.copy("myplot3.png",file)
  }
)
  # Generate a plot of the data
  #output$plot <- renderPlot({
    #dist <- input$dist
    #n <- input$n
    
    #return(hist(d(),
                #main = paste0('Distribution plot: ', dist, '(n = ', n, ')'),
                #xlab = '',
                #col = plot_cols))
  #})
  
  # Test that the Python functions have been imported
  #output$message <- renderText({
    #return(test_string_function(input$str))
  #})
  
  # Test that numpy function can be used
  #output$xy <- renderText({
    #z = test_numpy_function(input$x, input$y)
    #return(paste0('x + y = ', z))
  #})
  
  # Display info about the system running the code
  output$sysinfo <- DT::renderDataTable({
    s = Sys.info()
    df = data.frame(Info_Field = names(s),
                    Current_System_Setting = as.character(s))
    return(datatable(df, rownames = F, selection = 'none',
                     style = 'bootstrap', filter = 'none', options = list(dom = 't')))
  })
  
  # Display system path to python
  output$which_python <- renderText({
    paste0('which python: ', Sys.which('python'))
  })
  
  # Display Python version
  output$python_version <- renderText({
    rr = reticulate::py_discover_config(use_environment = 'python35_env')
    paste0('Python version: ', rr$version)
  })
  
  # Display RETICULATE_PYTHON
  output$ret_env_var <- renderText({
    paste0('RETICULATE_PYTHON: ', Sys.getenv('RETICULATE_PYTHON'))
  })
  
  # Display virtualenv root
  output$venv_root <- renderText({
    paste0('virtualenv root: ', reticulate::virtualenv_root())
  })
  
})

Created on 2022-04-18 by the reprex package (v2.0.1)

# Import R packages needed for the UI
library(shiny)
library(shinycssloaders)
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable

# Begin UI for the R + reticulate example app
ui <- fluidPage(
  
  titlePanel('Matched Data Evaluation and Variable Importance'),
  
  sidebarLayout(
    
    # ---------------- Sidebar panel with changeable inputs ----------------- #
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),
      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head"),
      br(),
      
      h5("Select Dependent and Independent Variables"),
      tags$hr(),
      uiOutput("outcomeVar"),
      tags$hr(),
      uiOutput("independents"),
      tags$hr(),
      uiOutput("treat"),
      tags$hr(),
      actionButton("random", "Estimate linear model"),
      tags$hr(),
      radioButtons(inputId = "var3", label = "Select type of file", choices = list("png")),
      tags$hr(),
      downloadButton("down", label = "Download the plot")
      ),
    
    # ---------------- Sidebar panel with changeable inputs ----------------- #
    mainPanel(
      
      # Output: Tabset w/ plot, summary, and table ----
      tabsetPanel(type = 'tabs',
                  tabPanel('Using R and Python functions', 
                           br(),
                           tableOutput("contents"),
                           br(),
                           h3('Here is logistic regression in python'),
                           verbatimTextOutput("testFunc")),
                           #h3('Outputs generated with pure R'),
                           #br(),
                           #withSpinner(plotOutput('plot')),
                           #br(),
                           #h3('Outputs generated by Python functions via reticulate'),
                           #verbatimTextOutput('message'),
                           #br(),
                           #'Use the numpy Python package to add two numbers',
                           #verbatimTextOutput('xy')),
                           
                  tabPanel('Architecture Info', 
                           h3('Current architecture info'),
                           '(These values will change when app is run locally vs on Shinyapps.io)',
                           hr(),
                           withSpinner(DT::dataTableOutput('sysinfo')),
                           br(),
                           verbatimTextOutput('which_python'),
                           verbatimTextOutput('python_version'),
                           verbatimTextOutput('ret_env_var'),
                           verbatimTextOutput('venv_root')),
                  
                  tabPanel('Summary of linear model',
                           h4("Here is a summary of linear model"),
                           verbatimTextOutput("linearModel")),
                  
                  #tabPanel('Matplot of ROC',
                  #        h4("Receiver Operating Characteristic (ROC)
                  #           Curve of Signal Strength of treatment "),
                  #         withSpinner(plotOutput('matPlot'))
                  tabPanel('Matplot of ROC',
                           h4("Receiver Operating Characteristic (ROC)
                              Curve of Signal Strength of treatment"),
                           imageOutput('matPlot'),
                          
                  ),
                  
      )
    )
  )
)

Created on 2022-04-18 by the reprex package (v2.0.1)

Here is the shiny app logs
2022-04-18T13:23:54.366487+00:00 shinyapps[6021658]: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.5/98.5 KB 25.9 MB/s eta 0:00:00
2022-04-18T13:23:54.366592+00:00 shinyapps[6021658]: Downloading cycler-0.11.0-py3-none-any.whl (6.4 kB)
2022-04-18T13:23:54.366640+00:00 shinyapps[6021658]: Collecting fonttools>=4.22.0
2022-04-18T13:23:54.366839+00:00 shinyapps[6021658]: Downloading scipy-1.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (41.6 MB)
2022-04-18T13:23:55.384054+00:00 shinyapps[6021658]: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 41.6/41.6 MB 10.3 MB/s eta 0:00:00
2022-04-18T13:23:56.366160+00:00 shinyapps[6021658]: Collecting zope.interface
2022-04-18T13:23:56.366242+00:00 shinyapps[6021658]: Downloading zope.interface-5.4.0-cp38-cp38-manylinux2010_x86_64.whl (259 kB)
2022-04-18T13:23:56.366767+00:00 shinyapps[6021658]: Collecting setuptools
2022-04-18T13:23:56.366302+00:00 shinyapps[6021658]: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 259.2/259.2 KB 47.3 MB/s eta 0:00:00
2022-04-18T13:23:56.366819+00:00 shinyapps[6021658]: Using cached setuptools-62.1.0-py3-none-any.whl (1.1 MB)
2022-04-18T13:23:56.366398+00:00 shinyapps[6021658]: Collecting six>=1.5
2022-04-18T13:23:56.366866+00:00 shinyapps[6021658]: Building wheels for collected packages: sklearn
2022-04-18T13:23:56.366461+00:00 shinyapps[6021658]: Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
2022-04-18T13:23:56.366521+00:00 shinyapps[6021658]: Collecting threadpoolctl>=2.0.0
2022-04-18T13:23:56.366921+00:00 shinyapps[6021658]: Building wheel for sklearn (setup.py): started
2022-04-18T13:23:56.366570+00:00 shinyapps[6021658]: Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
2022-04-18T13:23:56.366672+00:00 shinyapps[6021658]: Downloading joblib-1.1.0-py2.py3-none-any.whl (306 kB)
2022-04-18T13:23:56.366622+00:00 shinyapps[6021658]: Collecting joblib>=0.11
2022-04-18T13:23:56.366720+00:00 shinyapps[6021658]: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 307.0/307.0 KB 52.0 MB/s eta 0:00:00
2022-04-18T13:23:58.366144+00:00 shinyapps[6021658]: Building wheel for sklearn (setup.py): finished with status 'done'
2022-04-18T13:23:58.366251+00:00 shinyapps[6021658]: Created wheel for sklearn: filename=sklearn-0.0-py2.py3-none-any.whl size=1310 sha256=8defb1318353b43d6609b84d1a8f551559322f19ecba7cd5d65591fab98a6d5e
2022-04-18T13:23:58.366315+00:00 shinyapps[6021658]: Stored in directory: /home/shiny/.cache/pip/wheels/22/0b/40/fd3f795caaa1fb4c6cb738bc1f56100be1e57da95849bfc897
2022-04-18T13:23:58.366404+00:00 shinyapps[6021658]: Successfully built sklearn
2022-04-18T13:23:58.366462+00:00 shinyapps[6021658]: Installing collected packages: pytz, threadpoolctl, six, setuptools, pyparsing, pip, pillow, numpy, kiwisolver, joblib, fonttools, cycler, zope.interface, scipy, python-dateutil, packaging, xgboost, scikit-learn, pandas, matplotlib, datetime, sklearn
2022-04-18T13:24:18.366286+00:00 shinyapps[6021658]: Successfully installed cycler-0.11.0 datetime-4.4 fonttools-4.32.0 joblib-1.1.0 kiwisolver-1.4.2 matplotlib-3.5.1 numpy-1.22.3 packaging-21.3 pandas-1.4.2 pillow-9.1.0 pip-22.0.4 pyparsing-3.0.8 python-dateutil-2.8.2 pytz-2022.1 scikit-learn-1.0.2 scipy-1.8.0 setuptools-62.1.0 six-1.16.0 sklearn-0.0 threadpoolctl-3.1.0 xgboost-1.6.0 zope.interface-5.4.0
2022-04-18T13:24:26.366129+00:00 shinyapps[6021658]: Matplotlib is building the font cache; this may take a moment.
2022-04-18T13:24:30.366313+00:00 shinyapps[6021658]: the condition has length > 1 and only the first element will be used
2022-04-18T13:24:30.366166+00:00 shinyapps[6021658]: Warning in if (apply(df, 2, is.character) == TRUE) { :
2022-04-18T13:24:30.366400+00:00 shinyapps[6021658]: Warning in if (apply(df, 2, is.character) == TRUE) { :
2022-04-18T13:24:30.366471+00:00 shinyapps[6021658]: the condition has length > 1 and only the first element will be used
2022-04-18T13:24:51.366143+00:00 shinyapps[6021658]: Warning: The renderImage output named 'matPlot' is missing the deleteFile argument; as of Shiny 1.5.0, you must use deleteFile=TRUE or deleteFile=FALSE. (This warning will become an error in a future version of Shiny.)
2022-04-18T13:24:58.366267+00:00 shinyapps[6021658]: virtualenv: example_env_name
2022-04-18T13:24:58.366439+00:00 shinyapps[6021658]: Using virtual environment 'example_env_name' ...
2022-04-18T13:24:59.388082+00:00 shinyapps[6021658]: Collecting pip
2022-04-18T13:24:59.388142+00:00 shinyapps[6021658]: Using cached pip-22.0.4-py3-none-any.whl (2.1 MB)
2022-04-18T13:24:59.388188+00:00 shinyapps[6021658]: Collecting numpy
2022-04-18T13:25:00.366152+00:00 shinyapps[6021658]: Using cached numpy-1.22.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.8 MB)
2022-04-18T13:25:00.366225+00:00 shinyapps[6021658]: Collecting pandas
2022-04-18T13:25:00.366437+00:00 shinyapps[6021658]: Collecting sklearn
2022-04-18T13:25:00.366494+00:00 shinyapps[6021658]: Using cached sklearn-0.0-py2.py3-none-any.whl
2022-04-18T13:25:00.366557+00:00 shinyapps[6021658]: Collecting matplotlib
2022-04-18T13:25:00.366277+00:00 shinyapps[6021658]: Using cached pandas-1.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.7 MB)
2022-04-18T13:25:01.366277+00:00 shinyapps[6021658]: Using cached matplotlib-3.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.3 MB)
2022-04-18T13:25:01.366514+00:00 shinyapps[6021658]: Collecting xgboost
2022-04-18T13:25:05.366224+00:00 shinyapps[6021658]: Warning: Error in : Error installing package(s): "'pip'", "'numpy'", "'pandas'", "'sklearn'", "'matplotlib'", "'xgboost'", "'datetime'"
2022-04-18T13:25:05.366136+00:00 shinyapps[6021658]: Killed
2022-04-18T13:25:05.366280+00:00 shinyapps[6021658]: 64: stop
2022-04-18T13:25:05.366333+00:00 shinyapps[6021658]: 63: pip_install
2022-04-18T13:25:05.366418+00:00 shinyapps[6021658]: 62: reticulate::virtualenv_install
2022-04-18T13:25:05.366466+00:00 shinyapps[6021658]: 61: server [/srv/connect/apps/pythonAndR/server.R#27]
2022-04-18T13:25:05.366518+00:00 shinyapps[6021658]: Error : Error installing package(s): "'pip'", "'numpy'", "'pandas'", "'sklearn'", "'matplotlib'", "'xgboost'", "'datetime'"

Thank you.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.