【问题标题】:Identify all functions with an empty argument list用空参数列表识别所有函数
【发布时间】:2013-02-14 21:27:00
【问题描述】:

如何找到具有给定形式参数签名的所有函数(在当前工作环境中可用,所以base 加上加载的包)?

这是因为我试图找到一个例子来回答这个问题:If function(x) can work, why would we need function()?

【问题讨论】:

    标签: r


    【解决方案1】:

    所需件数:

    • 所有函数的字符向量:apropos("^",mode="function")
    • 函数没有参数吗? length(formals(x))==0
    • 抓取由字符向量lapply( x, get )命名的对象

    感谢@RichieCotton 提供apropos,感谢@hadley 提供is.primitive

    allFxnNames <- apropos("^",mode="function")
    emptyFormals <- function(x) length(formals(x))==0 && !is.primitive(x)
    allFxns <- lapply( allFxnNames, get )
    whichEmpty <- sapply(allFxns, emptyFormals)
    allFxnNames[whichEmpty]
    

    这会找到所有没有正式参数的非原始函数:

    “警报”、“closeAllConnections”、“颜色”、“颜色”、“贡献者”、 “Cstack_info”、“日期”、“defaultPrototype”、“default.stringsAsFactors”、 “dev.cur”、“dev.list”、“dev_packages”、“element_blank”、“empty.dump”、 “.First”、“.First.sys”、“flush.console”、“frame”、“getAllConnections”、 “getCConverterDescriptions”、“getCConverterStatus”、“geterrmessage”、 “get_ll_TOL”、“get_ll_warn”、“getLoadedDLLs”、“getNumCConverters”、 “get_path”、“get_Polypath”、“get_PolypathRule”、“get_ReplCRS_warn”、 “getRversion”、“getTaskCallbackNames”、“getwd”、“graphics.off”、 “has_devel”、“iconvlist”、“is_false”、“is.R”、“is_true”、“Java”、 “l10n_info”、“last_plot”、“licence”、“license”、“loadedNamespaces”、 “loaded_pa​​ckages”、“loadingNamespaceInfo”、“memory.profile”、 "newEmptyObject", ".NotYetImplemented", ".OptRequireMethods", “plot.new”、“progress_none”、“rc.status”、“Rdoc”、“recordPlot”、 “恢复”、“.rs.activateGraphicsDevice”、“.rs.createUUID”、 ".rs.defaultLibPathIsWriteable", ".rs.defaultUserLibraryPath", “.rs.disableQuartz”、“.rs.helprIsActive”、“.rs.iconvcommon”、 ".rs.initGraphicsDevice", ".rs.knitrChunkOptions", ".rs.listJsonRpcHandlers", ".rs.packages.initialize", ".rs.rpc.check_for_package_updates", ".rs.rpc.get_cran_mirrors", ".rs.rpc.get_package_install_context", ".rs.rpc.iconvlist", ".rs.rpc.init_default_user_library", ".rs.rpc.list_objects", ".rs.rpc.list_packages", ".rs.rpc.remove_all_objects", “.rs.setHelprLoadHook”、“.rs.sweaveChunkOptions”、“RStudioGD”、 "RStudio.version", ".rs.uniqueLibraryPaths", “.rs.updatePackageEvents”、“.rs.writeableLibraryPaths”、“Rtangle”、 “R.Version”、“RweaveLatex”、“搜索”、“搜索路径”、“会话数据”、 “.standard_regexps”、“stderr”、“stdin”、“stdout”、“sys.calls”、 “Sys.Date”、“sys.frames”、“Sys.getpid”、“Sys.info”、“Sys.localeconv”、 “sys.nframe”、“sys.on.exit”、“sys.parents”、“sys.status”、“系统”、 “Sys.time”、“Sys.timezone”、“tempdir”、“test”、“theme_get”、“waiver”

    【讨论】:

    • 查看?is.primitive并在github.com/hadley/devtools/wiki/functions上搜索“原始”
    • 另外,你可以用Filter(emptyFormals, allFxns)替换最后两行
    • 最后,你可能还想看看github.com/hadley/pryr/blob/master/R/find-funs.r——它解决的不是同一个问题,但它是相似的。
    • @hadley 像往常一样最有帮助。不过,Filter 并没有完全按照我的要求做,因为它返回的是对象而不是它们的名称。
    • 哦,对不起,我看错了你的代码。我有` where allFxnNames[where(allFxns, emptyFormals)]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多