Setting RStudio Connect Environment Variables


We have a dev and prod RSC (RStudio Connect) that we deploy our apps to. There are some functions that operate differently depending on which environment the app is running on. I would like to add a simple environment variable that is accessible across all projects to each environment.

Example: On RSC dev if I ran Sys.getenv("RSC_ENV") it would return the value "dev". If I ran Sys.getenv("RSC_ENV") within RSC prod it would return the value "prod".

I want to avoid having to add a secure environment variable to every project one by one. Is this possible? I found the following article, Environment variables on RStudio Connect – RStudio Support, I think does what I want, but I do not have access to execute the steps and wanted to see if this is the best route for setting environment variables that can be accessed by applications running on RSC.

Example from article modified; would this get me the result I want in RSC dev? Assuming I followed the article's instructions.

echo arguments: "$@" >&2
echo >&2
export RSC_ENV="dev"
exec "$@"

So in my R code Sys.getenv("RSC_ENV") would return "dev".

I appreciate any insight on global environment variables in RSC or another direction for identifying my environment! :smiley:

By default, the R_CONFIG_ACTIVE variable is set to rsconnect--this makes it easy to know if your code is running on a Connect server. One way to handle this would be to update your configuration to set that value to dev or prod on the respective server. Importantly, you cannot override the value of that environment variable from the content dashboard. The config package is also helpful here for fetching different values based on your deployment target.

In more recent versions of Connect, the CONNECT_SERVER environment variable returns the server address of the content as it's being built, so detecting that may be another way to go.


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.

Small update here: in the 2023.05 release of Connect, you are allowed to modify the R_CONFIG_ACTIVE environment variable at the content level. Posit Connect: News

1 Like