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

vignette for simple use cases? #26

Closed
BarkleyBG opened this issue Sep 25, 2017 · 6 comments
Closed

vignette for simple use cases? #26

BarkleyBG opened this issue Sep 25, 2017 · 6 comments

Comments

@BarkleyBG
Copy link
Contributor

@BarkleyBG BarkleyBG commented Sep 25, 2017

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:
- useful and important
- easy to implement
so I think it would be a great way to show off the utility/power/ease of rprojroot

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
Copy link
Member

@krlmlr krlmlr commented Sep 25, 2017

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
Copy link
Contributor Author

@BarkleyBG BarkleyBG commented Sep 25, 2017

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
Copy link
Member

@krlmlr krlmlr commented Sep 25, 2017

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

@jennybc
Copy link
Member

@jennybc jennybc commented Sep 25, 2017

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

BarkleyBG referenced this issue in BarkleyBG/rprojrootTester Oct 31, 2017
Fixes #1

Thanks to Jennny Bryan's suggestion to use a helper.R script, as
discussed in https://github.com/krlmlr/rprojroot/issues/26
@krlmlr
Copy link
Member

@krlmlr krlmlr commented Dec 17, 2017

@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", ...)
## }

@krlmlr krlmlr closed this in 0cae4b8 Dec 17, 2017
krlmlr added a commit that referenced this issue Dec 17, 2017
- Add more examples to vignette (#26, @BarkleyBG).
krlmlr added a commit that referenced this issue Dec 17, 2017
- New `thisfile()` moved from kimisc (#8).
- Add more examples to vignette (#26, @BarkleyBG).
krlmlr added a commit that referenced this issue Dec 18, 2017
- Adapt to testthat 2.0.0.
- New `thisfile()`, moved from kimisc (#8).
- Add more examples to vignette (#26, @BarkleyBG).
- Detect `.git` directories created with `git clone --separate-git-dir=...` (#24, @karldw).
@github-actions
Copy link

@github-actions github-actions bot commented Dec 8, 2020

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.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants