0

Of the three blocks below, can anyone explain how to use the . placeholder in the last example? It won't run. I assumed it was an evaluation issue, but I can't quite identify it.

library(tidyverse)
library(magrittr)

#Lazy: Doesn't run
iris%>%
  map(.x = .$Sepal.Length,
      .f = function(.x){
        print(.x)
      })

#Eager: Doesn't run
iris%!>%
  map(.x = .$Sepal.Length,
      .f = function(.x){
        print(.x)
      })

#Runs
map(.x = iris$Sepal.Length,
    .f = function(.x){
      print(.x)
    })

Aegis
  • 145
  • 10
  • 1
    If you don't have `.` by itself in the call, `%>%` will pass the input to the first value of the function. If you want to disable that behavior, wrap your expression in a `{}` block. For example: `iris%>%{map(.x = .$Sepal.Length,.f = function(.x){print(.x)})}` otherwise your first example is the same as `map(iris, .x=.$Sepal.Length, .f = function(.x) print(.x) })` – MrFlick Apr 27 '23 at 15:46
  • In the first block, if I use ~print(.x) instead of function(.x){print(.x)}, it runs. Do you know why? Does it work for a reason related to your solution? – Aegis Apr 27 '23 at 15:51
  • 1
    It would still be passing `iris` to your anonymous function. via the `...` mechanism. If you try the unnamed parameter version you can see that it's not right either: `iris%>% map(.$Sepal.Length, ~print(.x))`. If you pulled the column out first, you could just do `iris %>% pull(Sepal.Length) %>% map(~print(.x))`. – MrFlick Apr 27 '23 at 15:55

0 Answers0