我正在尝试在自定义函数中修改点(...)。这是我plot2
函数的简化示例,该函数在屏幕上显示type="p"
(带有默认值)的绘图,并使用保存一个svg type="l"
。当其中一个...
绘图选项已存在于函数中时,问题浮出水面。在此示例中,"type"
由多个实际参数匹配。
plot2 <-function(...){
plot(...) #visible on screen
svg("c:/temp/out.svg") #saved to file
plot(...,type="l")
dev.off()
}
#This works
plot2(1:10)
#This does not work because type is redefined
plot2(1:10, type="o")
我试图将点list
放在函数内部并对其进行修改,但plot
不接受列表作为输入。
#Does not work
plot2 <-function(...){
plot(...)
dots <<-list(...)
print(dots)
if("type" %in% names(dots)) dots$type="l"
print(dots)
svg("c:/temp/out.svg")
plot(dots)
dev.off()
}
plot2(1:10, type="o")
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' is a list, but does not have components 'x' and 'y'
如果您要转发的修订版...
,则需要做两件事:
do.call
。其工作方式如下:
plot2 = function (...) {
# capture:
dots = list(...)
# modify:
dots$type = 'l'
# forward call:
do.call(plot, dots)
}
一般而言,do.call(f, list(‹…›))
相当于f(‹…›)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句