I'm trying to modify the dots (...) inside a custom function. Here's a simplified example of my plot2
function, which displays a plot onscreen with type="p"
(the default) and saves an svg with type="l"
. The problem surfaces when one of the ...
plot options is already in the function. In this example, "type"
is matched by multiple actual arguments.
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")
I have tried to put the dots in a list
inside the function and modify it, but plot
does not accept a list as an input.
#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'