Can someone explain what a placeholder is and how to use it in R programming?
CodePudding user response:
A pipe operator was introduced in R 4.1.
The placeholder _
is a new way of referring to the data in the LHS of the pipe operator in the RHS of a pipe. It was introduced in R 4.2.0 and
- The placeholder refers to the data set in the LHS of a pipe instruction;
- can only be used in the RHS of a pipe instruction;
- can only be used with a named argument;
- can only be used once;
- it's not needed if it is the first argument of the RHS function, only as a placeholder for a 2nd, 3rd, etc argument.
See help('|>')
and this r-devel thread.
Example:
mtcars |> row.names() |> grep("Merc", x = _)
#> [1] 8 9 10 11 12 13 14
Created on 2022-04-25 by the reprex package (v2.0.1)