简体   繁体   English

将对象传递给函数

[英]Passing objects to functions

I am currently working on user defined functions aimed at modelling empirical data and I have problems with objects / parameters passed to the function: 我目前正在开发旨在对经验数据进行建模的用户定义函数,但是传递给该函数的对象/参数存在问题:

bestModel <- function(k=4L, R2=0.994){
  print(k)   # here, everything is still fine

  lmX <- mixlm::lm(getLinearModelFunction(k), data)
  best <- mixlm::best.subsets(lmX, nbest=1)
  .
  .
  .
}

At first, everything works as expected, but as soon as I want to pass the parameter k to another user defined function getLinearModelFunction() , an error is thrown: 最初,一切都按预期工作,但是一旦我想将参数k传递给另一个用户定义的函数getLinearModelFunction() ,就会引发错误:

Error in getLinearModelFunction(k) : object 'k' not found 

It doesn't help, if I am assigning a new parameter, eg l <- k and try to pass that on. 如果我要分配一个新参数(例如l <- k并尝试将其传递,那将无济于事。 The parameter doesn't seem to be available for the other function. 该参数似乎不适用于其他功能。 I ran into this problem not only with primitive data types, but as well complex structures. 我不仅遇到了原始数据类型,还遇到了复杂的结构。 On command line, everything works, as long as the objects are in my workspace. 在命令行上,只要对象在我的工作空间中,一切都可以工作。

To sum it up: Passing parameters work only within that function, but calls of other functions from there onwards result in error. 概括起来:传递参数仅在该函数内起作用,但是从此以后调用其他函数会导致错误。 Why? 为什么? And: What to do about it? 并且:该怎么办?

EDIT: While trying to resolve the problem, it gets really weird. 编辑:在尝试解决问题时,它真的很奇怪。 I stripped down all functions: 我剥离了所有功能:

functionA <- function(data, k){
      lmX <- mixlm::lm(functionB(k), data)
      summary(lmX)

      # best <- mixlm::best.subsets(lmX,nbest=1)
    }


    functionB <- function(k=4){
      if(k==1){
        return(formula("raw ~ L1"))
      }else if(k==2){
        return(formula("raw ~ L1 + L2"))
      }else if(k==3){
        return(formula("raw ~ L1 + L2 + L3 "))
      }else if(k==4){
        return(formula("raw ~ L1 + L2 + L3 + L4"))
      }
    }

Let's say, we have a data.frame d with the variables raw, L1, L2, L3, L4 ... As long, as there is the commenting # before best, it works. 假设,我们有一个data.frame d,其中包含raw,L1,L2,L3,L4变量...只要在最前面添加注释#,它就可以工作。 As soon as it is removed, calling functionA(d, 3) results in 删除函数后,立即调用functionA(d,3)

Error in functionB(k) : object 'k' not found  

Even, though k doesn't play a role in that function and before that, it worked. 即使k在该函数中不起作用,在此之前它仍然起作用。

Ok, indeed, this was an environment thing. 好的,的确,这是环境问题。 The solution is to get the current environment and to take the object from there: 解决方案是获取当前环境并从那里获取对象:

functionA <- function(data, k){
   e <- environment()
   lmX <- mixlm::lm(functionB(e$k), e$data)
   summary(lmX)

   best <- mixlm::best.subsets(lmX,nbest=1)
 }

This is usually not a problem, when directly working with are packages. 当直接使用are包时,这通常不是问题。 The objects usually are in the global environments then. 这些对象通常位于全局环境中。 When working with functions, each function has its' own environment. 使用功能时,每个功能都有其自己的环境。 I managed to solve this while starting to learn about packaging the code: http://adv-r.had.co.nz/Environments.html 在开始学习打包代码的过程中,我设法解决了这一问题: http : //adv-r.had.co.nz/Environments.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM