9

I'm getting unexpected results when attempting to pipe inside map()

map(ls(), ~ . %>% get %>% dim)

returns the following message:

Functional sequence with the following components:

 1. get(.)
 2. dim(.)

Use 'functions' to extract the individual functions. 

I don't really know how functions() would get me the result I want.

Is there a way to do it with pipes and map?

Without using pipes,

map(ls(), ~ get(dim(.)))

, the result is what I would expect.

vuzun
  • 942
  • 2
  • 8
  • 15

1 Answers1

12

. %>% get %>% dim is already a function so just omit the ~, i.e.

map(ls(), . %>% get %>% dim)

or:

ls() %>% map(. %>% get %>% dim)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks, that worked! I don't really understand how that chain is a function if I start it with data though. – vuzun Apr 17 '18 at 13:12
  • 1
    A magrittr pipe whose left hand side is dot defines a function. See the paragraph **Using the dot-place holder as lhs** in `help("%>%", "magrittr")` – G. Grothendieck Apr 17 '18 at 13:18