2

In this tutorial, there is the following code:

library(repurrrsive)
library(listviewer)
library(purrr)
library(tibble)
got_chars %>% {
  tibble(
       name = map_chr(., "name"),
    culture = map_chr(., "culture"),
     gender = map_chr(., "gender"),       
         id = map_int(., "id"),
       born = map_chr(., "born"),
      alive = map_lgl(., "alive")
  )
}

And the explanation provided says: "The dot . above is the placeholder for the primary input: got_chars in this case. The curly braces {} surrounding the tibble() call prevent got_chars from being passed in as the first argument of tibble()"

I'd like an explanation to what the {} are doing, and is this a general r property, or one exclusive to purrr? Is this similar to defining a nameless function such as function(x){ x + 1}?

Henrik
  • 65,555
  • 14
  • 143
  • 159
kmace
  • 1,994
  • 3
  • 23
  • 39
  • I notice that `rnorm(100000) %>% {sum(.)/length(.)}` does the same thing as: `rnorm(100000) %>% (function(.){sum(.)/length(.)})`, So I believe my intuition is correct. I'm still unclear on whether this is a general r property is some black magic from the tidyverse. – kmace Jan 04 '18 at 20:10
  • 7
    The comments here [When should we use curly brackets when piping with dplyr](https://stackoverflow.com/questions/42623497/when-should-we-use-curly-brackets-when-piping-with-dplyr) point you to relevant docs. – Henrik Jan 04 '18 at 20:11
  • 1
    Nice comments there, Henrik, I was just about to do the same. – Gregor Thomas Jan 04 '18 at 20:33
  • I guess the only followup question I have is: are the curly brackets performing the same function that they do when I am defining a function? I guess I never really thought about what they do – kmace Jan 04 '18 at 20:51

0 Answers0