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 column with [[ ]] fails when column name is created from glue #778

Closed
dturkenk opened this issue Jun 2, 2020 · 6 comments
Closed
Labels
bug an unexpected problem or unintended behavior subsetting 🍽
Milestone

Comments

@dturkenk
Copy link

dturkenk commented Jun 2, 2020

Newly upgraded to tibble 3.0.1 and referencing a column using [[ ]] no longer seems to work if the column name is created using glue::glue.

Casting the return as a character seems to work ok.

library(tibble)
#> Warning: package 'tibble' was built under R version 3.6.3
df <- tribble(~a, ~b, "foo", 3)
lbl <- "c"
df[[glue::glue("{lbl}")]] <- "bar"
#> Error: Can't assign to columns that don't exist.
#> x Column `c` doesn't exist.
df[[as.character(glue::glue("{lbl}"))]] <- "bar"
@dturkenk
Copy link
Author

dturkenk commented Jun 2, 2020

An additional note, using glue::glue to reference a column that already exists seems to work fine as well.

@hadley
Copy link
Member

hadley commented Jun 5, 2020

Slightly more minimal reprex:

library(tibble)
#> Warning: package 'tibble' was built under R version 3.6.2
df <- tibble(a = 1, b = 2)
df[[glue::glue("c")]]
#> NULL
df[[glue::glue("c")]] <- "bar"
#> Error: Can't assign to columns that don't exist.
#> x Column `c` doesn't exist.

Created on 2020-06-05 by the reprex package (v0.3.0)

@hadley hadley added bug an unexpected problem or unintended behavior subsetting 🍽 labels Jun 5, 2020
@krlmlr krlmlr added this to the 3.0.2 milestone Jul 5, 2020
@krlmlr
Copy link
Member

krlmlr commented Jul 5, 2020

This should be solved upstream, otherwise we'll end up replicating lots of vctrs logic here:

library(vctrs)

vec_as_location(glue::glue("a"), 26, letters)
#> [1] 1
vec_as_location2(glue::glue("a"), 26, letters)
#> [1] 1

foo <- function(x) {
  structure(x, class = "foo")
}

vec_as_location(foo("a"), 26, letters)
#> Error: Must subset elements with a valid subscript vector.
#> x Subscript has the wrong type `foo`.
#> ℹ It must be logical, numeric, or character.
vec_as_location2(foo("a"), 26, letters)
#> Error: Must extract element with a single valid subscript.
#> x Subscript has the wrong type `foo`.
#> ℹ It must be numeric or character.

Created on 2020-07-05 by the reprex package (v0.3.0)

@krlmlr krlmlr removed this from the 3.0.2 milestone Jul 5, 2020
@krlmlr
Copy link
Member

krlmlr commented Jul 5, 2020

On the other hand, we can use vec_as_subscript() to be more consistent here.

@krlmlr krlmlr added this to the 3.0.2 milestone Jul 5, 2020
@krlmlr krlmlr closed this as completed in 8dc679d Jul 6, 2020
@krlmlr
Copy link
Member

krlmlr commented Jul 6, 2020

Now:

library(tibble)

df <- tibble(a = 1, b = 2)
df[[glue::glue("c")]]
#> NULL
df[[glue::glue("c")]] <- "bar"
df
#> # A tibble: 1 x 3
#>       a     b c    
#>   <dbl> <dbl> <chr>
#> 1     1     2 bar

Created on 2020-07-06 by the reprex package (v0.3.0)

krlmlr added a commit that referenced this issue Feb 25, 2021
tibble 3.0.2

- `[[` works with classed indexes again, e.g. created with `glue::glue()` (#778).
- `add_column()` works without warning for 0-column data frames (#786).
- `tribble()` now better handles named inputs (#775) and objects of non-vtrs classes like `lubridate::Period` (#784) and `formattable::formattable` (#785).

- Subsetting and subassignment are faster (#780, #790, #794).
- `is.null()` is preferred over `is_null()` for speed.
- Implement continuous benchmarking (#793).

- `is_vector_s3()` is no longer reexported from pillar (#789).
@github-actions
Copy link
Contributor

github-actions bot commented Jul 7, 2021

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 7, 2021
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 subsetting 🍽
Projects
None yet
Development

No branches or pull requests

3 participants