Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to R's dput in Julia

Tags:

julia

Is there a way to convert an object in Julia to a code representation generating the same object? I am basically looking for an equivalent to R's dput function.

So if I have an object like:

A = rand(2,2)
# Which outputs
>2×2 Array{Float64,2}:
 0.0462887  0.365109
 0.698356   0.302478

I can do something like dput(A) which prints something like the following to the console that can be copy-pasted to be able to replicate the object:

[0.0462887  0.365109; 0.698356   0.302478]
like image 826
Stuart Avatar asked Mar 05 '23 20:03

Stuart


2 Answers

I think you are looking for repr:

julia> A = rand(2, 2);

julia> repr(A)
"[0.427705 0.0971806; 0.395074 0.168961]"
like image 144
fredrikekre Avatar answered Mar 11 '23 18:03

fredrikekre


Just use Base.dump.

julia> dump(rand(2,2))
Array{Float64}((2, 2)) [0.162861 0.434463; 0.0823066 0.519742] 

You can copy the second part.

like image 24
wul Avatar answered Mar 11 '23 16:03

wul