r-lib / rprojroot

Finding files in project subdirectories
https://rprojroot.r-lib.org/
Other
149 stars 23 forks source link

vignette for simple use cases? #26

Closed BarkleyBG closed 6 years ago

BarkleyBG commented 7 years ago

I thought the vignette was helpful. Perhaps some very short examples could also be helpful?

Here's a very short, concrete example that for the user would be:

I was able to get this working for my use case in one-line of code. I have a package and I have some baseline data for unit tests that I was storing in an .Rds file:

output <- myFun(stuff)

## a one-liner with rprojroot
baseline_test_data_file <- rprojroot::find_testthat_root_file("baseline_test_data.Rds")
## now load that data
baseline_test_data <- readRDS(file = baseline_test_data_file)
## and use it
testthat::test_that(
  "myFun returns correct output",
  testthat::expect_equal(
    output,
    baseline_test_data
  )
)

Previously, I had some if-then syntax for whether I was running interactively or via devtools::test(). Basically, I'd try to determine what the working directory was, and then specify the relative path from that; i think rprojroot does ~ exactly this, but with one line.

nc <- nchar(getwd())
if (substr(getwd, nc-7,nc)=="testthat") { ## called from devtools::test()
   relative_path_name <- "baseline_test_data.Rds"
} else 
if (substr(getwd, nc-4,nc)=="mypkg") { ## running interactively 
   relative_path_name <- "tests/testthat/baseline_test_data.Rds"
}
baseline_test_data <- readRDS(file = baseline_test_data_file)

So I replaced the above with the below:

baseline_test_data_file <- rprojroot::find_testthat_root_file("baseline_test_data.Rds")
baseline_test_data <- readRDS(file = baseline_test_data_file)

I can write up a short vignette & start a PR if you'd like. Thanks for making my life better!

krlmlr commented 7 years ago

There is a section "testthat files" in the existing vignette, is anything missing from there? Maybe it should be more visible, or the vignette needs a better structure? Either way, happy to review your PR!

BarkleyBG commented 7 years ago

I don't think anything is missing at all!

I opened the issue because the power and flexibility of rprojroot and made it seem like the pkg was "too sophisticated" for the issue I was trying to solve, in a way. But what "sold" me on using this pkg was seeing that you had condensed dir(is_testthat$find_file("hierarchy", path = is_r_package$find_file())) into a single function. So I think that providing a simple example like this would be great (in the name of evangelizing).

I can add my thoughts to the vignette. Should I PR to master, develop, or some other branch?

krlmlr commented 7 years ago

Thanks, master is fine (I just removed the develop branch).

jennybc commented 7 years ago

If you're tinkering in the vignette ... if there are lots of reference files, it can be nice to create a folder for them and a helper function in helper.R to fetch them. readxl maintains a zoo of weird workbooks and here's how I access them in tests:

https://github.com/tidyverse/readxl/blob/0d9ad4f570f6580ff716e0e9ba5048447048e9f0/tests/testthat/helper.R#L1-L3

krlmlr commented 6 years ago

@jennybc: Would a subdir argument to make_fix_file() help, or add confusion?

test_sheet <- rprojroot::is_testthat$make_fix_file(subdir = "sheets")
test_sheet
## function (...) {
##   file.path("/.../rprojroot/tests/testthat", "sheets", ...)
## }
github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.