0

How do I pipe in a result to stopifnot() and testit::assert()?

library(magrittr)

0 %>% 
  testit::assert("gotta be small", . < 10)  
# Error: . is not TRUE

0 %>% 
  testit::assert(. < 10)  
# Error: . is not TRUE

0 %>% 
  stopifnot(. < 10)
# Error in function_list[[k]](value) : . is not TRUE

Works:

testit::assert(0 < 10)  
stopifnot(0 < 10)  

I'm assuming they're not acting like a conventional function like add() because they're evaluating an expression. I'm guessing I should package the argument differently, so it's interpreted correctly by their match.call() calls, but I'm not sure how to.

wibeasley
  • 5,000
  • 3
  • 34
  • 62
  • 3
    Use braces `0 %>% {testit::assert("gotta be small", . < 10)}`. That will get the `.` value to the right place. You need to do this when you want to pass the `.` as part of an expression rather than a parameter on it's own. – MrFlick Oct 16 '18 at 18:39
  • @MrFlick is right. This is [related post](https://stackoverflow.com/questions/42623497/when-should-we-use-curly-brackets-when-piping-with-dplyr?noredirect=1&lq=1). – pogibas Oct 16 '18 at 18:41
  • That helps, and I voted to close it as a duplicate. Even knowing the answer, I'm still not sure how I would have searched for the answer differently; there aren't any terms or keywords I can grab on to. The closest I see are "secondary purpose" or "expression". – wibeasley Oct 16 '18 at 21:10

0 Answers0