Usethis: use_testhat() appears to fail if package is not in root directory of the repo

Created on 24 Jun 2019  路  5Comments  路  Source: r-lib/usethis

I have a project that includes an R package in the package directory. If I try to use usethis::use_testthat() I get the following error message:

Error: `use_testthat()` is designed to work with packages.
Project 'Foo' is not an R package.

The R project file includes

BuildType: Package
PackageUseDevtools: Yes
PackagePath: package
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette

Is there a way to tell use_testthat() about such a package?

Most helpful comment

I just had the same issue with use_data ... workaround is to

  • create a new project in the package directory (I had my project one level above the package directory)
  • use the function outside an R project

That being said - I don't understand why you are trying to enforce project = package. In RStudio, it is possible to set the project path as a subfolder of the project, which clearly seems to indicate that many people do this!

All 5 comments

No, I'm afraid usethis has a pretty hard assumption / requirement that its notion of the active project == the R package. usethis is aware of RStudio's .Rproj file in superficial ways, but it is not used to drive usethis behaviour.

You could try explicitly setting the usethis project to the sub-directory containing the package via proj_set(), but I suspect sooner or later you'll encounter other infelicities. That type of usage is not tested at all.

Thanks for the quick reply. I feared that this might be the case.

It would be great to see this change at some point in the future. I often construct projects that include an R package as one piece of a larger project of related materials. I guess a work-around is to promote the package to the top level and move everything else into one or more directories that are ignored when the package is built.

Yeah I too went through a phase of organizing projects this way (package within project) or, at least, being tempted to. Ultimately it felt too much like swimming against the current.

We don't have any immediate plans to accommodate this other design, so for now you'll need to experiment with the workaround I suggested or the alternative directory structure you describe. I frequently handle design notes, external docs, and talk materials that way, inside a package. Or keep the package in one folder/git repo/RStudio project and all the other stuff in a sibling folder/git repo/RStudio project.

I just had the same issue with use_data ... workaround is to

  • create a new project in the package directory (I had my project one level above the package directory)
  • use the function outside an R project

That being said - I don't understand why you are trying to enforce project = package. In RStudio, it is possible to set the project path as a subfolder of the project, which clearly seems to indicate that many people do this!

I would like to second @rpruim here. I usually also use packages in a subdirectory of a project. This is also supported by the Build structure in R-Studio and I do not see any reason why this would be poor practice? I would say in most project that are not pure packages, this is how I work. Not being able to use 麓usethis麓 in these projects without using a workaround is limiting.

The workarounds suggested by @florianhartig will solve the problem, but I think this should be supported by usethis directly - either by looking at the build path in the .Rproj by just giving a path as an argument?

Was this page helpful?
0 / 5 - 0 ratings