r-lib / here

A simpler way to find your files
https://here.r-lib.org/
Other
411 stars 45 forks source link

feature request: localized here() #107

Open torbjorn opened 4 weeks ago

torbjorn commented 4 weeks ago

In our work we find ourselves (due to git submodules) needing to temporarily changing here() root when sourcing files.

Would a local_here( "/now/here", r_expression ) make sense? (and have r_expression being evaluated with here's root temporarily changed to said dir)

Wasn't sure to take this here (hah!) or to withr, but it sounds specific enough that it ought to go here I believe.

krlmlr commented 4 weeks ago

Thanks. The here package currently doesn't allow changing its perception of what it thinks the root of the project should be.

Am I reading this correctly that you have a main Git repo that contains several other projects that need to run both self-contained and as a submodule in the bigger repo? What would your use case of a local_here() or with_here() function look like? Would the i_am() function be of any use?

torbjorn commented 3 weeks ago

Yes, thats correct.

We have a project with a pipeline written in one repository. Code in that project uses here() on all file paths local to that project, so when sourcing R scripts and function definitions and trying to run them from another here()-root, paths dont work.

Locally changing here root would make it all magically just work, with no extra work necessary to make that pipeline support being run externally and internally. It's not ideal, a rewrite would be better, but for it to seemlessly work with no extra effort, would be quite nice.

Until, or if, this gets implemented, we have written a with_here() and local_here() that solves this for us for the time being. They both use i_am() to temporarily change the here-root, which seems to work. They are not complicated functions to write.

They work roughly like this:

  1. store here's current root
  2. create a temporary .here file here for future reference
  3. chdir to the new but temporary here root
  4. create a temporary .here file here
  5. Run i_am("./.here"), this changes the root
  6. optionally chdir() back to the old working dir, or stay here.
  7. use defer() or on.exit(), to repeat with the old .here file in the old root to get back
krlmlr commented 1 week ago

Thanks, interesting. Would you like to share these functions, perhaps in a package that imports here or rprojroot? I don't have time for a deeper dive now or in the coming weeks.

torbjorn commented 4 days ago

See the two functions here:

https://github.com/torbjorn/wither

(I tried publishing it to CRAN, but couldn't get it passed the win-buider r-devel check, seems I wasnt allowed to create files in temporary directories?)