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