0

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:

  1. I have another folder containing another app.R file such as
library(repository_name)

repository_name::run_app()
  1. 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:

  1. Making sure the last line in .Renviron is empty
  2. Deploying from the project's working directory
  3. Trying to run readRenviron(".Renviron") at the start of the app's server function
  4. Trying to run readRenviron("~/.Renviron") at the start of the app's server function
  5. Trying to run readRenviron(normalizePath('.Renviron')) at the start of the app's server function
  6. 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

2
  • 1
    I would start by downloading that deployed bundle from shinyapps.io (if this is where your are hosting) to check if it actually contains .Renviron. Maybe I'm missing something, but from appDir = path/to/the/folder/containing/the/.R/file I'd assume it's not included. Commented Nov 30, 2023 at 16:48
  • 1
    Thank you! I am the one who was missing something! The .Renviron needs indeed to be in the folder path/to/the/folder/containing/the/.R/file (the one with the reproduced app). I knew I was missing something trivial. I was trying to put the .Renviron in the same folder as the project (the working directory) Commented Nov 30, 2023 at 18:21

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.