如何在函数内使用ls()搜索环境?

时间:2021-02-01 23:26:53

I want to find a set of functions and save them, because I want to send them to a remote server in an Rdata file, and I don't want to install a new package on the server.

我想找到一组函数并保存它们,因为我想将它们发送到Rdata文件中的远程服务器,我不想在服务器上安装新的软件包。

Although I am getting an error using the approach below, easier / better approaches are welcome.

虽然我使用下面的方法收到错误,但欢迎使用更简单/更好的方法。

MWE:

Here are two dummy functions:

这是两个虚函数:

abcd.fun.1    <- function() return(1)
abcd.fun.2    <- function() return(2)

I can identify the dummy functions:

我可以识别虚函数:

ls()[grep('abcd', ls())]

But when I wrap this in a function:

但是当我将它包装在一个函数中时:

 find.test <- function(x) {
     return(ls()[grep(x, ls())])
 }
 find.test('abcd')

The function returns character(0)

该函数返回字符(0)

Ultimately I would like to

最终我想

 save(find.test('abcd'), file = test.Rdata)

2 个解决方案

#1


10  

  1. Why not use the pattern= argument to ls?
  2. 为什么不使用ls的pattern =参数?

  3. Calling ls inside a function lists the objects that exist within the function scope, not the global environment (this is explained in ?ls).
  4. 在函数内部调用ls会列出函数作用域中存在的对象,而不是全局环境(这将在?中解释)。

If you want to list the objects in the global environment from a function, specify envir=.GlobalEnv.

如果要从函数列出全局环境中的对象,请指定envir = .GlobalEnv。

x <- 1:10
f <- function() ls()
g <- function() ls(envir=.GlobalEnv)
h <- function() ls(envir=.GlobalEnv, pattern="[fg]")
f()
# character(0)
g()
# [1] "f" "g" "h" "x"
h()
# [1] "f" "g"

#2


7  

You need to tell your function to list objects in an environment other than itself, e.g. the global environment. (And while you're at it, you can also specify the regex pattern as an argument to ls.):

您需要告诉您的函数列出除自身以外的环境中的对象,例如全球环境。 (当你在它时,你也可以指定正则表达式作为ls的参数。):

find.test <- function(x, envir=.GlobalEnv) {
  ls(pattern=x, envir=envir) 
}

See ?ls for more info about ls() and ?environment for other options to specify the environment.

有关ls()和?环境的更多信息,请参阅?ls以了解指定环境的其他选项。

#1


10  

  1. Why not use the pattern= argument to ls?
  2. 为什么不使用ls的pattern =参数?

  3. Calling ls inside a function lists the objects that exist within the function scope, not the global environment (this is explained in ?ls).
  4. 在函数内部调用ls会列出函数作用域中存在的对象,而不是全局环境(这将在?中解释)。

If you want to list the objects in the global environment from a function, specify envir=.GlobalEnv.

如果要从函数列出全局环境中的对象,请指定envir = .GlobalEnv。

x <- 1:10
f <- function() ls()
g <- function() ls(envir=.GlobalEnv)
h <- function() ls(envir=.GlobalEnv, pattern="[fg]")
f()
# character(0)
g()
# [1] "f" "g" "h" "x"
h()
# [1] "f" "g"

#2


7  

You need to tell your function to list objects in an environment other than itself, e.g. the global environment. (And while you're at it, you can also specify the regex pattern as an argument to ls.):

您需要告诉您的函数列出除自身以外的环境中的对象,例如全球环境。 (当你在它时,你也可以指定正则表达式作为ls的参数。):

find.test <- function(x, envir=.GlobalEnv) {
  ls(pattern=x, envir=envir) 
}

See ?ls for more info about ls() and ?environment for other options to specify the environment.

有关ls()和?环境的更多信息,请参阅?ls以了解指定环境的其他选项。