查找具有特定参数的函数 [英] Find functions with specific arguments

查看:21
本文介绍了查找具有特定参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到所有具有特定参数的函数的名称和位置?有没有办法在全局环境、附加包、已安装包中找到函数?

How can you find the names and locations of all the functions that have a specific argument? Is there a way to find them for functions in the global environment, attached packages, and installed packages?

推荐答案

我假设你问这个问题只是为了不丢失 本好答案.在这里,我稍微修改了 Ben 的答案以搜索任何参数:

I assume that you ask the question just to not lose Ben great answer. Here I slightly modify Ben answer to search for any argument :

uses_arg <- function(x,arg) 
  is.function(fx <- get(x)) && 
  arg %in% names(formals(fx))

例如使用 na.rm 参数获取函数:

For example to get function with na.rm argument :

basevals <- ls(pos="package:base")      ## package name : here I use the base package
basevals[sapply(basevals,uses_arg,'na.rm')]

编辑

最好将 lsname 参数与 asNamespace 结合使用:

better to name argument of ls in conjunction with asNamespace :

basevals  <- ls(asNamespace('base'))
basevals[sapply(basevals,uses_arg,'na.rm')]

这篇关于查找具有特定参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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