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
Contribution Guidelines
Security Policy
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:
Created on 2025-10-24 with reprex v2.1.1
One workaround is to use the dplyr's walrus
:=assignment: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
withindoesn't have the@aliases withinso users searching with?withinwill not find it (but it is linked from eval_code and qenv).sessionInfo()
Relevant log output
Code of Conduct
Contribution Guidelines
Security Policy