Skip to content

[Bug]: within doesn't replace the lefthand side of an expression. #275

@llrs-roche

Description

@llrs-roche

What happened?

I wanted to mutate a variable by using as.factor but I found that using this approach doesn't replace the content but creates a new column:

library("teal.code")
var <- "Species"
q <- within(qenv(),
  {
    library("dplyr")
    iris <- iris
    iris2 <- mutate(iris, a = as.factor(a))
  },
  a = as.name(var)
)
setdiff(colnames(q$iris2), colnames(q$iris))
#> [1] "a"

Created on 2025-10-24 with reprex v2.1.1

One workaround is to use the dplyr's walrus := assignment:

library("teal.code")
var <- "Species"
q <- within(qenv(),
  {
    library("dplyr")
    iris <- iris
    iris2 <- mutate(iris, a := as.factor(a))
  },
  a = as.name(var)
)
setdiff(colnames(q$iris2), colnames(q$iris))
#> character(0)

Created on 2025-10-24 with reprex v2.1.1

but this might confuse readers of the code if this end up on the Show R code on teal.
If not a bug perhaps mentioning this on the documentation would be helpful to users.

Also, I realized that within doesn't have the @aliases within so users searching with ?within will not find it (but it is linked from eval_code and qenv).

sessionInfo()

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcore

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions