phuse-org / valtools

Validation framework for R packages used in clinical research and drug development.
https://phuse-org.github.io/valtools/
Other
51 stars 10 forks source link

Working directory friction #214

Closed wlandau closed 7 months ago

wlandau commented 2 years ago

valtools seems to require the working directory to be the root of the validation file space, not the root of the package. If I try this command, but with name equal to file.path("r_pharma_validation", "req1.md") and my working directory one level up, I get an error and warnings. This took a few minutes to debug during the workshop today.

packageVersion("valtools")
#> [1] ‘0.4.0’
getwd()
#> [1] "/cloud/project/Materials/Materials-04-Requirements_Report"
vt_use_req(
  file.path("r_pharma_validation", "req1.md"),
  username = "useR",
  title = "Requirement 1",
  open = TRUE
)
#> Error: Failed to create validationrequirementsr_pharma_validation/req1.md 
#> Run `rlang::last_error()` to see where the error occurred.
#> In addition: Warning messages:
#> 1: In if (file.exists(item_file_path)) { :
#>   the condition has length > 1 and only the first element will be used
#> 2: In if (!dir.exists(file.path(validation_directory, type))) { :
#>   the condition has length > 1 and only the first element will be used
thebioengineer commented 2 years ago

The root can either be the package root directory or within the packet directory.

This is a weird error we thought we had solved (ISSUE #204, PR #210), but apparently not. I will investigate.

thebioengineer commented 2 years ago

I think I sorted out what is going on - it is supposed to throw an error if the function is called outside of a package/packet.

However, because the workshop is a project overall, it finds and sets the root to the workshops root. When it searches for the config, it finds all the various config files within the workshop, and lists all of them. Which leads to your error, @wlandau, because the path gets sent to create_item() (a helper function under valtools).

reprex code:


library(valtools)
library(testthat)

test_that("Informative error when inside an Rproj, but packet or package is nested in a subfolder", {

  withr::with_tempdir({

    quiet <- capture.output({
      usethis::create_project("test_project",open = FALSE)
      vt_create_packet("test_project/example_packet", 
                         target = "example.package",
                         open = FALSE)
      vt_create_packet("test_project/example_packet2", 
                       target = "example.package",
                       open = FALSE)
    })

    withr::with_dir(new = "test_project", {
     expect_error(
      vt_find_config(),
      paste0(
      "Could not find root directory. ",
      "Is your working directory inside a package, validation packet, or project?\n"
      ),
      fixed = TRUE)

    })

  })

})
thebioengineer commented 2 years ago

@wlandau, would it have been useful for an error to point to the fact that you were in a situation of nested projects, and you needed to change directories? I have a PR opened in #217 that makes it so it would give an error like below:

Error: Nested projects with validation infrastructures exist. Set the working directory to one of:
    - `setwd("r_pharma_validation")`
    - `setwd("example_packet2")`
wlandau commented 2 years ago

Yes, that definitely would have helped. I was confused about what working directory I needed, especially since the workshop example was not part of an R package.