Failing to load a library into shinyapp.io

Hello,

I am trying to upload a shiny app to shinyapp.io. If I run the commands locally, it works fine, but clicking 'Run App' in RStudio or uploading it to shinyapp.io gives an issue. I have located the offending issue to be a failure run library(survival). While I know shinyapp.io likes to use the CRAN repository, I believe this package to be on CRAN, which leaves me mighty confused. I see the package using available.packages() but not necessarily on https://cran.r-project.org/web/checks/check_summary_by_package.html. If the key is through rsconnect::appDependencies(), it appears there are too many files despite the minimal code run below:

Here's a minimal statement to induce the error:

 library(survival)
 simple_patient <- data.frame("event" = c(293, 627, 607, 691, 277, 652, 143, 684))
 surv_object <- Surv(time = simple_patient$event, event = rep(1, length(simple_patient$event)))

Thanks for any help, I'll be sure to mark an answer if I come across one for future readers.

Here is an app deployed with your code.
I found no issue in principle.
https://nguk.shinyapps.io/survapp/

library(shiny)

ui <- fluidPage(
  verbatimTextOutput("s")
)

server <- function(input, output, session) {
  library(survival)
  simple_patient <- data.frame("event" = c(293, 627, 607, 691, 277, 652, 143, 684))
  surv_object <- Surv(time = simple_patient$event, event = rep(1, length(simple_patient$event)))
  
  output$s <- renderPrint({
    surv_object
  })
}

shinyApp(ui, server)

Thanks,

I too just found a solution around what you suggested for the original code.

fit1 <- survfit(Surv(death, event) ~ 1, data = simple_patient)

If you're curious what you helped with, the alpha is now working:
https://pancreatic-cancer-survival-prediction.shinyapps.io/Simple_Only_Clinical_ML_Prediction/

I was hoping patients could upload the molecular features and get predictions, such as survival and predicted response to treatment, but the 1g RAM ceiling is mighty hard to deal with. As of now, it's only a very simple glm.

This topic was automatically closed 7 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.

Thanks for your help! It looks like the error was a bit further down. I've added it in conjunction with your code syntax. It looks like the error is associated with survminer::ggsurvplot() claiming that:

Error in FUN(X[[i]], ...) : object 'the_survival' not found

However, if I run the lines of code, it works! Just the 'Run App' and deployment appears to fail.

# Library prep
library(shiny)
library(survival)
library(survminer)

# Processing data
simple_patient <- data.frame("death" = c(293, 627, 607, 691, 277, 652, 143, 684))
simple_patient <- cbind(simple_patient, 'event' = rep(1, length(simple_patient$death)))
the_survival <- Surv(time = simple_patient$death, event = simple_patient$event)
fit1 <- survfit(the_survival ~ simple_patient$event, data = simple_patient)
test <-  survminer::ggsurvplot(fit1, pval = FALSE, data = simple_patient, xlab="Time (days)" )

# shiny time
ui <- fluidPage(
    plotOutput("s")
)

server <- function(input, output, session) {
  output$s <- renderPlot({test})
}

shinyApp(ui, server)

edit: I've been following resources for the function from here: https://www.datacamp.com/community/tutorials/survival-analysis-R and https://rpkgs.datanovia.com/survminer/survminer_cheatsheet.pdf
I could make a new question, but my confusion is why it works in RStudio in a fresh environment, but not when 'Run App' in clicked

Thanks again,
Ryan

I would guess ggsurvplot needs ggplot, based on its name.
It's in the list of installed packages on your local computer, but when uploading it to shinyapps.io it doesn't recognise this dependency.
Try to add ggplot2 to the libraries and check if it's working afterwards.

Not super clear to me whats happening, as I havent tried to deploy, but the runApp button on your version of the code does execute for me and show me a chart....

If I had to guess though, I'd look here:

I would think that the problem is probably with this syntax.
normally in a function that allows formulas, you would either construct the formula from independent elements
fit1 <- survfit(the_survival ~ simple_patient$event)
and not pass a data parm that serves no purpose....
or else, you use the data parm, but you make sure that the forumula variables you want to use are in that data object

simple_patient$the_survival <- Surv(time = simple_patient$death, event = simple_patient$event)
fit1 <- survfit(the_survival ~ event, data = simple_patient)

I can't vouch for this being the reason, as its not an impediment for my R session to run your code as it is.... but I would check this out for yourself

Thanks for the reply. I've re-installed RStudio, but still no luck. Running it in regular R works (locally), but the server deploy has the same issue that RStudio reports as if I had clicked the 'Run App' button. It appears if I run the code and then click the 'Run App' button, it works (locally), but won't deploy to shinyapp. If I clean my environment, it won't run again (locally).

Since the demo code I posted above wasn't working, I took the code directly from the tutorial site and it still doesn't work. This must be some environment issue? Any suggestions to get around it? I'm about ready to forfeit shiny if the same code on two machines works differently.

# Library prep
library(shiny)
library(survival)
library(survminer)
library(dplyr)
library(ggplot2)

# Code taken from https://www.datacamp.com/community/tutorials/survival-analysis-R
data(ovarian)
ovarian$rx <- factor(ovarian$rx, 
                     levels = c("1", "2"), 
                     labels = c("A", "B"))
ovarian$resid.ds <- factor(ovarian$resid.ds, 
                           levels = c("1", "2"), 
                           labels = c("no", "yes"))
ovarian$ecog.ps <- factor(ovarian$ecog.ps, 
                          levels = c("1", "2"), 
                          labels = c("good", "bad"))
ovarian <- ovarian %>% mutate(age_group = ifelse(age >=50, "old", "young"))
ovarian$age_group <- factor(ovarian$age_group)
surv_object <- Surv(time = ovarian$futime, event = ovarian$fustat)
fit1 <- survfit(surv_object ~ rx, data = ovarian)
test <- ggsurvplot(fit1, data = ovarian, pval = TRUE)

# shiny time
ui <- fluidPage(
    plotOutput("s")
)

server <- function(input, output, session) {
    output$s <- renderPlot({test})
}

shinyApp(ui, server)

If I save the file as app.R to get the Run App button in Rstudio, then I can confirm the code you shared just now fails with the same object error missing.
However, the fix seems to be along the lines I suggested, as with these modifications the Run App button works without errors

# Library prep
library(shiny)
library(survival)
library(survminer)
library(dplyr)
library(ggplot2)

# Code taken from https://www.datacamp.com/community/tutorials/survival-analysis-R
data(ovarian)
ovarian$rx <- factor(ovarian$rx, 
                     levels = c("1", "2"), 
                     labels = c("A", "B"))
ovarian$resid.ds <- factor(ovarian$resid.ds, 
                           levels = c("1", "2"), 
                           labels = c("no", "yes"))
ovarian$ecog.ps <- factor(ovarian$ecog.ps, 
                          levels = c("1", "2"), 
                          labels = c("good", "bad"))
ovarian <- ovarian %>% mutate(age_group = ifelse(age >=50, "old", "young"))
ovarian$age_group <- factor(ovarian$age_group)
ovarian$surv_object_var <- Surv(time = ovarian$futime, event = ovarian$fustat)
fit1 <- survfit(surv_object_var ~ rx, data = ovarian)
test <- ggsurvplot(fit1, data = ovarian, pval = TRUE)

# shiny time
ui <- fluidPage(
  plotOutput("s")
)

server <- function(input, output, session) {
  output$s <- renderPlot({test})
}

shinyApp(ui, server)