# Initializing a dataframe

**URL:** https://discourse.julialang.org/t/initializing-a-dataframe/35933
**Category:** New to Julia
**Created:** [March 13, 2020, 4:22pm UTC](https://discourse.julialang.org/t/initializing-a-dataframe/35933 "2020-03-13T16:22:30Z")
**Posts on this page:** 1
**Showing post:** 21

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [March 15, 2020, 12:11am UTC](https://discourse.julialang.org/t/initializing-a-dataframe/35933/21 "2020-03-15T00:11:32Z")

</div>

Here are some further experiments, getting closer to what I really want. At this point, it is no longer about my original problem, but about me being obsessed with getting a specific result.

```julia
struct A end
struct B end

function getCurrents(::A)
           random = rand()
           println("A")
           J_δ = [8, 6]
           J_β = [3,23]
           return (J_δ = J_δ, J_β = J_β, c = random)
end

function getCurrents(::B)
           dict = Dict()
           random = rand()
           dict[:J_δ] = [8, 6]
           dict[:J_β] = [3,25]
           return dict
end

a = A(); output_named_tuple = getCurrents(a)
for (k,v) in zip(keys(output_named_tuple), output_named_tuple)
  # eval evaluates in the global context
  eval(:($k = $v))
end

b = B(); dict = getCurrents(b)

for (k,v) in zip(keys(dict), dict)
  # eval evaluates in the global context
  eval(:($k = $v)) # Defines J_β, J_δ
end

```

My function getCurrents, either returns a NamedTuple or a Dictionary. When it returns a dictionary, J\_\alpha and J\_\beta only appear once. In the original solution, J\_\beta and J\_\delta appear each three times, which violates the DRY principle. Lower down, I have two loops, one over the NamedTuple, and one over the dictionary. In both cases, the variables J\_\beta and J\_\delta are instantiated in the global space (perhaps the wrong term in Julia) in the sense that in any function I could type something like

```julia
  z = J_\alpha + J_\beta

```

and I would get a result.

My real objective is to specify the variables I wish to track only ONCE, and have my callback collect the value of these variables once per time step of an ODE solver, and store the results in a DataFrame. Of course it is possible. Anything is possible: after all, that is what packages and modules are all about. Creating easy to use functionality that is not simple with the current structure of julia and packaging it for the user.

I am open to any suggestions you might have. This little excursion has taught me about NamedTuples, Structures, zip, loops of various kinds and the most basic form of metaprogramming.

One question: I wonder how efficient or non-efficient my approach is. Note that efficiency is not the point here, but feasibility.

When using DifferentialEquations.jl, if I must unpack a dictionary everything the right-hand side is invokedm there might be a penalty. Those are experiments I might drive myself to run.

Another issue: in the right-hand side routine, without the callback, J\_\beta and J\_\delta are variables local to the method. In the current approach, J\_\alpha and J\_\beta are defined in the global space, which is never a good idea. So one question to answer is whether it is possible to apply a macro to create a variable in a local context of some kind.

I have read the following three links: ’

> <https://stackoverflow.com/questions/21267962/how-can-i-eval-a-local-variable-in-julia>

> <https://stackoverflow.com/questions/26071317/declaring-top-level-variables-in-julia-using-metaprogramming/26071597#26071597>

> <https://stackoverflow.com/questions/35051773/create-local-variables-programmatically-from-a-dictionary-in-julia>

> [@Listing the names of local variables](https://discourse.julialang.org/t/listing-the-names-of-local-variables/9776/19):
>
> FWIW, this kind of functionality is often easier to implement than it looks. With [MacroTools](https://github.com/MikeInnes/MacroTools.jl#expression-walking): using MacroTools: prewalk using MacroTools # Your example: expr = quote function readfile(filename::String) a = Dict{String,Int}() buf = "" open(filename,"r") do ios while(!eof(ios)) bud = strip(readline(ios)) items = split(buf,"") a[items[1]] = length(items) end end return a end end # Gather all local variables vars = [] prewalk(expr) do x …

Thank you,

Gordon

---

_[View the full topic](https://discourse.julialang.org/t/initializing-a-dataframe/35933)._
