purrr and dplyr conflicts when trying to use "." as a dplyr functions' argument

I'm working on an excel spreadsheet with many sheets, each sheet represents a kind of product, and every sheet consists of the same table structure, but what really sad is, the only distinction between all those tables is the sheet name and nothing represents in the table. Thus, to bind them together and make me not messy them up, I have to add the sheet name as a primary key to the table.
I want to read them all in one map to a list of data frames, then use bind_rows() to bind them into a whole, my solutions works like that way:

library(dplyr)
name = c("dog", "cat")
exmp = data.frame(num = rnorm(100)) # to the real data, it is map(name, ~ read_excel(path, sheet = .), and these works as expected

purrr::map(name, ~ exmp %>% mutate(tag = .))

it results in:

Error in tbl_df(.data) : argument ".data" is missing, with no default

I know an explicit loop can solve that, but I'm interested in a "tidy" way and these phenomenon may relate to NSE I suppose, and I'm interested in what really happens inside the function.

Regarding reading in Excel files with many sheets, this should get you started:
https://readxl.tidyverse.org/articles/articles/readxl-workflows.html#iterate-over-multiple-worksheets-in-a-workbook

Thanks for a good reply for solving my first task(how to read a multi-sheets table)!
But what Iā€™m really interested in is that how to bind them together with each one an identity and not messy them up.

The full version is further down on the same page:
https://readxl.tidyverse.org/articles/articles/readxl-workflows.html#putting-it-all-together

I think purrr::map2_dfr() is the best way to go about your task, as described in the link Martin pointed out to you, but to answer the question in your topic title, to avoid name collisions you should use the full parameter name i.e .x instead of .
This is exactly why the use of . is disencouraged when working with purrr functions.

library(dplyr)
name = c("dog", "cat")
exmp = data.frame(num = rnorm(100)) # to the real data, it is map(name, ~ read_excel(path, sheet = .), and these works as expected

