I'm creating a function which takes column names as arguments and to plot survival plots. however my arguments can't be found in the calling function.
thanks in advance for your help
plt_survive <- function(data_in, time_var, event_var, group_var){
require(ggsurvfit)
require(survival)
p_tittle <- as_label(group_var) # use plot title
p <- survfit2(Surv({{time_var}}, {{event_var}}) ~ {{group_var}}, data = data_in) %>%
ggsurvfit(linewidth = 1) +
add_confidence_interval() +
add_risktable()
return(p)
}
plt_survive(data_in=df_test, time_var=time_to_event_yearmonth, event_var=dfs_event, group_var=restaging)
##### Error in is_quosure(quo) : object 'restaging' not found
data looks like this
df_test <- structure(list(time_to_event_yearmonth = c(90, 80, 89, 78, 8,
96), dfs_event = c(0, 0, 0, 0, 1, 0), restaging = c("ND", "D",
"D", NA, NA, "D"), follow_up = c("D", "D", "ND", "ND", "D", "D"
)), row.names = c(NA, -6L), class = "data.frame")
You shouldn't use this kind of syntax here. Replace the survfit2
call with
p <- survfit2(Surv(time_var) ~ group_var, data = data_in) %>% ...
and call your function by explicitly specifying df_test
:
plt_survive(
data_in = df_test,
time_var = df_test$time_to_event_yearmonth,
event_var = df_test$dfs_event,
group_var = df_test$restaging
)