R可以识别用作函数参数的分布类型吗?

时间:2017-05-25 19:10:03

标签: r function random

背景

我有一个名为TBT的简单函数。此函数有一个名为x的参数。用户可以提供R中存在的任何类型rdistribution_name()(例如,rnorm()rf()rt()rbinom()等)以用于参数x除了一个:rcauchy()”。

问题

我想知道R如何识别用户提供rcauchy()作为x的输入,在这种情况下,R会发出警告消息?

这是我的R代码没有成功:

TBT = function(x) {

if( x == rcauchy(...) ) { warning("\n\tThis type of distribution is not supported.") }

}

TBT( x = rcauchy(1e4) )

Error in TBT(rcauchy(10000)) : '...' used in an incorrect context

1 个答案:

答案 0 :(得分:1)

如果您正在预测它们,当他们调用您的函数时,请调用随机函数,您可以这样做

TBT <- function(x) {
    xcall <- match.call()$x
    if (class(xcall)=="call" && xcall[[1]]=="rcauchy") { 
        warning("\n\tThis type of distribution is not supported.") 
    }
}
TBT( x = rcauchy(1e4) )

但这不会发现像

这样的情况
x <- rcauchy(1e4)
TBT( x )

R无法跟踪x变量中的数据来自

相关问题