Transcript#
This transcript was generated automatically and may contain errors.
Have you ever tried to use the native R pipe and then encounter this error? The native R pipe takes the object on the left and places it as the first argument of the argument on the right. It's very similar to the pipe from Magritter, but the native R pipe has a few differences.
The dot syntax is a feature of Magritter and not of base R. So this works because it's the same as this, but this doesn't because it doesn't know what to do with that dot. So you have to create what is called an anonymous function. Let's break it down.
Breaking down the anonymous function
This is what's being passed into the pipe, native pipe operator. It takes the object on the left and places it as the first argument of the function on the right. The anonymous function definition, the formal argument or parameter, which is the variable name in this case, a dot, the function body. This is the actual work being done using the argument we defined and the immediate invocation. And these trailing parentheses tell R, execute this function I just defined right now. And it works.
And these trailing parentheses tell R, execute this function I just defined right now. And it works.
There's also an anonymous function shortcut, so you can run this instead. And if you don't want a dot, you could use other name like X, or you can be explicit and say data. Now there's an even easier way of writing this using the pipe placeholder from R 4.2. Are you interested in learning about the placeholder? Let us know in the comments.