Why doesn't assertive::is positive work in this example?

I am trying to use the package 'assertive'. I managed to make it work in this simple case:

 foo <- function(x) {
  assertive::is_positive(x)
}
foo(-1)
# There was 1 failure:
#  Position Value   Cause
# 1        1    -1 too low

However, in a more complicated function it has no effect. I show only the beginning of the function, as it has many lines.

model.optimize <- function(model_info,
                           data,
                           n.best=1L,
                           dt.intvl=NULL,
                           stat="Sharpe_Ratio",
                           stats.list=stats.list,
                           min.length=-1L,
                           n.cores=1L) {

  assertive::is_positive(-1L)

# many lines of code

Here is the result or running 'model.optimize':

> model.optimize(model_info,
+                df_all_data,
+                n.best=n.best,
+                dt.intvl=dt.intvl,
+                stat=statistic,
+                stats.list=stats.list,
+                min.length=-1L,
+                n.cores=n.model.cores) -> mo

Optimizing Model
Number of Iterations: 1 
Number of Cores: 1 
Optimization executed in: 2.23 secs

The command 'is_positive' had no effect!

If I wrap 'print' around the assertion, like this:

print(is_positive(-1))

then I get the correct warning.

Any tips on why does 'is_positive' work in one case and not in the other?

Hi @Soldalma,
This happens because a function only returns a single object and this is always the last object produced in the function. An object will not be returned if it has been assigned. If you want to return more than one object, you must place these in a list, and specifically return() the list. Also, as you have found, a nested function wrapped by print() will deliver its output.

In this example see the different outputs from some simple functions, which have your foo() function nested inside. With this knowledge you should be able to fix your larger function.

library(assertive)
foo <- function(x) {
  assertive::is_positive(x)
}


# Only the date object is returned
fun3 <- function(num=-5){
  foo(num)
  Sys.Date()
}

fun3()
#> [1] "2021-08-06"

# Clunky way to see both objects
fun4 <- function(num=-5){
  print(foo(num))
  Sys.Date()
}

fun4()
#> There was 1 failure:
#>   Position Value   Cause
#> 1        1    -5 too low
#> [1] "2021-08-06"

# Nothing returned
fun5 <- function(num=-5){
  foo(num)
  xx <- Sys.Date()
}

fun5()

# Both objects returned in list form
fun6 <- function(num=-5){
  test <- foo(num)
  xx <- Sys.Date()
  output <- list(test=test, xx=xx)
  return(output)
}

fun6()
#> $test
#> There was 1 failure:
#>   Position Value   Cause
#> 1        1    -5 too low
#> 
#> $xx
#> [1] "2021-08-06"

Created on 2021-08-06 by the reprex package (v2.0.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.