Thanks @maelle. I will give that a go when I log back in to the work computer.
It is currently used in the tests, but also in a file where I am playing with the output by using devtools::load_all(). I think my current solution involves putting the config.yml file in both the top folder and in the inst folder, then hiding one or both in the .gitignore and .Rbuildignore files. This seems to work, but isn't ideal.
I'm not sure how this all works. I have tried this, but still have the same issues.
test_that("test checks", {
# connect this way in the tests
creds <- config::get("postgres", file = testthat::test_path("config.yml"))
CON <- DBI::dbConnect(RPostgres::Postgres(),
host = creds$host,
dbname = creds$dbname,
user = creds$user,
password = creds$password,
port = creds$port)
query <- # the function query that connects to a db
expect_s3_class(query, "data.frame")
})
It would be good if there was an example for ?test_path. I found your blog post, which sort of explains some of it though, but still have the issue described earlier.
If you need the file to be available through load_all(), you could define a function like below in the file tests/testthat/helper-config.R (helper files are described in the same blog post )
Thanks Maelle. I am still having issues though. It works fine when debugging with load_all() and works with test() but not with check().
What I have got:
example_config function in tests/testthat/helper-config.R - I have used yours
config.yml in tests/testthat/config.yml
no inst folder
no top level config.yml
none of the files in the tests folder are being ignored by .Rbuildignore
This is an example error:
ββ Failed tests ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββ Error (test_age_year_agg.R:5:3): age_year_agg checks ββββββββββββββββββββββββ
Error: Config file config.yml not found in current working directory or parent directories
Backtrace:
β
test_that("test checks", {
# # helper function for connection from 'tests/testthat/helper-config.R'
creds <- config::get("postgres", file = example_config())
CON <- DBI::dbConnect(RPostgres::Postgres(),
host = creds$host,
dbname = creds$dbname,
user = creds$user,
password = creds$password,
port = creds$port)
query <- # the function query that connects to a db
expect_s3_class(query, "data.frame")
})
Do you know what might be wrong and what the solution to this might be?