Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!!as.name() does not work with nested tibbles using map2() #5511

Closed
vovoch opened this issue Aug 31, 2020 · 1 comment
Closed

!!as.name() does not work with nested tibbles using map2() #5511

vovoch opened this issue Aug 31, 2020 · 1 comment

Comments

@vovoch
Copy link

vovoch commented Aug 31, 2020

I ran into inability to use !!as.name() with .x or .y input variables inside of mutate( map2( rename(...) ).
I provide a reprex with several attempts below.

Also I would like to add it is very strange but .x and .y global variables are not visible on the Environment tab in Data or Values sections in RStudio 1.3.1073. It is a problem if you used them for debugging previously without further rm(). The only way to know about their presence is to print them in the console.

library(tidyverse)

df <- tibble(
  var_name = "new_name",
  data = list(data.frame(foo = 1))
)

df
#> # A tibble: 1 x 2
#>   var_name data            
#>   <chr>    <list>          
#> 1 new_name <df[,1] [1 x 1]>

df$data
#> [[1]]
#>   foo
#> 1   1

# OK example 1
df %>% mutate(data = map2(data, var_name, ~.x %>% mutate(foo = .y))) %>% .$data
#> [[1]]
#>        foo
#> 1 new_name

# OK example 2
df %>% mutate(data = map(data, ~.x %>% rename(!!as.name("new_name") := foo))) %>% .$data
#> [[1]]
#>   new_name
#> 1        1

# ERROR example 1
df %>% mutate(data = map2(data, "new_name", ~.x %>% rename(!!as.name(.y) := foo)))
#> Error in as.name(.y): object '.y' not found

# ERROR example 2
df %>% mutate(data = map2(data, var_name, ~.x %>% rename(!!as.name(.y) := foo)))
#> Error in as.name(.y): object '.y' not found

# ERROR example 3
df %>% mutate(data = map2(var_name, data, ~.y %>% rename(!!as.name(.x) := foo)))
#> Error in as.name(.x): object '.x' not found

# Example with global .y value 1
.y <- "global"
df %>% mutate(data = map2(data, var_name, ~.x %>% rename(!!as.name(.y) := foo))) %>% .$data
#> [[1]]
#>   global
#> 1      1

# Example with global .y value 2
.y <- list("a")
df %>% mutate(data = map2(data, var_name, ~.x %>% rename(!!as.name(.y) := foo))) %>% .$data
#> Error in as.name(.y): invalid type/length (symbol/1) in vector allocation

# repeating ERROR example 2
rm(.y)
df %>% mutate(data = map2(data, var_name, ~.x %>% rename(!!as.name(.y) := foo))) %>% .$data
#> Error in as.name(.y): object '.y' not found

Created on 2020-09-01 by the reprex package (v0.3.0)

@vovoch vovoch changed the title !!as.name() doesn't work with input variables inside of map2(mutate()) !!as.name() doesn't work with input variables inside of map2(rename()) Aug 31, 2020
@vovoch vovoch changed the title !!as.name() doesn't work with input variables inside of map2(rename()) !!as.name() doesn't work with nested tibbles using map2() Aug 31, 2020
@vovoch vovoch changed the title !!as.name() doesn't work with nested tibbles using map2() !!as.name() does not work with nested tibbles using map2() Aug 31, 2020
@lionel-
Copy link
Member

lionel- commented Sep 1, 2020

This is expected. !! happens very early, at the time the first mutate() is called. At that point, the purrr lambda isn't executing yet, so there's no .x or .y argument.

We have plans to possibly change that in the next major version of rlang: r-lib/rlang#845. Maybe this would be too large of a breaking change and we can't go through with it.

@lionel- lionel- closed this as completed Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants