I have created a package containing a run_app
function (which calls a Shiny app with runApp
) and uploaded it to a private GitHub repository.
The package folder is like this:
project_name/
project_name.Rproj
DESCRIPTION
NAMESPACE
.Renviron (hidden)
(other git hidden files)
R/
app.R
Within the app, I use methods such as get_bucket
, hence I need AWS credentials to be read by the app. Locally, it works beautifully, since Rstudio is able to retrieve variables from .Renviron without any problems.
When I deploy the app, though, Sys.getenv("AWS_ACCESS_KEY_ID")
for example returns a string with 0 length, rather than the variable I wrote in .Renviron.
The app is deployed normally, but the AWS methods that need environment variables won't work, since the script is failing to read the .Renviron file.
The app is deployed like this:
- I have another folder containing another
app.R
file such as
library(repository_name)
repository_name::run_app()
- Then I execute
tmp.enc <- options()$encoding
options(encoding = "UTF-8", rsconnect.packrat = TRUE)
rsconnect::deployApp(appDir = path/to/the/folder/containing/the/.R/file,
appName = "app_adress",
upload = TRUE, logLevel = "verbose", lint = TRUE, forceUpdate = TRUE)
I've tried many things, such as:
- Making sure the last line in .Renviron is empty
- Deploying from the project's working directory
- Trying to run
readRenviron(".Renviron")
at the start of the app's server function - Trying to run
readRenviron("~/.Renviron")
at the start of the app's server function - Trying to run
readRenviron(normalizePath('.Renviron'))
at the start of the app's server function - Always making sure the .Renviron existed in the working directory I was deploying from
I really don't want to store the .Renviron in the repository, since it's not safe. I believe there must be a way for the shinyapps.io to understand my environment when deploying.
It's also not optimal to execute Sys.setenv
with my credentials everytime I needed to deploy an app. Ideally, the deploy should understand my .Renviron, but I'm probably doing something wrong that prevents it.
This unfortunately didn't help: How to pass environment variables to shinyapps
As margusl pointed out, the .Renviron needs to be in the folder path/to/the/folder/containing/the/.R/file (the one with the reproduced app), not in the folder where the project is (working directory).