This is a great question. From the Plumber docs:
"When you plumb() a file, Plumber calls source() on that file which will evaluate any top-level code that you have defined."
This means that any globally defined functions / objects are made available to subsequently defined Plumber endpoints. If you have these object definitions in a separate R script, you can source that script at the beginning of the Plumber file in order to make the objects available to endpoints.
# utils.R
add <- function(x, y) x + y
# plumber.R
library(plumber)
source("utils.R")
#* Add two values
#* @param x First value
#* @param y Second value
#* @get /sum
function(x, y) add(as.numeric(x), as.numeric(y))
In this example, the definition for the add() function is only run once, when the file is plumbed. Calls to the /sum endpoint use this function, but don't require it to be re-defined each time.
RStudio Connect will try to identify any external dependencies your API may have. For example, consider a directory with previously defined plumber.R file and utils.R files. When publishing to RStudio Connect, the option of including the utils.R file along with the plumber.R file is presented:
Once the API is published to RStudio Connect, the functions and objects declared in utils.R will only be run once for each R process associated with the API. The Runtime settings of the API can be adjusted in RStudio Connect to control how many R processes are dedicated to the API.
More detail about these settings can be found in the RStudio Connect Admin Guide.