My goal is to run a function that will use an object created into another function.
I do have a function that initialize an object and run another function after that.
I do not achieve to retrieve the value of the object and I don't understand why.
Here's a quick example:
library(tidyverse)
func_1 <- function() {
ToFilter <- c("group")
func_2()
}
func_2 <- function() {
selection <-
PlantGrowth %>%
dplyr::filter(sym(ToFilter) == "trt2")
}
test <- func_1()
I do get the following error:
> test <- func_1()
Error in `dplyr::filter()`:
! Problem while computing `..1 = ... == "trt2"`.
Caused by error in `noquote()`:
! object 'ToFilter' not found
What I need to get is that:
selection <-
PlantGrowth %>%
dplyr::filter(group == "trt2")
I tried a lot of different things with no success. I don't understand because sym(ToFIlter)
gives group
as a result, which is exactly what I need. noquote()
and ensym()
are also working externally to a function, but not into it.
ToSel <- c("group")
ToSel
sym(ToSel)
noquote(ToSel)
ensym(ToSel)
> ToSel <- c("group")
> ToSel
[1] "group"
> sym(ToSel)
group
> noquote(ToSel)
[1] group
> ensym(ToSel)
group
>
I don't understand how to get the result I want, and I'm pretty lost in the rlang
world. Could you help me? Thanks a lot!