rmagrittrdo.call

magrittr pipe of dot to do.call function


When the . is transferred indirectly to the function like do.call, then the magrittr does not see the ..

Example which will gives an error, as dot is not visible.

airquality %>% do.call("mean", list(x = .$Ozone, na.rm = TRUE))

Solution

  • Unless there is a dot as an argument to the main function, here do.call, the pipe will insert the left hand side into argument 1. Using dot within an expression does not count.

    Brace brackets can be used to disable automatic insertion, with can be used such that with is the main function instead of do.call, use magrittr's %$% pipe or use a function which explicitly does what you want.

    (Note that mean will give a result of NA if there are any NA's in its argument; to suppress the NA's add na.rm=TRUE as an argument to mean -- not shown.)

    airquality %>% { do.call("mean", list(x = .$Ozone)) }
    
    airquality %>% with(do.call("mean", list(x = Ozone)))
    
    library(magrittr)
    airquality %$% do.call("mean", list(x = Ozone))
    
    # needs R 4.1+ . With earlier versions use function(.) in place of \(.)
    airquality %>% (\(.) do.call("mean", list(x = .$Ozone)))
    

    |>

    With the |> pipe introduced into base R 4.1 these work but not the analogs to others above:

    airquality |> with(do.call("mean", list(x = Ozone)))
    
    airquality |> (\(.) do.call("mean", list(x = .$Ozone)))()