purrr::map(name, ~ exmp %>% mutate(tag = .x))
#> [[1]]
#>             num tag
#> 1    1.08797194 dog
#> 2    1.36797685 dog
#> 3   -0.04580569 dog
#> 4    0.55540382 dog
#> 5    1.68809560 dog
#> 6   -0.77897599 dog
#> 7   -1.02219955 dog
#> 8    1.51511912 dog
#> 9   -1.11496992 dog
#> 10   0.23461216 dog
#> 11  -1.40869422 dog
#> 12   1.06710702 dog
#> 13  -0.45007003 dog
#> 14  -1.37873370 dog
#> 15  -0.14195191 dog
#> 16  -0.03567318 dog
#> 17   0.45416636 dog
#> 18  -0.03658434 dog
#> 19  -1.27588663 dog
#> 20   1.58728530 dog
#> 21   1.01074928 dog
#> 22  -0.01447381 dog
#> 23  -2.02663227 dog
#> 24   0.86160996 dog
#> 25  -0.57463357 dog
#> 26   0.78413143 dog
#> 27  -0.98430090 dog
#> 28  -0.93863616 dog
#> 29  -0.54241223 dog
#> 30   1.51427662 dog
#> 31  -0.78392607 dog
#> 32   0.48780887 dog
#> 33   0.54601491 dog
#> 34  -0.70639579 dog
#> 35   1.17578650 dog
#> 36  -0.37590886 dog
#> 37   0.96448312 dog
#> 38  -0.29742858 dog
#> 39  -1.37644005 dog
#> 40  -1.12218019 dog
#> 41  -0.85236121 dog
#> 42   0.06883306 dog
#> 43  -0.28932515 dog
#> 44   0.97345443 dog
#> 45   0.19962829 dog
#> 46  -0.45399617 dog
#> 47  -1.84040315 dog
#> 48   1.55360424 dog
#> 49   0.28187637 dog
#> 50   0.31510375 dog
#> 51   0.65092930 dog
#> 52  -0.59274358 dog
#> 53   1.40459451 dog
#> 54  -0.57044692 dog
#> 55  -0.27227408 dog
#> 56  -0.40789342 dog
#> 57   0.13419103 dog
#> 58   0.44752898 dog
#> 59  -0.46449507 dog
#> 60   0.27995583 dog
#> 61  -0.76607400 dog
#> 62  -0.81636831 dog
#> 63   0.49164715 dog
#> 64  -0.90593078 dog
#> 65  -2.11796477 dog
#> 66  -0.54859520 dog
#> 67   0.25528267 dog
#> 68  -0.10590443 dog
#> 69  -0.81250577 dog
#> 70   1.25303883 dog
#> 71  -0.03826912 dog
#> 72  -1.42674102 dog
#> 73   2.31965030 dog
#> 74  -0.66360846 dog
#> 75  -0.36699076 dog
#> 76   0.13331265 dog
#> 77   2.08919048 dog
#> 78   0.06967059 dog
#> 79   0.61750171 dog
#> 80   1.21847642 dog
#> 81  -1.86632413 dog
#> 82  -2.23209910 dog
#> 83   0.23432913 dog
#> 84  -0.55059376 dog
#> 85   1.17429036 dog
#> 86  -0.78470434 dog
#> 87  -0.93235911 dog
#> 88   0.98956868 dog
#> 89  -1.93364995 dog
#> 90  -0.18014501 dog
#> 91   0.30737915 dog
#> 92   0.26749170 dog
#> 93  -1.76571044 dog
#> 94   0.89476091 dog
#> 95   0.57680824 dog
#> 96  -0.56370567 dog
#> 97  -0.57851277 dog
#> 98  -0.09325139 dog
#> 99   0.31769633 dog
#> 100 -0.93100578 dog
#> 
#> [[2]]
#>             num tag
#> 1    1.08797194 cat
#> 2    1.36797685 cat
#> 3   -0.04580569 cat
#> 4    0.55540382 cat
#> 5    1.68809560 cat
#> 6   -0.77897599 cat
#> 7   -1.02219955 cat
#> 8    1.51511912 cat
#> 9   -1.11496992 cat
#> 10   0.23461216 cat
#> 11  -1.40869422 cat
#> 12   1.06710702 cat
#> 13  -0.45007003 cat
#> 14  -1.37873370 cat
#> 15  -0.14195191 cat
#> 16  -0.03567318 cat
#> 17   0.45416636 cat
#> 18  -0.03658434 cat
#> 19  -1.27588663 cat
#> 20   1.58728530 cat
#> 21   1.01074928 cat
#> 22  -0.01447381 cat
#> 23  -2.02663227 cat
#> 24   0.86160996 cat
#> 25  -0.57463357 cat
#> 26   0.78413143 cat
#> 27  -0.98430090 cat
#> 28  -0.93863616 cat
#> 29  -0.54241223 cat
#> 30   1.51427662 cat
#> 31  -0.78392607 cat
#> 32   0.48780887 cat
#> 33   0.54601491 cat
#> 34  -0.70639579 cat
#> 35   1.17578650 cat
#> 36  -0.37590886 cat
#> 37   0.96448312 cat
#> 38  -0.29742858 cat
#> 39  -1.37644005 cat
#> 40  -1.12218019 cat
#> 41  -0.85236121 cat
#> 42   0.06883306 cat
#> 43  -0.28932515 cat
#> 44   0.97345443 cat
#> 45   0.19962829 cat
#> 46  -0.45399617 cat
#> 47  -1.84040315 cat
#> 48   1.55360424 cat
#> 49   0.28187637 cat
#> 50   0.31510375 cat
#> 51   0.65092930 cat
#> 52  -0.59274358 cat
#> 53   1.40459451 cat
#> 54  -0.57044692 cat
#> 55  -0.27227408 cat
#> 56  -0.40789342 cat
#> 57   0.13419103 cat
#> 58   0.44752898 cat
#> 59  -0.46449507 cat
#> 60   0.27995583 cat
#> 61  -0.76607400 cat
#> 62  -0.81636831 cat
#> 63   0.49164715 cat
#> 64  -0.90593078 cat
#> 65  -2.11796477 cat
#> 66  -0.54859520 cat
#> 67   0.25528267 cat
#> 68  -0.10590443 cat
#> 69  -0.81250577 cat
#> 70   1.25303883 cat
#> 71  -0.03826912 cat
#> 72  -1.42674102 cat
#> 73   2.31965030 cat
#> 74  -0.66360846 cat
#> 75  -0.36699076 cat
#> 76   0.13331265 cat
#> 77   2.08919048 cat
#> 78   0.06967059 cat
#> 79   0.61750171 cat
#> 80   1.21847642 cat
#> 81  -1.86632413 cat
#> 82  -2.23209910 cat
#> 83   0.23432913 cat
#> 84  -0.55059376 cat
#> 85   1.17429036 cat
#> 86  -0.78470434 cat
#> 87  -0.93235911 cat
#> 88   0.98956868 cat
#> 89  -1.93364995 cat
#> 90  -0.18014501 cat
#> 91   0.30737915 cat
#> 92   0.26749170 cat
#> 93  -1.76571044 cat
#> 94   0.89476091 cat
#> 95   0.57680824 cat
#> 96  -0.56370567 cat
#> 97  -0.57851277 cat
#> 98  -0.09325139 cat
#> 99   0.31769633 cat
#> 100 -0.93100578 cat

Created on 2019-07-24 by the reprex package (v0.3.0.9000)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.