title: "Object not found" error within a user defined function, eval() function?
body: <p>I'm trying to write a function for a task I need to do many times (running a cox proportional hazards function over multiple imputed datasets).  When I pass the necessary objects to my user-defined function, however, it gives an error, stating that the object cannot be found. I think this is because the object is defined within a dataframe that is specified with the "data=" argument within the cch() function. Can anyone help me with this?</p>
  
  <p>Example data:</p>
  
  <pre><code>my.list&lt;-list(my.df1 &lt;- data.frame(my.id = 1:100, my.time = rlnorm(100), 
  my.event= c(rbinom(50,1,0.2),rep(1,50)), my.det=rbinom(100,1,0.5), 
  sub= c(rep(1,50), rbinom(50, 1, 0.1))),  my.df2 &lt;- data.frame(my.id = 1:100, 
  my.time = rlnorm(100), my.event= c(rbinom(50,1,0.2),rep(1,50)), 
  my.det=rbinom(100,1,0.5), sub= c(rep(1,50), rbinom(50, 1, 0.1))))
  </code></pre>
  
  <p>Outside my user-defined function, this works:</p>
  
  <pre><code>library(KMsurv)
  library(survival)
  cch(Surv(my.time,my.event)~as.factor(my.det), data=my.df1, subcoh=~sub, 
  id=~my.id, cohort.size=500)
  </code></pre>
  
  <p>However, this does not work (this is an example function, not the real function as the real function is more complex and runs analyses on multiple datasets, then combines them):</p>
  
  <pre><code>myfun&lt;-function(dflist,time,event){
  for (i in 1:length(dflist)){
  out&lt;-cch(Surv(time,event)~as.factor(my.det), data=dflist[[i]], 
  subcoh=~sub, id=~my.id, cohort.size=500)
  print(out)}
  }   
  myfun(my.list,my.time,my.event)
  </code></pre>
  
  <p>I get this error: "Error in Surv(time, event) : object 'my.time' not found".</p>
  
  <p>I found some posts about using an eval(substitute()) function to deal with a similar problem, but I can't get it to work. Any suggestions are greatly appreciated!</p>