assertr icon indicating copy to clipboard operation
assertr copied to clipboard

Chaining adds "assertr_in_chain_success_fun_override" attribute to dataframe?

Open jack-gregory opened this issue 2 years ago • 1 comments

Hello,

Thanks so much for making this package available!

I have been using the chain_start and chain_end functions to link various assertions together. However, I noticed that this appears to add an "assertr_in_chain_success_fun_override" attribute to the dataframe, if it is redefined through the chaining process. This does not occur when chaining is omitted. See the minimum working example below.

library(assertr)

df <- data.frame(X1 = letters, X2 = seq(1,26))

## With chaining
df_with <- df |>
  chain_start() |>
  assert(is_uniq, X1, X2) |>
  assert(is.character, X1) |>
  assert(is.numeric, X2) |>
  chain_end()

attributes(df_with)
#$names
#[1] "X1" "X2"

#$class
#[1] "data.frame"

#$row.names
#[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

#$assertr_in_chain_success_fun_override
#function (data, ...) 
#{
#   return(data)
#}
#<bytecode: 0x0000023be0ad13c8>
#<environment: namespace:assertr>

## Without chaining
df_without <- df |>
  assert(is_uniq, X1, X2) |>
  assert(is.character, X1) |>
  assert(is.numeric, X2)

attributes(df_without)
#$names
#[1] "X1" "X2"

#$class
#[1] "data.frame"

#$row.names
#[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Is this the anticipated behaviour? Perhaps I'm not using the package in the intended manner?

My intended use case is to compare if the "asserted" dataframe is identical to another where the additional attribute causes the comparison to fail. After a set of chained assertions, would it be possible to return a "clean" dataframe with the "assertr_in_chain_success_fun_override" attribute removed?

jack-gregory avatar Jun 03 '22 12:06 jack-gregory

Ohh, I see. Hmm, maybe I can remove the attribute during the chain_end function. Great idea In the meantime, you can run attributes(df_with)$assertr_in_chain_success_fun_override <- NULL after the pipeline. I'll work on a fix to this

tonyfischetti avatar Jul 10 '22 15:07 tonyfischetti