error: 'object not found' in nested functions

不羁的心 提交于 2020-12-29 07:41:25

问题


I was writing a function using the logistf::logistf and logistf::forward function. I will give here a minimum working example using sex2 from the logistf package.

data(sex2)
fwSel <- function(datamod) {
  fitnull <- logistf(case ~ 1, data = datamod, pl = FALSE) 
  fw <- forward(fitnull)
  return(fw)
}
fwSel(sex2)

I get the following output:

Step 0 : starting model

Error in is.data.frame(data) : object 'datamod' not found`.

Has anybody an explanation for that?


回答1:


This is a typical error which you can get in R. It has been asked again and unfortunately it happens according to how different functions work in different environments and how functions try to find data according to the use of parent.env or parent.frame. It might be one of the two problems:

  • Lazy evaluation problem:
    Try to use force(datamod) before your logistf function because your datamod is not currently evaluated in your custom function. This might not work if the following problem exists:
  • Your datamod data set exists in your function's execution environment. If one of the functions inside the chain of functions uses a call to a parent.frame() or a call to parent_env(), this would cause a problem because of the different ways that R looks in different environments to find the data. The only way to solve this is to initiate datamod in the global environment i.e.:

data(sex2)
datamod <- sex2
fwSel <- function(datamod) {
    fitnull <- logistf(case ~ 1, data = datamod, pl = FALSE) 
    fw <- forward(fitnull)
    return(fw)
}
fwSel(sex2)

This will definitely work because the global environment will be searched anyway.

I find this link as an excellent way of finding out how the parent.env is different to parent.frame and how using those two inside functions can cause problems like the one you are facing.

I made a new example based on the functions in the link that demonstrates your problem exactly:

f = function() {
  print(c(f=environment(), defined_in=parent.env(environment()),  
    called_from=parent.frame()))

  #search for mydata in calling environment
  try(print(get('mydata',env=parent.frame())))  

  #search for mydata in parent evnironment
  try(print(get('mydata',env=parent.env(environment())))) 
  }

g = function() {
  mydata <- c(1,2,3)
  print(c(g=environment(), f()))
  }   

> g()
$f
<environment: 0x0000000030868df8>

$defined_in
<environment: R_GlobalEnv>

$called_from
<environment: 0x000000003086a360>

#the first get works perfect
[1] 1 2 3       

#the second produces an error
Error in get("mydata", env = parent.env(environment())) : 
  object 'mydata' not found

$g
<environment: 0x000000003086a360>

As you can see above using get with the calling environment works whereas using get with the parent environment fails and produces an error. This is what (probably) happens in your functions too.



来源:https://stackoverflow.com/questions/28601959/error-object-not-found-in-nested-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!