Context
I would like to customize a function that uses non-standard evaluation (NSE) to fit the cox model. It works fine when I fit the cox model using the code.
But when I wrap the code into a custom function using NSE it reports an error.
Question
I don't know why using NSE in a custom function myfun
is reporting an error and how to fix this error.
Reproducible code
library(survival)
library(rms)
data(cancer)
# works fine in normal code
dd = datadist(lung)
options(datadist = 'dd')
fit1 = cph(Surv(time, status) ~ rcs(meal.cal), data = lung)
fit1
# report an error in self-define function using non-standard evaluation
myfun(data, var){
dd = datadist(data)
options(datadist = 'dd')
fit1 = cph(Surv(time, status) ~ rcs({{var}}), data = data)
fit1
}