2

I was wondering if Julia has an easily already built in capability to pass arguments meant for a function in a function?

For example,

I'm working with Gadfly but I want to create a function that makes a specific plot, let's say one that does a line graph with the plots pointed already.

So for a working example

using Gadfly, Random
Random.seed!(50)
x = randn(10)
y = 10 * x .+ 2 .+ randn(10)/10
function dummy1(x, y; plotOptionsToPass...)
    plot(x = x, y = y, Geom.point, Geom.line; plotOptionsToPass...)
end

And I want to be able to pass in all different types of Gadfly plot options such as

dummy1(x, y; Theme(panel_fill = nothing))

so that it makes the dummy1 function turn into something like

plot(x = x, y = y, Geom.point, Geom.line; Theme(panel_fill = nothing))

without me having to actually prespecify all of the types of options Gadfly allows plot() to take.

NoviceStat
  • 115
  • 7

1 Answers1

1

Not sure what you are after, but maybe it helps to see that you can define a new function inside dummy1 and return it. The retruned fucntion will use less arguments. dummy1 becomes a drawing function 'constructor'.

function dummy1(;plotOptionsToPass...)
    function foo(x, y) 
        plot(x = x, y = y, Geom.point, Geom.line; plotOptionsToPass...)
    end 
    return foo
end 

# create new drawing function
new_artist = dummy1(Theme(panel_fill = nothing))
# draw something
new_artist(x, y) 
Evgeny
  • 4,173
  • 2
  • 19
  • 39
  • Hi @EPo, thanks for the answer. I tried using your syntax above and got the following error. `ERROR: MethodError: no method matching dummy1(::Theme) Closest candidates are: dummy1(; plotOptionsToPass...) at REPL[5]:2 Stacktrace: [1] top-level scope at none:0`. Maybe to clarify, I am just trying to create a function that makes a scatter plot with lines connecting the dots, but also lets me specify any other plot functions/arguments that `plot()` takes in Gadfly. – NoviceStat Dec 24 '18 at 07:55
  • 1
    Let me try the functionality on desktop and get back with an update. – Evgeny Dec 25 '18 at 21:26