我想使用R中的列表将参数传递给函数。我相信一般的共识是永远不要使用attach()
。但是,当目标是将列表传递给函数并将其作为函数环境中的控制参数附加时,这似乎很有意义。这类似于在control
中使用参数列表optim
的方式。
一个例子:
opts = list(N = 50, m = c(0,0,0), V = diag(3^2-0.1,3)+0.1)
a.fun = function(opts){
attach(opts)
# N = opts$N; m = opts$m; V = opts$V # isn't this just extra typing?
# mapply(function(a,b) assign(a,b),names(opts), opts)
X = MASS::mvrnorm(N, m, V)
if( all(m == 0 ) ) VX = 1/(N-1)*t(X) %*% X else VX = var(X)
return(VX)
}
a.fun(opts)
但是R抱怨对象被遮罩了。因此,显然没有本地搜索路径。
有更好的方法吗?我可以用Assign呼叫mapply
,但这似乎有点过分。如果我想传递嵌套的参数列表,它也会变得很复杂。
如果不想让所有参数都成为显式参数,在函数内分配参数的首选方法是什么?
谢谢
答案 0 :(得分:2)
opts = list(N = 50, m = c(0,0,0), V = diag(3^2-0.1,3)+0.1)
a.fun = function(N, m, V){
X = MASS::mvrnorm(N, m, V)
if( all(m == 0 ) ) VX = 1/(N-1)*t(X) %*% X else VX = var(X)
return(VX)
}
with( opts, a.fun(N, m, V))
# [,1] [,2] [,3]
# [1,] 9.6807152 2.540357 -0.1270927
# [2,] 2.5403570 10.095482 2.8155613
# [3,] -0.1270927 2.815561 9.4595351
答案 1 :(得分:1)
如果您确实需要简写的名称,请考虑使用within
,并避免重复使用opt$
限定词。 within
方法允许多个分配,例如 X 和 VX ,并返回对象作为列表的一部分:
library(MASS)
opts = list(N = 50, m = c(0,0,0), V = diag(3^2-0.1,3)+0.1)
set.seed(62218)
a.fun = function(opts)
within(opts,{
X <- MASS::mvrnorm(N, m, V)
if( all(m == 0 )) VX = 1/(N-1)*t(X) %*% X
else VX = var(X)
})
new_opts <- a.fun(opts)
输出
new_opts
$N
[1] 50
$m
[1] 0 0 0
$V
[,1] [,2] [,3]
[1,] 9.0 0.1 0.1
[2,] 0.1 9.0 0.1
[3,] 0.1 0.1 9.0
$VX
[,1] [,2] [,3]
[1,] 8.2858307 -1.427765 0.2446076
[2,] -1.4277652 5.961497 1.6060490
[3,] 0.2446076 1.606049 10.6135314
$X
[,1] [,2] [,3]
[1,] 4.665824224 -4.6765516 -2.4337000
[2,] -2.099468386 1.0783591 -1.8071952
[3,] 0.336563627 3.0536903 5.8581184
[4,] -1.650679259 1.8740465 4.0216621
[5,] 2.734859078 0.4038395 -1.6786622
[6,] 2.371694498 2.9171274 3.1788630
[7,] 2.114912549 0.4875905 -2.6360668
[8,] 0.388181150 -4.4120655 -3.0818534
[9,] 0.890230929 -1.6782046 -1.7543100
[10,] 5.516072440 -2.4788016 3.8561955
[11,] -0.294422855 -2.4483438 -0.3957462
[12,] -2.750255396 0.3527426 -7.8503780
[13,] 1.017828550 -0.5284536 -2.2027337
[14,] 0.024603249 -1.0532559 0.7042761
[15,] -4.250768981 2.5956117 -1.2632032
[16,] -0.669452033 2.7804910 2.0632924
[17,] 2.262865043 3.7968546 3.0055665
[18,] -1.432375413 1.6244284 -2.4446858
[19,] -3.926978090 -0.3174094 0.8649914
[20,] -0.649302263 0.9863867 -2.7072189
[21,] 7.076439594 -0.1097301 -2.0893510
[22,] -1.406789747 -1.1165661 -0.8801084
[23,] 2.353987384 0.2617199 -2.1814957
[24,] -1.147918893 -4.6585662 -3.6298189
[25,] -6.150519725 2.1693621 -5.2421818
[26,] -3.036748707 -2.8722558 -0.5655960
[27,] -1.969603593 1.5264114 -0.4650506
[28,] 1.980890284 -3.0119946 3.6450285
[29,] 1.581720927 -2.4479232 -3.1369536
[30,] -2.270400502 1.7667221 -3.0248372
[31,] -3.861221387 4.4131931 5.4808763
[32,] -2.448882331 0.2711234 -1.3864900
[33,] 1.680174711 -3.2132275 -4.7415458
[34,] -1.358019507 -1.2661220 -2.1774265
[35,] 1.228070476 -0.3154850 -2.3526299
[36,] -0.001047131 1.8794075 3.5546999
[37,] 5.529764024 -0.2454083 -0.1378435
[38,] -2.856147583 5.1200475 -0.2286176
[39,] -2.262467781 -0.8382753 9.0457395
[40,] -2.285365117 2.0572415 -2.7630034
[41,] 2.451295387 2.4030019 -0.5152660
[42,] -5.846573384 2.0942516 -0.5320590
[43,] 2.468044109 2.8170636 -0.8445475
[44,] 3.629899500 3.5186268 2.3032573
[45,] 2.127080073 1.8658157 -4.8428420
[46,] -0.727723102 0.1421247 1.0030250
[47,] -1.818292041 1.7646028 1.4841969
[48,] -0.271364961 -2.2127800 -3.7642276
[49,] 3.312789724 3.6894729 -3.5619642
[50,] -1.171523623 0.6163963 -2.5790328