在R中分析外部源代码 - 如何找到难以谷歌搜索的函数/运算符



试图从源代码中学习一些东西。我偶然发现了"%||%"在ggplot2rCharts中。显然,后者不导入前者,也不定义"%||%"作为函数。如何在 R 生态系统中搜索这些函数?OS X的聚光灯似乎不是最简单的解决方案。

任何人都可以解释它的作用并指出它的定义吗?

在这两种情况下,函数执行相同的操作。

这是来自rCharts

#' Set a default value for an object
#' 
#' This function sets the value of an object to a default value if it is not defined. 
#' @params x object
#' @params y object
#' @keywords internal
#' @noRd
`%||%` <- function(x, y){
  if (is.null(x)) y else x
}

这是来自"ggplot2" - 语法略有不同,但操作相同:

ggplot2:::`%||%`
# function (a, b) 
# {
#     if (!is.null(a)) 
#         a
#     else b
# }

要查找这些函数的定义,可以从尝试 getAnywhere() 开始。这是我系统上的结果:

getAnywhere("%||%")
# 3 differing objects matching ‘%||%’ were found
# in the following places
#   namespace:ggplot2
#   namespace:gtable
#   namespace:plyr
#   namespace:reshape2
#   namespace:scales
# Use [] to view one of them

编辑:请注意 [] 接受数字参数,而不是包名称

最新更新