Editor-aware shortcut for gluing file paths from the Rproj directory?

I use this str_glue('{here::here()}/...') combination a lot to make robust file paths within RStudio projects, trying to follow the infinite wisdom of @jennybryan on project organisation. But there are two things that seem like there could be room for improvement:

  1. It's seems like a lot of keystrokes. I could save a few with a library(here) at the start of my script. But still, it seems like a str_glue_here could be a keystroke-saver because I could tab-complete the function name. Or maybe @jimhester's fs could have something like fs::path_project akin to the existing path_home? The downside is that fs is currently not loaded with library(tidyverse)

  2. RStudio isn't aware that that I'm typing a file path in this combination, so I don't get the amazing tab-complete directory/file listing that I get with list.files, for example. I guess it would be hard to make that work in the quoted string in a str_ function, but it does work for the fs::path_home functions. So that might be a good reason to have a fs::path_project to build up a robust path starting from the project's directory.

Or is there already an existing function that does this even better that I haven't heard of?

I don't quite follow why you embed here::here() inside str_glue(). To work with your example, why not here::here(...)? This workflow also lets you take advantage of RStudio file path completion. I feel like I probably misunderstand something.

2 Likes

Ah, I see now, thanks. No, you've followed perfectly, it's me who didn't understand the documentation for here correctly.

I just assumed here only had the side-effect of returning the project directory, and didn't take arguments. I glossed over the sense that ... can take paths below the project root. I see now that the example in the docs shows this clearly. The function I want is here, I just didn't know that I could use it like that, thanks for setting me straight! That does simplify things.

2 Likes

Hi Ben,

If your question's been resolved (I'm not totally sure whether or not it has been yet, based on your response), would you mind choosing a solution? If not, then, of course, leave it as is! Just trying to promote this as a best practice here on the community site, since it makes it easier to figure out who/which questions still need help.

Thanks!

1 Like