Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config file not found in current working directory or parent directories #552

Closed
dylanrussellmd opened this issue Oct 30, 2020 · 8 comments

Comments

@dylanrussellmd
Copy link

dylanrussellmd commented Oct 30, 2020

I am attempting to use the pool package in my Shiny golem application.

I have slightly updated the default golem-config.yml:

default:
  golem_name: mygolem
  golem_version: 0.0.0.9000
  app_prod: no
  default_programs: !expr c(1:10000)
  db_host: mydata.us-west-2.rds.amazonaws.com
  db_name: efs
dev:
  golem_wd: !expr here::here()
  default_programs: !expr c(1:10000)
  db_host: mydata-new.us-west-2.rds.amazonaws.com
  db_name: efs
production:
  default_programs: !expr c()
  db_host: mydata-new.us-west-2.rds.amazonaws.com
  db_name: efs
  app_prod: yes

In order to use pool, I have a R/globals.R file that contains a single call:

pool <- pool::dbPool(
  RMySQL::MySQL(),
  dbname = get_golem_config("db_name"),
  host = get_golem_config("db_host"),
  username = Sys.getenv("DB_USERNAME"),
  password = Sys.getenv("DB_PASSWORD")
)

My .Renviron file is in my project root folder.

I can not figure out why I keep getting this error when I deploy to Shinyapps.io:

Config file not found in current working directory or parent directories

It builds locally fine. And, when I hard replace dbname and host with the actual values from the golem-config.yml file, it builds and deploys just fine.

So there is something about the get_golem_config() function that is causing this error. Here is my function:

get_golem_config <- function(value, config = Sys.getenv("R_CONFIG_ACTIVE", "default"),  use_parent = TRUE) {
  config::get(value = value, 
              config = config, 
              file = app_sys("golem-config.yml"), 
              use_parent = use_parent
  )
}

I think it has something to do with the config::get, because when I edit my globals.R file to be:

pool <- pool::dbPool(
  RMySQL::MySQL(),
  dbname = config::get(value = "db_name", 
                       config = Sys.getenv("R_CONFIG_ACTIVE", "default"), 
                       file = app_sys("golem-config.yml"), 
                       use_parent = TRUE
  ),
  host = config::get(value = "db_host", 
                     config = Sys.getenv("R_CONFIG_ACTIVE", "default"), 
                     file = app_sys("golem-config.yml"), 
                     use_parent = TRUE
  ),
  username = Sys.getenv("DB_USERNAME"),
  password = Sys.getenv("DB_PASSWORD")
)

The error returns.

@ColinFay
Copy link
Member

ColinFay commented Nov 6, 2020

Hey @dylanrussellmd

Thanks for the issue.

I just gave it a shot with a local SQLite:
https://github.com/ColinFay/golem-552

Here is the pool
https://github.com/ColinFay/golem-552/blob/main/R/pool.R#L1

Here is the config:
https://github.com/ColinFay/golem-552/blob/main/inst/golem-config.yml

For the rest, it's default {golem} files.

Could you point me to something you'd be doing differently?

Thanks
Colin

@dylanrussellmd
Copy link
Author

That looks correct - I'm sure it runs fine locally. The issue is when I deploy to Shinyapps.io. Do you have issues doing so?

@ColinFay
Copy link
Member

ColinFay commented Nov 13, 2020

I confirm that I can reproduce the issue on shinyappsio.

Looking into it as we speak.

Colin

@dylanrussellmd
Copy link
Author

Any update on this issue?

@ColinFay
Copy link
Member

ColinFay commented Dec 11, 2020

Ok I think I've got it:

The way you've build your app is: pool <- is not a function so is not lazy loaded.

pkgload::load_all() tries to actually run this (not lazy loading it) and when it loads everything the app_sys() is not made available (yet).

If you have something called pools.R:

get_pool <- list(
  dbname = get_golem_config("db_name"),
  host = get_golem_config("db_host"),
  username = Sys.getenv("DB_USERNAME"),
  password = Sys.getenv("DB_PASSWORD")
)

It will fail. Actually you can think of pkgload::load_all() doing a source() of every R script, then it overrides system.file(), which powers the way app_sys() works.
And when this recursive source() reaches pools.R, it tries to create the object, instead of creating a function, with the content being lazy loaded. So this will actually be true for any R file that contains a x <- get_golem_config("bla"), instead of y <- function() get_golem_config("bla"), then call y() inside the app.

To make this happen, this is the way to go:

get_pool <- function() {
  list(
    dbname = get_golem_config("db_name"),
    host = get_golem_config("db_host"),
    username = Sys.getenv("DB_USERNAME"),
    password = Sys.getenv("DB_PASSWORD")
  )
}
app_server <- function( input, output, session ) {

  pool <- get_pool()

}

Does this helps?

Colin

@dylanrussellmd
Copy link
Author

Well the problem is that pool needs to be available globally to other functions in the application that need to access the server. Declaring pool in the scope of app_server means I'd have to pass it on to any modules that require it as an argument.

@ColinFay
Copy link
Member

That's the spirit of {golem} : nothing should be available globally :) so if you need this value downstream in the module, yes, you should definitely pass it as an argument to the function.

If you want to "hijack" this, you can also use
session$userData$pool <- pool(), then this userData value will be available anywhere the session object is available.

An example of how to handle this is available here with a mongo connection:

https://github.com/ColinFay/hexmake/blob/master/R/fct_mongo.R

@ColinFay
Copy link
Member

As far as I can tell this issue can be closed :)

@dylanrussellmd feel free to comment if you want more information about that.

Thanks for your feedback.

Cheers,
Colin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants