Reputation: 10619
I have
explanatory_vars<-c("age", "bmi")
outcome<-"Surv(time = mytime, event = myevent)"
Why does this work
explanatory_vars %>%
str_c(paste(outcome," ~ ", sep=""), .)
whereas this
explanatory_vars |>
str_c(paste(outcome," ~ ", sep=""), .)
throws an error?
Error in str_c(explanatory_vars, paste(outcome, " ~ ", sep = ""), .) :
object '.' not found
I was under the impression that %>%
and |>
are interchangeable.
Upvotes: 2
Views: 68
Reputation: 886938
We can specify a named argument with _
. With str_c
, it is a variadic argument (...
), so we need to backquote it
library(stringr)
explanatory_vars |>
str_c(paste(outcome," ~ ", sep=""), `...` = _)
-output
[1] "Surv(time = mytime, event = myevent) ~ age"
[2] "Surv(time = mytime, event = myevent) ~ bmi"
Or another option is to have a lambda function
explanatory_vars |>
{\(x) str_c(paste(outcome," ~ ", sep=""), x)}()
-output
[1] "Surv(time = mytime, event = myevent) ~ age"
[2] "Surv(time = mytime, event = myevent) ~ bmi"
Or another option with reformulate
explanatory_vars |>
sapply(reformulate, response = outcome)
-output
$age
Surv(time = mytime, event = myevent) ~ age
<environment: 0x7fce8fbee740>
$bmi
Surv(time = mytime, event = myevent) ~ bmi
<environment: 0x7fce8fbee740>
Upvotes: 2