The following chunks of code should be doing exactly the same thing, but the first chunk unexpectedly prints "We reach here?" also when x = 2, which shouldn't be the case (I hope it's obviosu what I mean).
Chunk 1
library(tidyverse)
input <- c(1,2,3)
output <- map(input, function(x) {
print(x)
if (x == 2) {
return(x)
}
print('We reach here?')
return(x)
})
Output
This output is as expected, as the string doesn't print when x = 2.
[1] 1
[1] "We reach here?"
[1] 2
[1] 3
[1] "We reach here?"
Chunk 2
input <- c(1,2,3)
output <- map(input, function(x) {
print(x)
if (x == 2) {
x %>% return()
}
print('We reach here?')
x %>% return()
})
Output
[1] 1
[1] "We reach here?"
[1] 2
[1] "We reach here?"
[1] 3
[1] "We reach here?"
What am I missing here? Is this a bug?