kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
509 stars 162 forks source link

eval in .get_pvalue #9

Closed MarcinKosinski closed 8 years ago

MarcinKosinski commented 8 years ago

I have seen that you are using risky parameter eval with default environment = parent.frame() https://github.com/kassambara/survminer/blob/master/R/ggsurvplot.R#L470

I am developing an [RTCGA](https://github/RTCGA/RTCGA) package for simple access to TCGA data with as simple plot functions as possible, since the target group are mainly biologists. I have appreacieated survminer as a alternative for survMisc and I am really glad, that my suggestions help upgrading survminer which result in better functionalities in RTCGA package.

I have wrote a wprapper for ggsurvplot in kmTCGA function : https://github.com/RTCGA/RTCGA/blob/master/R/kmTCGA.R#L53-L87 where I prepare survfit from names of parameters. This is comfortable for biologist because they don't need to bother about learning formula R specifications.

I have occured such an error with this function which brakes when I pass pval = TRUE

# install_github('RTCGA/RTCGA')
library(RTCGA)
## Extracting Survival Data
library(RTCGA.clinical)
survivalTCGA(BRCA.clinical, OV.clinical, extract.cols = "admin.disease_code") -> BRCAOV.survInfo

# first munge data, then extract survival info
library(dplyr)
BRCA.clinical %>%
     filter(patient.drugs.drug.therapy_types.therapy_type %in%
                c("chemotherapy", "hormone therapy")) %>%
     rename(therapy = patient.drugs.drug.therapy_types.therapy_type) %>%
     survivalTCGA(extract.cols = c("therapy"))  -> BRCA.survInfo.chemo

# first extract survival info, then munge data                  
    survivalTCGA(BRCA.clinical, 
                 extract.cols = c("patient.drugs.drug.therapy_types.therapy_type"))  %>%
    filter(patient.drugs.drug.therapy_types.therapy_type %in%
               c("chemotherapy", "hormone therapy")) %>%
    rename(therapy = patient.drugs.drug.therapy_types.therapy_type) -> BRCA.survInfo.chemo

## Kaplan-Meier Survival Curves
kmTCGA(BRCAOV.survInfo, explanatory.names = "admin.disease_code",  pval = TRUE)
Hide Traceback

 Rerun with Debug
 Error in eval(expr, envir, enclos) : object 'formu' not found 
6 eval(expr, envir, enclos) 
5 eval(fit$call$formula) 
4 survival::survdiff(eval(fit$call$formula), data = eval(fit$call$data)) at ggsurvplot.R#470
3 .get_pvalue(fit) at ggsurvplot.R#346
2 ggsurvplot(fit, risk.table = risk.table, conf.int = conf.int, 
    pval = pval, main = main, ...) at kmTCGA.R#73
1 kmTCGA(BRCAOV.survInfo, explanatory.names = "admin.disease_code", 
    pval = TRUE) 

So I understand from that error is:

kmTCGA passes fit in character mode to ggsurvplot, which uses .get_pvalue that eval the formula call in the environment of .get_pvalue where the variable times passed to kmTCGA is not seen, since eval uses default environment parent.frame().

I have no proposition for workaround for this. Do you know any function as match_all_parent_frames_levels? or get_anywhere/ getAnywhere()? If you have any suggestion you could fix survminer or provide a PR for RTCGA. I would really appreciate that. I think from this team work both packages may benefit.

kassambara commented 8 years ago

I'll figure it out how to fix this error in survminer. Meanwhile, I'd suggest using do.call() in kmTCGA function as follow:

fit <- do.call(survival::survfit, list(formula = formu, data = data))

Please, let me know if it works...

Best Regards, A. Kassambara

MarcinKosinski commented 8 years ago

Thanks. I'll Have a look at this :)

Marcin Kosinski

Dnia 17.02.2016 o godz. 20:23 Alboukadel KASSAMBARA notifications@github.com napisał(a):

I'll figure it out how to fix this error in survminer. Meanwhile, I'd suggest using do.call() in kmTCGA function as follow:

fit <- do.call(survival::survfit, list(formula = formu, data = data)) Please, let me know if it works...

Best Regards, A. Kassambara

— Reply to this email directly or view it on GitHub.

MarcinKosinski commented 8 years ago

This did solve the issue! Thanks for PR https://github.com/RTCGA/RTCGA/pull/48