Writing functions in R, keeping scoping in mind

后端 未结 4 1003
臣服心动
臣服心动 2020-12-13 18:52

I often write functions that need to see other objects in my environment. For example:

> a <- 3
> b <- 3
> x <- 1:5
> fn1 <- functio         


        
4条回答
  •  暖寄归人
    2020-12-13 19:20

    Does the problem come about when you're just using a global variable in a function or when you try to assign the variable? If it's the latter I suspect it's because you're not using <<- as an assignment within the function. And while using <<- appears to be the dark side 1 it may very well work for your purposes. If it is the former, the function is probably masking the global variable.

    Naming global variables in a manner that it would be difficult to mask them locally might help. e.g.: global.pimultiples <- 1:4*pi

提交回复
热议问题