4.6 RStudio, usethis and working directories

usethis functions don’t take a path (i.e. usethis::use_r("new_fx"))

usethis assumes that the following coincide:

  • The current RStudio Project.
  • The active usethis project.
  • Current working directory for the R process.

It’s therefore much easier to not mess with your working directory for your R console. Leave it as the project directory so that when you’re calling functions from usethis, it puts things in the right place.

You can double check that these three directories are coinciding by running:

## •   working_directory: '/home/runner/work/bookclub-rpkgs/bookclub-rpkgs'
## • active_usethis_proj: <unset>
## • active_rstudio_proj: <unset>
## • There is currently no active usethis project.
##   usethis attempts to activate a project upon first need.
##   Call `proj_get()` to initiate project discovery.
##   Call `proj_set("path/to/project")` or `proj_activate("path/to/project")` to provide
##   an explicit path.

Similarly, when you run helper functions from devtools to develop your package (document(), check(), test()), these are also expecting your working directory to be the project directory. The also expect that your project follows the conventions of project structure (test scripts living in tests/, R code living in R/, vignettes living in vignettes/ compiled code living in src/, images living in man/figures/ etc).