Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why ggsurvplot can not be called in function frame #288

Closed
ShixiangWang opened this issue Feb 1, 2018 · 6 comments
Closed

why ggsurvplot can not be called in function frame #288

ShixiangWang opened this issue Feb 1, 2018 · 6 comments

Comments

@ShixiangWang
Copy link

ShixiangWang commented Feb 1, 2018

Expected behavior

I wanna call ggsurvplot function instead of default plot function in my custom function

Actual behavior

A error occur.

Steps to reproduce the problem

  1. I debug my function and find something wrong when call ggsurvplot.

  2. I store my dataset used to call ggsurvplot
    df2.zip

  3. I reduce anything else not with this problem and create a minimal function, find the same problem.

test_fun <- function(df2){
    sfit <- survfit(Surv(df2[,"OS"], df2[, "OS_IND"])~group, data=df2)
    ggsurvplot(sfit)
}

test_fun(df2=df2)
> test_fun(df2=df2)
 Show Traceback
 
 Error in is.data.frame(data) : object 'df2' not found 
  1. I debug it in function and return
Error in eval(fit$call$data) : object 'df2' not found

But it is very strange that I can use eval(fit$call$data) inside the function.

  1. I directly use ggsurvplot in global environment, it works well.

  2. I try put df2 in global environment, it works well, and if I change the value of df2, error occur.

I figure it out that ggsurvplot function will retrieve origin data.frame in global environment instead of current frame, can you fix it?

session_info()

R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936 
[2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936   
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936
[4] LC_NUMERIC=C                                                   
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] survminer_0.4.2 ggpubr_0.1.6    magrittr_1.5    ggplot2_2.2.1   survival_2.41-3

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13        compiler_3.4.2      plyr_1.8.4          bindr_0.1           tools_3.4.2        
 [6] digest_0.6.12       tibble_1.3.4        nlme_3.1-131        gtable_0.2.0        lattice_0.20-35    
[11] pkgconfig_2.0.1     rlang_0.1.2         Matrix_1.2-11       psych_1.7.8         cmprsk_2.2-7       
[16] yaml_2.1.14         parallel_3.4.2      bindrcpp_0.2        gridExtra_2.3       knitr_1.17         
[21] dplyr_0.7.4         stringr_1.2.0       survMisc_0.5.4      grid_3.4.2          data.table_1.10.4-1
[26] glue_1.1.1          R6_2.2.2            KMsurv_0.1-5        km.ci_0.5-2         foreign_0.8-69     
[31] reshape2_1.4.2      tidyr_0.7.1         purrr_0.2.3         scales_0.5.0        splines_3.4.2      
[36] assertthat_0.2.0    mnormt_1.5-5        xtable_1.8-2        colorspace_1.3-2    labeling_0.3       
[41] stringi_1.1.5       lazyeval_0.2.0      munsell_0.4.3       broom_0.4.2         zoo_1.8-0   
@ShixiangWang ShixiangWang changed the title why ggsurvplot can not be called in function why ggsurvplot can not be called in function frame Feb 1, 2018
@kassambara
Copy link
Owner

Hi, when using ggsurvplot, you should always specify the argument data. In your case, specifying data = df2, should fix the issue

@ShixiangWang
Copy link
Author

ShixiangWang commented Feb 1, 2018 via email

@ShixiangWang
Copy link
Author

ShixiangWang commented Feb 2, 2018

Hello,
@kassambara it is still something wrong with the result

I used following coding to test [df2.zip]

load("./df2.Rdata")
test_fun <- function(df2){
    sfit <- survfit(Surv(df2[,"OS"], df2[, "OS_IND"])~group, data=df2)
    ggsurvplot(sfit, data=df2)
}
df1 <- df2; rm(df2)
test_fun(df2=df1)

the error is still

object 'df2' not found

I try debug function as deep as I can, and find in .extra.survfit function the formula is still what I parse to function, but the names of data.frame have been changed to data, so the function still can not find df2.

Browse[8]> stats::get_all_vars(.formula, data = data)
Error in eval(inp, data, env) : object 'df2' not found
Browse[8]> .formula
Surv(df2[, "OS"], df2[, "OS_IND"]) ~ df2$group
<environment: 0x000000001ad19d48>

I already know what is wrong,

if I change Surv(df2[,"OS"], df2[, "OS_IND"] to Surv(OS, OS_IND), your function works well.

test_fun <- function(df2){
    sfit <- survfit(Surv(OS, OS_IND)~group, data=df2)
    ggsurvplot(sfit, data=df2)
}

But I cannot do this because I want to write a function which I do not know which variable is mapping to time, which mapping to event in survival analysis.

I tried to use as.symbol function,

test_fun <- function(df2, time="OS", event="OS_IND"){
    sfit <- survfit(Surv(as.symbol(time), as.symbol(event))~group, data=df2)
    ggsurvplot(sfit, data=df2)
}

but

>test_fun(df2=df1)
 Show Traceback
 
 Rerun with Debug
 Error in Surv(as.symbol(time), as.symbol(event)) : 
  Time variable is not numeric 

I use

    colnames(df2)[colnames(df2) == time] <- "time"
    colnames(df2)[colnames(df2) == event] <- "event"
    sfit <- survfit(Surv(time, event) ~ group, data=df2)

to fix this.

It is strange that I cannot use

sfit <- survfit(as.formula(paste0("Surv(", time, ",", event, ") ~ group")), data=df2)

or

.formula <- as.formula(paste0("Surv(", time, ",", event, ") ~ group"))
sfit <- survfit(.formula, data=df2

I know little about advanced R. Can you give me a suggestion to set a right formula that ggsurvplot can rightly work?

@youyuxiansen
Copy link

you should declare the "df2" in global env like
$ options(df2='df2')
before you use it in
$ test_fun(df2=df1)

@JanaJarecki
Copy link

JanaJarecki commented Jun 2, 2023

I second this issue, I don't think it makes sense that a function argument to ggsurvplot has to be declared in the global environment. This seems (IMHO), with all due respect, not perfectly in line with the expected R scoping rules.

@rogercherry
Copy link

rogercherry commented Feb 5, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants