Behavior of @df inside a function

Consider the function and a call to this function. Notice the placement of @df on the first line of myPlot().
There is a spae before the @df macro. Everything runs fine. However, if I remove this space, then when compiling myPlot in Atom, I get the error that df_jump is not defined. I understand that the macro executes before the call to the function, so it is expanding on something that is not defined. But if this is true, why does adding a space before the @ symbol solve the problem? I do not understand. Thanks.

function run_plot(tot_pop)
  	prop_infected = 0.001
 	I0 = floor(Int, tot_pop * prop_infected)
 	S0 = tot_pop - I0
  	u0 = [S0,I0,0];
	prob = DiscreteProblem(u0,tspan)
	prob_jump = JumpProblem(prob,Direct(),sir_model)
	sol_jump = solve(prob_jump,SSAStepper());
	out_jump = sol_jump(t);
	df_jump = DataFrame(out_jump')
	df_jump[!,:t] = out_jump.t;
    myPlot(df_jump)
end

function myPlot(df_jump)
 @df df_jump plot(:t,
    [:x1 :x2 :x3],
    label=["S" "I" "R"],
    xlabel="Time",
    ylabel="Number")
end

time run_plot(10000)
1 Like

That’s extremely odd. A leading space in front of the @ should have no effect.

It’s hard to diagnose the real problem from the example that you’ve given because your code relies on a bunch of external functions, variables, and packages, and it’s not clear what exactly those are.

The first thing I would do is find a minimal, reproducible example that shows the same problem. I would start by removing all the code that sets up and solves the problem in run_plot and replacing it with a dummy DataFrame.

Gordon Erlebacher <Gerlebacher@fsu.edu>

1:18 PM (1 minute ago)


to JuliaLang

I will create a minimal example and see if this happens with my setup in general.
But I agree this should not happen. I thought a MWE would help, but I was hoping that something would jump out. Thanks.