ggsurvplot:Error in parse(text = paste0("list(", paste(varnames, collapse = ","), : <text>:1:25: unexpected symbol

am trying to plot a KM curve using ggsurvplot. R throws an "unexpected symbol" , exception when I use a string something like this as a variable "RP11-181G12.2" (please see the code). There seems no issue when plotting using plot function.

rnaID <-"`RP11-181G12.2`"
days <- mergeData$Days
event <- as.integer(factor(mergeData$Vital_Status, levels = c("Alive", "Dead")))
surv <- sprintf("Surv(days,event)~as.numeric(%s >median(%s))",rnaID,rnaID)
fit_train <-survfit(formula=as.formula(surv),data= mergeData)
ggsurvplot(fit_train,data=mergeData)

When I give the string directly into surv string for the formula the ggsurvplot has no issues in ploting the curve but when I give it as variable it throws a error. I tried googling to find a solution but was unsuccessful. Any help would be appreciated

Hi, and welcome!

Your question can't be answered readily without a reproducible example, called a reprex. For one thing, ggsurvplot is in the survminer package, which requires some digging to discover. For another, the data are unknown, and the error is unidentified! There's not much to go on :grin:

Fortunately, in this case, the problem was easy to spot:

rnaID <-"`RP11-181G12.2`"
 
sprintf("Surv(days,event)~as.numeric(%s >median(%s))",rnaID,rnaID)
#> [1] "Surv(days,event)~as.numeric(`RP11-181G12.2` >median(`RP11-181G12.2`))"

Created on 2019-12-21 by the reprex package (v0.3.0)

Surv expects two numerics as an argument but surv is class character.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.