Skip to Content

testthat mask potential bug of not import %>% from magrittr

# install needed R packages
remotes::update_packages(c('testthat'), upgrade = TRUE)

magrittr is a wonderful package, but the biggest drawback is that you can’t write magrittr::%>%.

That can cause great pain in package development, I spend a whole morning to debug.

Basically, I summary the following rules:

  • testthat ignores .Rprofile
  • In theory, every pipe operator used must be imported from magrittr
  • except for %>% (testthat import and export it)
testthat::`%>%`
function (lhs, rhs) 
{
    parent <- parent.frame()
    env <- new.env(parent = parent)
    chain_parts <- split_chain(match.call(), env = env)
    pipes <- chain_parts[["pipes"]]
    rhss <- chain_parts[["rhss"]]
    lhs <- chain_parts[["lhs"]]
    env[["_function_list"]] <- lapply(1:length(rhss), function(i) wrap_function(rhss[[i]], 
        pipes[[i]], parent))
    env[["_fseq"]] <- `class<-`(eval(quote(function(value) freduce(value, 
        `_function_list`)), env, env), c("fseq", "function"))
    env[["freduce"]] <- freduce
    if (is_placeholder(lhs)) {
        env[["_fseq"]]
    }
    else {
        env[["_lhs"]] <- eval(lhs, parent, parent)
        result <- withVisible(eval(quote(`_fseq`(`_lhs`)), env, 
            env))
        if (is_compound_pipe(pipes[[1L]])) {
            eval(call("<-", lhs, result[["value"]]), parent, 
                parent)
        }
        else {
            if (result[["visible"]]) 
                result[["value"]]
            else invisible(result[["value"]])
        }
    }
}
<bytecode: 0x55ee922a9010>
<environment: 0x55ee922a9e48>