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

**URL:** https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673
**Category:** New to Julia
**Created:** [October 23, 2018, 9:53am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673 "2018-10-23T09:53:07Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [October 23, 2018, 9:53am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/1 "2018-10-23T09:53:07Z")

</div>

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?

```r
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.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 23, 2018, 9:57am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/2 "2018-10-23T09:57:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [October 23, 2018, 10:12am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/3 "2018-10-23T10:12:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 23, 2018, 10:43am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/4 "2018-10-23T10:43:22Z")

</div>

> [@DoktorMike](#):
>
> what would be the right way to compare complex output from a function against a known truth?

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).

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [October 23, 2018, 11:28am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/5 "2018-10-23T11:28:55Z")

</div>

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
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…

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 23, 2018, 11:38am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/6 "2018-10-23T11:38:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [October 23, 2018, 11:43am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/7 "2018-10-23T11:43:48Z")

</div>

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

```julia
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… 🙂 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?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 23, 2018, 11:52am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/8 "2018-10-23T11:52:10Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [October 23, 2018, 11:53am UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/9 "2018-10-23T11:53:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 23, 2018, 12:17pm UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/10 "2018-10-23T12:17:35Z")

</div>

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:

```julia
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]

```

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [October 23, 2018, 12:19pm UTC](https://discourse.julialang.org/t/how-to-achieve-the-equivalent-of-rs-dput-in-julia/16673/11 "2018-10-23T12:19:21Z")

</div>

`print` of course! That makes it a lot easier. 🙂 Awesome.
