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

Subassignment with a symbol doesn't work #893

Closed
DavisVaughan opened this issue Jun 4, 2021 · 5 comments · Fixed by #906
Closed

Subassignment with a symbol doesn't work #893

DavisVaughan opened this issue Jun 4, 2021 · 5 comments · Fixed by #906
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@DavisVaughan
Copy link
Member

It does work for data frames though, so I figure we should be consistent.

Found here:
https://community.rstudio.com/t/unquoting-issue-in-purrr-map-df-function/106676/2

library(tibble)
library(rlang)

df <- data.frame(x = 1)
tbl <- tibble(x = 1)

x_sym <- sym("x")
x_chr <- "x"

df[[x_chr]] <- 2
tbl[[x_chr]] <- 2

# Can subset-assign with a symbol for data frames
df[[x_sym]] <- 2

# Can't subset-assign with a symbol for tibbles
tbl[[x_sym]] <- 2
#> Error: Can't use character names to index an unnamed vector.
@DavisVaughan
Copy link
Member Author

Hmm, I guess this is just passing through to vec_as_location2(), so maybe it is a vctrs issue @lionel-?

subclass_col_index_errors(vec_as_location2(j, n, names), j_arg = j_arg, assign = assign)

@lionel-
Copy link
Member

lionel- commented Jun 4, 2021

I think so. We're now generally supporting symbol indexing so this should be fixed in vctrs.

@DavisVaughan
Copy link
Member Author

Oh wait, this does already work in vctrs

> vctrs::vec_as_location2(rlang::sym("x"), n = 1L, names = "x")
[1] 1

My analysis was slightly off. This is.vector(j) call returns FALSE for symbols, but TRUE for character vectors.

if (!is.vector(j) || length(j) != 1L || is.na(j) || (is.numeric(j) && j < 0) || is.logical(j)) {

I think this is actually a tibble bug

@krlmlr krlmlr added this to the 3.1.2 milestone Jun 9, 2021
@krlmlr krlmlr added the bug an unexpected problem or unintended behavior label Jun 24, 2021
@krlmlr krlmlr modified the milestones: 3.1.2, 3.1.3 Jul 17, 2021
@krlmlr
Copy link
Member

krlmlr commented Jul 20, 2021

It's even funnier: we do support symbols for one-bracket assignment, data frames don't.


author: kirill
date: 2021-07-20
output: "reprex::reprex\_document"
title: irate-booby_reprex.R

library(tibble)
library(rlang)

df <- data.frame(x = 1)
tbl <- tibble(x = 1)

x_sym <- sym("x")
x_chr <- "x"

df[[x_chr]] <- 2
df[x_chr] <- 2
tbl[[x_chr]] <- 2

df[[x_sym]] <- 1
df[x_sym] <- 1
#> Error in `[<-.data.frame`(`*tmp*`, x_sym, value = 1): anyNA() applied to non-(list or vector) of type 'symbol'

tbl[[x_sym]] <- 2
#> Error: Can't use character names to index an unnamed vector.
tbl[x_sym] <- 2

Created on 2021-07-20 by the reprex package (v2.0.0)

krlmlr added a commit that referenced this issue Jul 20, 2021
@github-actions
Copy link
Contributor

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 21, 2022
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.

3 participants