How to achieve the equivalent of R's dput in Julia?

Most likely a silly question but I didn’t see it anywhere previously. How can I do the julia equivalent of the following R code?

a<-matrix(rnorm(1:9), 3, 3)
dput(a)

which outputs

structure(c(-2.48628561144488, 0.617375236852632, -0.931683210857559,
0.895162636414633, 0.492770097936146, 1.19068318899589, -0.216421068256692,
-0.369957626678885, -0.539652705102354), .Dim = c(3L, 3L))

Basically I want a julian canonical way of re-creating the output from a function in a controlled setting i.e. testing.

AFAIK dput prints code which can be used to “recreate” the object in R.

Julia has no equivalent functionality if you insist on a source code representation, but it has various ways of saving and recreating objects, including the Serialization standard library and the JLD2.jl package.

It would be nice to have, but if there isn’t any then what would be the right way to compare complex output from a function against a known truth? Saving the object in a file and loading it each time you need to compare against it? If so, where should that saved file reside in a package infrastructure? I’m using it in a unit test setting and the function currently outputs a 2 dimensional array.

Implementing the relevant comparison as a function. I often use for this in unit tests.

Generalized text-based representations are not a good solution in most cases; they can expose irrelevant internals (like your example above, .Dim is an implementation quirk, albeit standardized in R), and don’t generalize to approximate comparisons (which are common in numerical code).

1 Like

Thanks for the answer. Makes sense. However, the implementation of that function is what eludes me. To make matters more concrete this is what I want to establish to always be true in a unit test.

julia> grammify(collect(1:10))
10×10 Array{Float64,2}:
  1.0        0.777778    0.555556   …  -0.555556   -0.777778   -1.0     
  0.777778   0.209877   -0.0905188     -0.954716   -1.0        -0.777778
  0.555556  -0.0905188  -0.382716      -1.0        -0.954716   -0.555556
  0.333333  -0.333333   -0.598741      -0.969111   -0.851852   -0.333333
  0.111111  -0.538228   -0.764602      -0.888059   -0.711067   -0.111111
 -0.111111  -0.711067   -0.888059   …  -0.764602   -0.538228    0.111111
 -0.333333  -0.851852   -0.969111      -0.598741   -0.333333    0.333333
 -0.555556  -0.954716   -1.0           -0.382716   -0.0905188   0.555556
 -0.777778  -1.0        -0.954716      -0.0905188   0.209877    0.777778
 -1.0       -0.777778   -0.555556       0.555556    0.777778    1.0     

How does one go about doing that as a function? In jldocs format this is easily done, but using Test I’m not sure at all…

If you are comparing arrays of Float64, can’t you use use == or isapprox in unit tests?

Yes I think that’s appropriate in this case. My question is then what do I actually write on the right hand side of

grammify(collect(1:10) == ?

I would not like to write out the array by hand in the code since the real test should be 1:1000… :slight_smile: I guess this is where your Serialize suggestion is applicable? In that case where should that file be stored when doing package development? Inside the test folder?

So, if I understand correctly, you are comparing with a large, known matrix, and want to just load it from somewhere instead of using a literal representation in the code?

For that, I would use JLD2 at the moment.

Yes that’s what I want. I’ll give JLD2 a shot then. Thanks :slight_smile:

For tests, usually matrices are small enough that you can just output them with print and paste them into the test.

By default, print doesn’t show all of the digits for floating-point values, but you can print them all using an IOContext. For example:

print(IOContext(stdout, :compact=>false), rand(3,3))
[0.9063855083253369 0.9628981343254646 0.5753885850862417; 0.46498852170762306 0.5947921287653546 0.3407140607787198; 0.6484533038505804 0.22145148469946463 0.6114550810627906]
1 Like

print of course! That makes it a lot easier. :slight_smile: Awesome.