App can't find loaded-in R objects in shinyapps.io, despite running locally and successful deployment with rsconnect

Hello,

I recently tried to deploy a Shiny app to shinyapps.io via rsconnect. I can run the app locally and get successful deployment with rsconnect, but I get an error log on shinyapps.io (see below) saying it can't find objects that are loaded into the R environment as part of the app.R code (see below). Specifically, it can't find the "markergenes" object, which is made from the "data_matrix" object that is loaded in with the "shiny_objects_3_1231.rdata" file. From a reply to my post on another forum, I think the problem is that I may need to specify the directory elsewhere (perhaps in my deployApp call to rsconnect) and possibly set a couple things in my .Rprofile (which I haven't done before) instead of within the app.R code.

Is the error with loading R objects in my app.R code when deploying just a directory issue? I assumed they would all go into the server directory after being bundled with rsconnect, but if there's a different way I need to specify the directory, I can make whatever changes are needed.

Thanks in advance!

shinyapps.io error log

2025-11-17T19:19:25.845897+00:00 shinyapps[15836180]: Running on host: b353779457f3
2025-11-17T19:19:25.851464+00:00 shinyapps[15836180]: Process ID: 128
2025-11-17T19:19:25.857023+00:00 shinyapps[15836180]: Running as user: uid=10001(shiny) gid=10001(shiny) groups=10001(shiny)
2025-11-17T19:19:25.862672+00:00 shinyapps[15836180]: Connect version: 2025.09.1
2025-11-17T19:19:25.868312+00:00 shinyapps[15836180]: LANG: C.UTF-8
2025-11-17T19:19:25.873746+00:00 shinyapps[15836180]: Working directory: /srv/connect/apps/epilepsy_pbmc
2025-11-17T19:19:25.879389+00:00 shinyapps[15836180]: Using R 4.5.1
2025-11-17T19:19:25.884658+00:00 shinyapps[15836180]: R.home(): /opt/R/4.5.1/lib/R
2025-11-17T19:19:25.893113+00:00 shinyapps[15836180]: Content will use current R environment
2025-11-17T19:19:25.898397+00:00 shinyapps[15836180]: R_LIBS: (unset)
2025-11-17T19:19:25.903984+00:00 shinyapps[15836180]: .libPaths(): /usr/lib/R, /opt/R/4.5.1/lib/R/library
2025-11-17T19:19:25.909745+00:00 shinyapps[15836180]: shiny version: 1.11.1
2025-11-17T19:19:25.915354+00:00 shinyapps[15836180]: httpuv version: 1.6.16
2025-11-17T19:19:25.921686+00:00 shinyapps[15836180]: rmarkdown version: 2.30
2025-11-17T19:19:25.927797+00:00 shinyapps[15836180]: knitr version: 1.50
2025-11-17T19:19:25.933257+00:00 shinyapps[15836180]: jsonlite version: 2.0.0
2025-11-17T19:19:25.938914+00:00 shinyapps[15836180]: RJSONIO version: (none)
2025-11-17T19:19:25.944115+00:00 shinyapps[15836180]: htmltools version: 0.5.8.1
2025-11-17T19:19:25.949400+00:00 shinyapps[15836180]: reticulate version: 1.44.0
2025-11-17T19:19:25.954814+00:00 shinyapps[15836180]: Warning: The reticulate package is installed but no Python environment is available.
2025-11-17T19:19:25.960215+00:00 shinyapps[15836180]: Did you forget to set the RETICULATE_PYTHON environment variable in your .Rprofile before publishing?
2025-11-17T19:19:25.966012+00:00 shinyapps[15836180]: Using pandoc: /opt/connect/ext/pandoc/2.16
2025-11-17T19:19:25.971457+00:00 shinyapps[15836180]: Shiny application starting ...
2025-11-17T19:19:25.976788+00:00 shinyapps[15836180]: 
2025-11-17T19:19:25.982518+00:00 shinyapps[15836180]: Listening on http://127.0.0.1:39523
2025-11-17T19:19:25.987939+00:00 shinyapps[15836180]: Warning: Error in eval: object 'markergenes' not found
2025-11-17T19:19:25.993261+00:00 shinyapps[15836180]:   108: hasGroups
2025-11-17T19:19:25.998462+00:00 shinyapps[15836180]:   107: choicesWithNames
2025-11-17T19:19:26.004077+00:00 shinyapps[15836180]:   106: selectInput
2025-11-17T19:19:26.009244+00:00 shinyapps[15836180]:   101: selectizeInput
2025-11-17T19:19:26.014686+00:00 shinyapps[15836180]:    15: <Anonymous>
2025-11-17T19:19:26.020184+00:00 shinyapps[15836180]:    13: fn
2025-11-17T19:19:26.025695+00:00 shinyapps[15836180]:     8: retry
2025-11-17T19:19:26.031297+00:00 shinyapps[15836180]:     7: connect$retryingStartServer
2025-11-17T19:19:26.037097+00:00 shinyapps[15836180]:     6: eval
2025-11-17T19:19:26.042545+00:00 shinyapps[15836180]:     5: eval
2025-11-17T19:19:26.047774+00:00 shinyapps[15836180]:     4: eval
2025-11-17T19:19:26.052978+00:00 shinyapps[15836180]:     3: eval
2025-11-17T19:19:26.058516+00:00 shinyapps[15836180]:     2: eval.parent
2025-11-17T19:19:26.063680+00:00 shinyapps[15836180]:     1: local

app.R code

setwd("C:/Users/dalli/Documents/Epilepsy data/Shiny app/PBMC")

library(ggplot2)
library(shiny)
library(tidyverse)
library(RColorBrewer)
library(patchwork)
library(Seurat)
library(ggrepel)
library(DT)
library(ggpubr)
library(dplyr)
library(gridExtra)
library(visNetwork)
library(bslib)
library(stringr)
library(CellChat)
library(cowplot)
library(scRepertoire)
library(ggiraph)
library(reticulate)

#############
#tags$hr(style="border-color: white;")

options(rsconnect.max.bundle.size = 10000*1024^2)
options(rsconnect.http.verbose = TRUE)
load(file = "shiny_objects_3_1231.rdata")
load(file="MASC_plots_tables.RData")
markergenes <- rownames(data_matrix)

load("Seurat.object.with.TCR.antigen.specificity.RData")
load("Cellchat.new.Seurat.object.l2.HC.RData")
load("Cellchat.new.Seurat.object.l2.PC.RData")
load("Cellchat.new.Seurat.object.l2.WC.RData")
load("cellchat.new.l2.HC.WC.RData")
load("cellchat.new.l2.HC.PC.RData")
load("cellchat.new.l2.WC.PC.RData")
cellchatpathways <- unique(c(cellchat0@netP$pathways, cellchat1@netP$pathways,
                             cellchat2@netP$pathways, cellchatl2hc@netP$HC$pathways,
                             cellchatl2hc@netP$WC$pathways, cellchatl2hr@netP$HC$pathways,
                             cellchatl2hr@netP$PC$pathways, cellchatl2cr@netP$WC$pathways,
                             cellchatl2cr@netP$PC$pathways))
cellchatl2hc <- liftCellChat(cellchatl2hc)
cellchatl2hr <- liftCellChat(cellchatl2hr)
cellchatl2cr <- liftCellChat(cellchatl2cr, group.new = levels(cellchatl2cr@idents$WC))
cellchatcompositelist <- c(cellchatl2hc, cellchatl2hr, cellchatl2cr)
names(cellchatcompositelist) <- c("HC vs WCE", "HC vs PCE", "WCE vs PCE")
cell_meta$TCR.cloneSize <- seurat@meta.data$cloneType

#Set factor levels of input objects and variables
l1_levels <- c("B", "Mono", "CD4 T", "CD8 T", "NK", "DC", "other T", "other")
l2_levels <- c("B naive", "B intermediate", "B memory", "Plasmablast", "CD14 Mono", "CD16 Mono",
               "CD4 Naive", "CD4 TCM", "CD4 TEM", "CD4 CTL", "Treg", "CD8 Naive",
               "CD8 TCM", "CD8 TEM", "gdT", "MAIT", "dnT", "CD4 Proliferating",
               "CD8 Proliferating", "NK", "NK_CD56bright", "NK Proliferating",
               "cDC2", "pDC", "Platelet", "HSPC", "ILC", "ASDC")
cell_meta$predicted.celltype.l1 <- factor(cell_meta$predicted.celltype.l1,
                                          levels = l1_levels)
cell_meta$predicted.celltype.l2 <- factor(cell_meta$predicted.celltype.l2,
                                          levels = l2_levels)
seurat@meta.data$predicted.celltype.l1 <- factor(seurat@meta.data$predicted.celltype.l1,
                                          levels = c("B", "Mono", "CD4T", "CD8T", "NK", "DC", "otherT", "other"))
seurat@meta.data$predicted.celltype.l2 <- factor(seurat@meta.data$predicted.celltype.l2,
                                          levels = c("Bnaive", "Bintermediate", "Bmemory", "Plasmablast", "CD14Mono", "CD16Mono",
                                                     "CD4Naive", "CD4TCM", "CD4TEM", "CD4CTL", "Treg", "CD8Naive",
                                                     "CD8TCM", "CD8TEM", "gdT", "MAIT", "dnT", "CD4Proliferating",
                                                     "CD8Proliferating", "NK", "NKCD56bright", "NKProliferating",
                                                     "cDC2", "pDC", "Platelet", "HSPC", "ILC", "ASDC"))

#Edit MC to WCE and MR to PCE in metadata
cell_meta$Condition <- str_replace(cell_meta$Condition, "MC", "WCE")
cell_meta$Condition <- str_replace(cell_meta$Condition, "MR", "PCE")
l1_l2$Condition <- str_replace(l1_l2$Condition, "MC", "WCE")
l1_l2$Condition <- str_replace(l1_l2$Condition, "MR", "PCE")
lis <- list(c("HC", "WCE"), c("HC", "PCE"), c("WCE", "PCE"))
MASCinput$Group <- str_replace(MASCinput$Group, "MC", "WCE")
MASCinput$Group <- str_replace(MASCinput$Group, "MR", "PCE")
pair_df$exclude <- str_replace(pair_df$exclude, "MC", "WCE")
pair_df$exclude <- str_replace(pair_df$exclude, "MR", "PCE")
pair_df$exclude <- factor(pair_df$exclude, levels = c("HC vs WCE", "HC vs PCE", "PCE vs WCE"))
PCWCL1$Group <- str_replace(PCWCL1$Group, "MC", "WCE")
PCWCL1$Group <- str_replace(PCWCL1$Group, "MR", "PCE")
l1_l2$Condition <- str_replace(l1_l2$Condition, "MC", "WCE")
l1_l2$Condition <- str_replace(l1_l2$Condition, "MR", "PCE")
summarized_data$exclude <- str_replace(summarized_data$exclude, "MC", "WCE")
summarized_data$exclude <- str_replace(summarized_data$exclude, "MR", "PCE")
summarized_data$exclude <- factor(summarized_data$exclude,
                                  levels = c("HC vs WCE", "HC vs PCE", "PCE vs WCE"))

source("ui.R")
source("server.R")

rsconnect::deployApp(appName = "epilepsy_pbmc",
                     account = "elyamanlab")

You have a call to setwd but that directory will not exist in shinyapps.io. You could try removing that and see what happens.

In addition to removing setwd(), it is probably easier if you deploy via the GUI which you can access when you run the app in RStudio. In the top right of the window containing the app you'll see a "Publish" button which makes the process straightforward in terms of seeing which files you are uploading.

You should also remove deployApp()- you want to run that on your computer to send the code to the server, but you don't want the server to try and run that again.

In terms of how you structure your project, file paths are all relative to the parent directory so it looks as if they should work okay.

We normally create ui.R and server.R or just app.R and I'm not sure what happens if you try to deploy all three at once. I'm also confused by your app.R file as it doesn't contain a shinyApp() object. If you have ui.R and server.R, then move all the code from app.R into server.R and delete app.R to create a more typical structure. You can also lose remove the source() as Shiny handles that automatically.

I removed the call to setwd, removed deployApp, moved all code from app.R into server.R, added a line to create a shinyApp object, and deployed via RStudio's GUI, and still got the same error in the log where it can't find an object that is derived from one of the objects loaded in with the code.

Hmm. Is the code on github / can you put it there? Have you tried clearing your R session (i.e. so that your environment is empty) before you run it locally?

I tried running it from a fresh R session and got the same error in the logs. I put the server code (which also contains the code I copied over from app.R) and the ui code on Github at GitHub - ddressman91/Epilepsy_PBMC_brain: Code used to analyze data from plasma, PBMCs, and brain tissue from epilepsy patients and healthy controls

Thanks for sharing the code. First up - you don't need to put rsconnect::setAccountInfo() in your app - you run this once on your own machine so that the deploy functions know where to send your code. You've included the key and token on GitHub so you should probably consider those details compromised now and create a new token over at shinyapps.io.

Now onto the problem - I can't be 100 % sure as I can't run your code, but I have replicated the same problem with this minimal example:

server.R:

marker_genes <- c("a", "b", "c")

function(input, output, session) {
}

ui.R:

fluidPage(
  selectInput("test", "Test", choices = marker_genes)
)

Because marker_genes is being defined in server.R but also being used in ui.R it cannot be found in ui.R resulting in the error. The solution is to move all of your code above this line: Epilepsy_PBMC_brain/server.R at 0e9a257e77ecc431c3c51eceb04956b9c649c14c · ddressman91/Epilepsy_PBMC_brain · GitHub to a new file called global.R.

Code in global.R is run before that in ui.R and server.R and crucially the variable are then available in both files. You can read more at Shiny - Scoping rules for Shiny apps or The awesomeness that is the global.R file. Or how to clean up your shiny app

Continuing the example, see:

global.R:

marker_genes <- c("a", "b", "c")

server.R:


function(input, output, session) {
  output$out <- renderText(marker_genes)
}

ui.R:

fluidPage(
  selectInput("test", "Test", choices = marker_genes),
  textOutput("out")
)

An alternative to using a global.R file is to put the entire app in a single file (typically named app.R) and put the line creating marker_genes in the global scope. Here's an example.

library(shiny)

marker_genes <- c("a", "b", "c")

ui <- fluidPage(
  selectInput("test", "Test", choices = marker_genes),
  textOutput("out")
)

server <- function(input, output, session) {
  output$out <- renderText(input$test)
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:6311

Created on 2025-12-08 with reprex v2.1.1

Thanks for catching the token issue, I have created a new token and am using that.

I wasn't aware of the global.R file. It looks like it does what I expected app.R to do, except that it handles cases where variables are called in both the server and the ui. I redeployed the app with the global.R file, and now it's getting oom killed according to the logs. After getting the first oom kill, I've tried configuring the app up to 3XL with the size argument of the rsconnect::configureApp() function, but the same thing happened. The total file size of all my input files for the Shiny app is around 3.660 Gb, and the memory usage report in RStudio shows about that same amount in GiB for the session after loading all files into the RStudio environment. Our lab's shinyapps.io account is at the basic level and should have 8 Gb of RAM available, so why is the app still getting oom killed?

Glad you've made some progress. Unfortunately OOM errors are the bane of my existence too and I don't have any neat solutions. RAM usage though is definitely much more complex than just how much data is being loaded initially, but if you are loading 3.6 GB, it is not too surprising that you run out of memory when you try to do something with that data.

The first thing I'd suggest is checking your system performance in task manager / equivalent when you run the app locally - I suspect you'll see you're using > 8 GB locally too.

In terms of changes to your code, do you need all that data at once in the app, or would a user only look at some of it at a time? If that's the case then you could investigate putting the data in a database (hosted somewhere else) and then the app would use less RAM.

If you can't change the code / amount of data, then you could try using Posit Connect Cloud instead of shinyapps.io. The enhanced plan has 16GB of RAM but is $59 a month. Alternatively, dockerise and host in the cloud somewhere, but it can be tricky to find high memory options. Probably worth asking your institution's IT department about it too as they may have servers you can deploy to.

Thanks Simon! I decided to test out the app after removing the tab that required loading in the full Seurat object. After doing so, the app deploys without errors and is now live (https://elyamanlab.shinyapps.io/epilepsy_pbmc/), but it's extremely slow, and I haven't been able to get it to actually display anything before the browser gives me a message that I was disconnected from the server and have to reload. I don't know if that's just an issue of needing to include more reactive expressions so the app isn't needing to load as much at once, or if it would make any difference to deploy the app from the university's HPC cluster instead of my machine, or if it's something else.

Glad to hear you're making progress! The first thing to do is look at the logs to see why it's not loading correctly.

This is unlikely to the problem since it's the total RAM use that causes issues, not how quickly it is used.

Can you make a subset of the data that isn't so large? That would help to isolate whether RAM is still an issue, or whether there is a problem with your code.

I checked the logs and realized I still hadn't deleted the shinyApp() call from the server.R file back when I had the material for app.R in it, so it wasn't finding the ui. I removed that and the app works now. Thank you for all your help!

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.