Mat*_*ert 3 open-source r ggplot2 rcharts
试图从源代码中学习一些东西.我偶然发现了"%||%" ggplot2,以及在rCharts.显然,后者不会导入前者,也不会将"%||%"定义为函数.如何在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
}
Run Code Online (Sandbox Code Playgroud)
这是来自"ggplot2" - 语法略有不同但操作相同:
ggplot2:::`%||%`
# function (a, b)
# {
# if (!is.null(a))
# a
# else b
# }
Run Code Online (Sandbox Code Playgroud)
要查找这些函数的定义,您可以先尝试一下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
Run Code Online (Sandbox Code Playgroud)
编辑:请注意[]接受数字参数,而不是包名称