Run pre-compiled stan model on Shinyapps.io

I tried a few ways of deploying a Stan program to shinyapps

  1. compiling on startup directly in shinyapps. This works, you need to have standard plan or higher so you can set 4 gb om ram (and the timeout at startup to its maximum 300 secs). So the app, stan program and tool chain works. But the startup get very slow and would probably time out for any serious Stan model.

  2. precompile with stan_model() on same x86_64-pc-linux-gnu platform before deploying, then sampling from the saved .rds model file. This results in following traceback when starting the app. Any idea what going on here? Is it some permission thing, that the Stan DSO doesn't get exec rights?

Traceback:
1: dyn.load(libLFile)
2: cxxfun_from_dso_bin(object)
4: grab_cxxfun(object@dso)
3: .local(object, ...)
5: grab_cxxfun(object@dso)
7: object@mk_cppmodule(object)
6: prep_call_sampler(object)
8: .local(object, ...)
10: sampling(stan_model, data = c(data_stan(), list(y = c(1, 1),     N = 2, y_sigma = 1)), chains = 1, iter = input$iter, warmup = as.integer(input$warmup/100 *     input$iter), control = list(metric = "diag_e"))
ā€¦
An irrecoverable exception occurred. R is aborting now ...
219: local({    if (identical(.Platform$OS.type, "unix")) {        whoami <- system("whoami", intern = TRUE)        if (identical(whoami, "root")) {            stop("Attempted to run application as whoami=", whoami,                 "; USER=", Sys.getenv("USER"))        }    }    fd <- file("stdin")    d <- read.dcf(fd)    close(fd)    config <- as.list(structure(as.vector(d), names = colnames(d)))    expectedFields <- c("ConnectDir", "Version", "InstalledRVersion",         "BundleRVersion", "AppDir", "BookmarkDir", "Host", "Port",         "SharedSecret", "WorkerId", "AppMode", "HTMLInclude",         "IgnorePackrat", "ErrorSanitization", "TraceFlushReact",         "Reconnect", "ReconnectMs", "DisabledProtocols", "TransportDebugging",         "ForcePandocVersion", "MinimumPandoc2RMarkdownVersion",         "InstalledPythonVersion")    missingFields <- setdiff(expectedFields, names(config))    unexpectedFields <- setdiff(names(config), expectedFields)    if (length(missingFields) != 0 || length(unexpectedFields) !=         0) {        fmt <- "Unexpected configuration; missing fields: %s; unexpected fields: %s"        stop(sprintf(fmt, paste0(missingFields, collapse = ", "),             paste0(unexpectedFields, collapse = ", ")))    }    PRODUCT_NAME <- "RStudio Connect"    MIN_R_VERSION <- "2.15.1"    MIN_SHINY_VERSION <- "0.7.0"    MIN_RMARKDOWN_VERSION <- "0.1.90"    MIN_KNITR_VERSION <- "1.5.32"    MIN_SHINY_RMARKDOWN_VERSION <- "0.9.1.9005"    source(file = file.path(config$ConnectDir, "R", "connect.R"),         local = TRUE)    connect$printOS(config)    connect$enforceMinimumRVersion(MIN_R_VERSION)    if (config$IgnorePackrat != "true") {        connect$enforceInstalledRVersion(config$InstalledRVersion)        connect$enforceAssumedRVersion(config$BundleRVersion)        connect$enforcePackratHealth()        connect$configureAppLibDir(config$AppDir)    }    connect$enforceInstalledPython(config$InstalledPythonVersion)    packages <- c("shiny", "httpuv", "rmarkdown", "knitr", "jsonlite",         "RJSONIO", "htmltools")    versions <- connect$getVersions(packages)    cat(paste("Server version: ", config$Version, "\n", sep = ""))    connect$printEnvironment()    connect$printVersions(packages, versions)    connect$checkReticulatePython()    connect$enforcePackageVersion("shiny", MIN_SHINY_VERSION,         versions$shiny)    if (identical(config$AppMode, "shinyrmd")) {        connect$enforcePackageVersion("rmarkdown", MIN_RMARKDOWN_VERSION,             versions$rmarkdown)        connect$enforceDependentPackageVersion("shiny", MIN_SHINY_RMARKDOWN_VERSION,             versions$shiny, "rmarkdown")        connect$enforceDependentPackageVersion("knitr", MIN_KNITR_VERSION,             versions$knitr, "rmarkdown")    }    connect$configurePandoc(config, versions)    connect$configureBrowseURL()    connect$fixupCrossPackageReferences()    library(shiny)    Sys.setenv(SHINY_PORT = config$Port, SHINY_SERVER_VERSION = config$Version)    options(shiny.sanitize.errors = (config$ErrorSanitization ==         "true"))    options(shiny.sharedSecret = config$SharedSecret)    if (exists("setServerInfo", envir = asNamespace("shiny"))) {        shiny:::setServerInfo(shinyServer = TRUE, version = config$Version,             edition = "Connect")    }    helpers <- connect$configureShinyHelpers(versions)    shinyFilter <- connect$configureShinyFilter(config, helpers,         versions)    options(shiny.http.response.filter = shinyFilter)    connect$configureShinyBookmarking(config$BookmarkDir)    port <- suppressWarnings(as.integer(config$Port))    if (is.na(port)) {        port <- config$Port        attr(port, "mask") <- strtoi("0077", 8)    }    cat(paste("\nStarting R with process ID: '", Sys.getpid(),         "'\n", sep = ""))    if (config$TraceFlushReact == "true") {        assign(".shiny__stdout", stdout(), envir = globalenv())    }    connect$retry(function() {        if (identical(config$AppMode, "shiny")) {            if ("host" %in% names(formals(shiny::runApp))) {                runApp(config$AppDir, host = config$Host, port = port,                   launch.browser = FALSE, workerId = config$WorkerId)            }            else {                runApp(config$AppDir, port = port, launch.browser = FALSE,                   workerId = config$WorkerId)            }        }        else if (identical(config$AppMode, "shinyrmd")) {            library(rmarkdown)            shiny_args <- list(port = port, launch.browser = FALSE,                 workerId = config$WorkerId)            if ("host" %in% names(formals(shiny::runApp))) {                shiny_args[["host"]] = config$Host            }            rmarkdown::run(file = NULL, dir = config$AppDir,                 shiny_args = shiny_args, render_args = if (compareVersion("1.7",                   versions$rmarkdown) <= 0)                   list(envir = globalenv()), auto_reload = FALSE)        }        else {            stop(paste("Unclear Shiny mode:", config$AppMode))        }    }, isRetryable = function(e) {        e$message == "Failed to create server"    }, delay = function(iter) {        (iter - 1) * 0.5    })    invisible(environment())})
217: eval(expr, p)
213: connect$retry(function() {    if (identical(config$AppMode, "shiny")) {        if ("host" %in% names(formals(shiny::runApp))) {            runApp(config$AppDir, host = config$Host, port = port,                 launch.browser = FALSE, workerId = config$WorkerId)        }        else {            runApp(config$AppDir, port = port, launch.browser = FALSE,                 workerId = config$WorkerId)        }    }    else if (identical(config$AppMode, "shinyrmd")) {        library(rmarkdown)        shiny_args <- list(port = port, launch.browser = FALSE,             workerId = config$WorkerId)        if ("host" %in% names(formals(shiny::runApp))) {            shiny_args[["host"]] = config$Host        }        rmarkdown::run(file = NULL, dir = config$AppDir, shiny_args = shiny_args,             render_args = if (compareVersion("1.7", versions$rmarkdown) <=                 0)                 list(envir = globalenv()), auto_reload = FALSE)    }    else {        stop(paste("Unclear Shiny mode:", config$AppMode))    }}, isRetryable = function(e) {    e$message == "Failed to create server"}, delay = function(iter) {    (iter - 1) * 0.5})
  1. make a r-package containing a precompiled stan model that get bundled if installed with install.github. I tired this with the new rstantools 2.0.0 package skeleton. I get the model to install and run on my own aws platform. But i can't deploy it to shinyapps.io as the Ā“configureĀ“ file doesn't have exec permission during the bundling (doesn't help giving the Ā“configureĀ“ exec permission as it gets moved to another directory during the bundling, whereby it looses its exec permission). If I remove the configure file then it deploys, but I get smilar error as in item 2 above (I guess the line rstantools::rstan_config() in the configure really needs to be run for setting up things correctly)

I would appreciate any help, especially on approach 2 as that is an smoother workflow as everything can be kept within one project.

I also posted on https://discourse.mc-stan.org/t/compile-stan-model-in-shiny-app/10022/10

Setting flags to CXXFLAGS = -g -O2 $(LTO) when pre-compiling the model worked

1 Like

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