-
-
Notifications
You must be signed in to change notification settings - Fork 8
Open
Labels
Description
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.