| pipe_return_linter {lintr} | R Documentation |
Block usage of return() in magrittr pipelines
Description
return() inside a magrittr pipeline does not actually execute return()
like you'd expect:
Usage
pipe_return_linter()
Details
bad_usage <- function(x) {
x %>%
return()
FALSE
}
bad_usage(TRUE) will return FALSE! It will technically work "as expected"
if this is the final statement in the function body, but such usage is misleading.
Instead, assign the pipe outcome to a variable and return that.
Tags
best_practices, common_mistakes
See Also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "function(x) x %>% return()",
linters = pipe_return_linter()
)
# okay
code <- "function(x) {\n y <- sum(x)\n return(y)\n}"
writeLines(code)
lint(
text = code,
linters = pipe_return_linter()
)
[Package lintr version 3.3.0-1 Index]