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

Referencing columns with [ ] fails when column names are created from glue #1150

Closed
asadow opened this issue May 10, 2022 · 1 comment · Fixed by #1503
Closed

Referencing columns with [ ] fails when column names are created from glue #1150

asadow opened this issue May 10, 2022 · 1 comment · Fixed by #1503
Labels
bug an unexpected problem or unintended behavior

Comments

@asadow
Copy link

asadow commented May 10, 2022

Like #778, but with multiple columns. Again casting the return as a character seems to work ok.

library(tibble)

tib <- tibble(a = 1, b = 2)
tib[glue::glue("c{1:2}")] <- "bar"
#> Error: Can't assign to columns that don't exist.
#> x Columns `c1` and `c2` don't exist.
tib[as.character(glue::glue("c{1:2}"))] <- "bar"

df <- data.frame(a = 1, b = 2)
df[glue::glue("c{1:2}")] <- "bar"

Created on 2022-05-10 by the reprex package (v2.0.1)

@krlmlr
Copy link
Member

krlmlr commented Dec 4, 2022

Thanks, confirmed:

library(tibble)

tib <- tibble(a = 1, b = 2)
cols <- glue::glue("c{1:2}")
tib[cols] <- "bar"
#> Error in `vectbl_as_new_col_index()`:
#> ! Can't assign to columns that don't exist.
#> ✖ Columns `c1` and `c2` don't exist.

#> Backtrace:
#>      ▆
#>   1. ├─base::`[<-`(`*tmp*`, cols, value = `<chr>`)
#>   2. ├─tibble:::`[<-.tbl_df`(`*tmp*`, cols, value = `<chr>`)
#>   3. │ └─tibble:::tbl_subassign(x, i, j, value, i_arg, j_arg, substitute(value))
#>   4. │   └─tibble:::vectbl_as_new_col_index(j, x, j_arg, names2(value), value_arg)
#>   5. │     └─tibble:::vectbl_as_col_location(...)
#>   6. │       ├─tibble:::subclass_col_index_errors(...)
#>   7. │       │ └─base::withCallingHandlers(...)
#>   8. │       └─vctrs::vec_as_location(j, n, names, call = call)
#>   9. └─vctrs (local) `<fn>`()
#>  10.   └─vctrs:::stop_subscript_oob(...)
#>  11.     └─vctrs:::stop_subscript(...)
#>  12.       └─rlang::abort(...)
tib[as.character(cols)] <- "bar"
tib
#> # A tibble: 1 × 4
#>       a     b c1    c2   
#>   <dbl> <dbl> <chr> <chr>
#> 1     1     2 bar   bar

df <- data.frame(a = 1, b = 2)
df[cols] <- "bar"
df
#>   a b  c1  c2
#> 1 1 2 bar bar

Created on 2022-12-04 with reprex v2.0.2

@krlmlr krlmlr added the bug an unexpected problem or unintended behavior label Dec 4, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants