3

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

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

4

1 回答 1

5

在这两种情况下,函数都做同样的事情。

这是来自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

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

于 2013-09-14T10:27:18.140 回答