dplyr icon indicating copy to clipboard operation
dplyr copied to clipboard

condition handling ignores locally set options

Open moodymudskipper opened this issue 2 years ago • 1 comments

Maybe because of a delayed evaluation issue ?

bugged <- function() {
  withr::local_options(warn = 2)
  rlang::warn("oops") # or warning("oops")
  24
}

# as expected

bugged()
#> Error: (converted from warning) oops
subset(cars, speed == bugged())
#> Error: (converted from warning) oops

# unexpected

dplyr::filter(cars, speed == bugged())
#> Warning: There was 1 warning in `dplyr::filter()`.
#> ℹ In argument: `speed == bugged()`.
#> Caused by warning:
#> ! oops
#>   speed dist
#> 1    24   70
#> 2    24   92
#> 3    24   93
#> 4    24  120

Created on 2023-10-25 with reprex v2.0.2

moodymudskipper avatar Oct 25 '23 12:10 moodymudskipper

I have been caught by a similar issue the other way around where warnings appear that should not be there:

require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
no_warn <- function() {
    withr::local_options(warn = -1)
    rlang::warn("oops") # some operation that produces a warning that should be ignored
    24
}
no_warn()
#> [1] 24
data.frame(a=1:2) %>% mutate(b=no_warn())
#> Warning: There was 1 warning in `mutate()`.
#> ℹ In argument: `b = no_warn()`.
#> Caused by warning:
#> ! oops
#>   a  b
#> 1 1 24
#> 2 2 24

bart1 avatar Mar 27 '24 17:03 bart1