错误:嵌套函数中“未找到对象" [英] error: 'object not found' in nested functions

查看:75
本文介绍了错误:嵌套函数中“未找到对象"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 logistf :: logistf logistf :: forward 函数编写函数.在这里,我将使用 logistf 包中的 sex2 给出一个最小的工作示例.

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)

我得到以下输出:

第0步:启动模型

Step 0 : starting model

is.data.frame(data)中的错误:找不到对象'datamod'.

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

有人对此进行解释吗?

推荐答案

这是一个典型的错误,您可以在R中得到.它再次被问到,不幸的是,它是根据不同功能在不同环境中的工作方式以及功能的不同而发生的尝试根据 parent.env parent.frame 的使用查找数据.这可能是两个问题之一:

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:

  • 懒惰评估问题:
    尝试在 logistf 函数之前使用 force(datamod),因为您的自定义函数当前未评估datamod.如果存在以下问题,这可能不起作用:
  • 您的datamod数据集存在于函数的执行环境中.如果功能链中的一个功能使用对 parent.frame()的调用或对 parent_env()的调用,则由于R在不同环境中查找数据的方式不同.解决此问题的唯一方法是在全局环境中启动datamod,即:
  • 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>

如上所示,

在调用环境中使用 get 可以正常工作,而在父环境中使用 get 会失败并产生错误.这(可能)也在您的函数中发生.

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.

这篇关于错误:嵌套函数中“未找到对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