newfound
newfound

Reputation: 41

How to execute Julia statement that was generated dynamically

In Julia 1.4.2 language, I have generated a statement dynamically. What command should I use to execute it?

Example:

import Pkg; 
Pkg.add("DataFrames"); 
using DataFrames

i=1;  
e="df_original$i  = DataFrame(a = Int[], b = String[])"  
#i.e., the statement is "df_original1  = DataFrame(a = Int[], b = String[])"

Julia_exec(e)

What is the equivalent of Julia_exec in Julia that can execute the above statement?

Thanks

Upvotes: 2

Views: 81

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

eval(Meta.parse(e))

For your example:

julia> eval(Meta.parse(e));

julia> df_original1
0×2 DataFrame

More information can be found in Julia metaprogramming tutorial https://docs.julialang.org/en/v1/manual/metaprogramming/

However, most tasks in Julia can be achieved without meta-programming and I strongly encourage you not to use it in normal workflows.

Upvotes: 4

Related Questions