inbo / camtraptor

Camtraptor is an R package to read, explore and visualize Camera Trap Data Packages (Camtrap DP)
https://inbo.github.io/camtraptor/
MIT License
10 stars 2 forks source link

Have `check_package()` return error or TRUE (on valid) #245

Closed peterdesmet closed 1 year ago

peterdesmet commented 1 year ago

Suggested in camtraptor July 2023 coding sprint

check_package() currently returns the object itself, so it can be used in pipes, but this reads oddly. Rather, check_package() should return TRUE, exactly how frictionless:::check_package() works.

This change will effect the use in other functions. There it should be used as:

check_package(package)
# next steps

Not:

check_package(package) %>% next steps
PietrH commented 1 year ago

Do we also want to move check_package() outside of zzz.R?

peterdesmet commented 1 year ago

Yes

damianooldoni commented 1 year ago

Thanks @PietrH to work on this! 👍 I will handle this PR after merging #223.

damianooldoni commented 1 year ago

Actually frictionless:::check_package() doesn't return TRUE, but nothing. And actually it 's what I expected by our function as well. I am working on this in #247

PietrH commented 1 year ago

I think they should probably return something invisibly, similar to testthat, there is a vignette on custom expectations: https://testthat.r-lib.org/articles/custom-expectation.html?q=expect#expectation-basics

I chose TRUE because this is the behaviour of assertthat, assertions either return an error, or TRUE if all is good. This way they can be used in internal logic.

On this note, we could use the on_failure() helper from assertthat, this is an example from their documentation;

is_odd <- function(x) {
  assert_that(is.numeric(x), length(x) == 1)
  x %% 2 == 1
}
assert_that(is_odd(2))
# Error: is_odd(x = 2) is not TRUE

on_failure(is_odd) <- function(call, env) {
  paste0(deparse(call$x), " is even")
}
assert_that(is_odd(2))
# Error: 2 is even

testthat seems to mostly return the expectation, so expect_length() will return the length when passing, and a message/error if not.


So, do we want check_package() to behave more like assertthat or more like testthat?

damianooldoni commented 1 year ago

Hi @PietrH. Behavior of assertthat seems to me ok. I was actually wrong in my previous comment. Or, let's say, not completely correct.

The difference between our function and the frictionless::check_package() was in the last if statement containing an assertion.

Without the final return(TRUE) you wrote, the output of the function was different depending on the evaluation of the if condition. If the condition was not satisfied the function would return nothing, while if the condition was satisfied than the output was TRUE or FALSE depending on the output of the assertion in the if.

So, I added back the return(TRUE) you wrote. Well done!