How to provide reproducible Sample Data in Julia

Viewed 472

Here on stackoverflow.com - when I provide sample data to make a reproducible example, how can I do it the Julian way?

In R for example dput(df) will output a string with which you can create df again. Hence, you just post the result here on stackoverflow and bam! - reproducible example. So, how should one do it in Julia?

2 Answers

I think the easiest thing to do generally is to simply construct an MWE DataFrame with random numbers etc in your example, so there's no need to read/write out.

In situations where that's inconvenient, you might consider writing out to an IO buffer and taking the string representation of that, which people can then read back in the same way in reverse:

julia> using CSV, DataFrames

julia> df = DataFrame(a = rand(5), b = rand(1:10, 5));

julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> string_representation = String(take!(CSV.write(io, df)))
"a,b\n0.5613453808585873,9\n0.3308122459718885,6\n0.631520224612919,9\n0.3533712075535982,3\n0.35289980394398723,9\n"

julia> CSV.read(IOBuffer(string_representation))
5×2 DataFrame
│ Row │ a        │ b     │
│     │ Float64  │ Int64 │
├─────┼──────────┼───────┤
│ 1   │ 0.561345 │ 9     │
│ 2   │ 0.330812 │ 6     │
│ 3   │ 0.63152  │ 9     │
│ 4   │ 0.353371 │ 3     │
│ 5   │ 0.3529   │ 9     │

Here is one way to mimic the behaviour of R's dput in Julia:

julia> using DataFrames

julia> using Random; Random.seed!(0);

julia> df = DataFrame(a = rand(3), b = rand(1:10, 3))
3×2 DataFrame
 Row │ a          b
     │ Float64    Int64
─────┼──────────────────
   1 │ 0.405699       1
   2 │ 0.0685458      7
   3 │ 0.862141       2

julia> julian_dput(x) = invoke(show, Tuple{typeof(stdout), Any}, stdout, df);

julia> julian_dput(df)
DataFrame(AbstractVector[[0.4056994708920292, 0.06854582438651502, 0.8621408571954849], [1, 7, 2]], DataFrames.Index(Dict(:a => 1, :b => 2), [:a, :b]))

That is, julian_dput() takes a DataFrame as input and returns a string that can generate the input.

Source: https://discourse.julialang.org/t/given-an-object-return-julia-code-that-defines-the-object/80579/12

Related