-1

I've followed the excellent answer, as described here: https://stackoverflow.com/a/59987272/7493594

But how can I make it work with ggadjustedcurves?

  myfun <- function(TimeVar, EventVar, CoxVar, CoxData){
  TimeVar <- as.name(TimeVar)
  EventVar <- as.name(EventVar)
  CoxVar <- as.name(CoxVar)
  CoxModel <- eval(bquote(coxph(Surv(.(TimeVar), .(EventVar)) ~.(CoxVar), data = .(CoxData))))
  ggadjustedcurves(CoxModel, 
                   variable = CoxVar, 
                   xlab = "Years", 
                   ylab = "Survival", 
                   ggtheme =  theme_survminer(), 
                   size = 2, palette = "lancet", 
                   data = CoxData)
AkuaMJ
  • 3
  • 1
  • 1
    Please include minimal & representative sample data and how you'd want to use `myfun`, e.g. are you planning on passing `TimeVar`, `EventVar`, etc. as strings, symbols, ...? – Maurits Evers Jul 06 '22 at 11:47

1 Answers1

0

I guess you're after something like this?

library(survminer)
library(survival)
myfun <- function(TimeVar, EventVar, CoxVar, CoxData) {
    TimeVar <- as.name(TimeVar)
    EventVar <- as.name(EventVar)
    CoxVar_char <- CoxVar               # Need to store `CoxVar` as string 
    CoxVar <- as.name(CoxVar)
    CoxData <- as.name(CoxData)
    
    CoxModel <- eval(bquote(
        coxph(Surv(.(TimeVar), .(EventVar)) ~.(CoxVar), data = .(CoxData))))
    
    ggadjustedcurves(
        CoxModel, 
        variable = CoxVar_char,         # `variable` needs to be a string
        xlab = "Years", 
        ylab = "Survival", 
        ggtheme =  theme_survminer(), 
        size = 2, palette = "lancet",
        data = eval(CoxData))           # Eval `CoxData` as symbol
}
myfun("stop", "event", "size", "bladder")

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68