<p>For <code>dataframe</code> objects you can use <code>HTTP GET</code> and set the <code>dataframe</code> argument:</p>

<pre><code>GET http://localhost:7177/ocpu/tmp/x000a0fb8/json?dataframe=rows
</code></pre>

<p>For example the <code>Boston</code> object from the <code>MASS</code> package is a dataframe as well:</p>

<pre><code>https://cran.ocpu.io/MASS/data/Boston/json?dataframe=columns
https://cran.ocpu.io/MASS/data/Boston/json?dataframe=rows
</code></pre>

<p>For <code>HTTP GET</code> requests to a <code>.../json</code> endpoint, all the http parameters are mapped to arguments in the <code>toJSON</code> function from the <a href="http://cran.r-project.org/package=jsonlite" rel="nofollow">jsonlite package</a>. You can can also specify other <code>toJSON</code> arguments:</p>

<pre><code>https://cran.ocpu.io/MASS/data/Boston/json?dataframe=columns&amp;digits=4
</code></pre>

<p>To see which arguments are available, have a look at the <a href="http://cran.r-project.org/web/packages/jsonlite/jsonlite.pdf" rel="nofollow">jsonlite manual</a> or <a href="https://www.opencpu.org/posts/publishing-data-with-opencpu/" rel="nofollow">this post</a>.</p>

<p>Note that this only works if you do the 2 step procedure: first a <code>HTTP POST</code> on a function that returns a <code>dataframe</code>, followed by retrieving that object in <code>json</code> format with a <code>HTTP GET</code> request. You can not specify <code>toJSON</code> parameters when you do the 1-step shortcut where you fix the <code>POST</code> request with <code>/json</code>, because in <code>POST</code> requests the HTTP parameters always get mapped to the function call.</p>

<p>The reason for this default is that the row based design seems to be the most conventional and interoperable way of encoding tabular data. The <a href="http://arxiv.org/abs/1403.2805" rel="nofollow">jsonlite paper/vignette</a> goes into some more detail. Note that it also works the other way around: you don't have to call the <code>data.frame</code> function to create a dataframe, just posting an argument in the form: </p>

<pre><code>[{"xx":1,"yy":6},{"xx":2,"yy":7},{"xx":3,"yy":8}]
</code></pre>

<p>will automatically turn it into a data frame:</p>

<pre><code>curl https://public.opencpu.org/ocpu/library/base/R/summary/console -d object='[{"xx":1,"yy":6},{"xx":2,"yy":7},{"xx":3,"yy":8}]'
</code></pre>
OpenCPU and jsonlite: column-based "/json" versus row-based
<p>Is there a clean way to change the default "/json" postfix option on data.frames to be column-based versus row-based?</p>

<p>Data.frames in R, if I understand correctly, are really just named lists where each list is the same length as the others. Using <code>jsonlite</code>, it's simple to show the difference (trivial example, yes):</p>

<pre><code>library(jsonlite)
ll &lt;- list(xx=1:3, yy=6:8)
dd &lt;- data.frame(xx=1:3, yy=6:8)
toJSON(dd)
# [1] "[ { \"xx\" : 1, \"yy\" : 6 }, { \"xx\" : 2, \"yy\" : 7 }, { \"xx\" : 3, \"yy\" : 8 } ]"
toJSON(ll)
# [1] "{ \"xx\" : [ 1, 2, 3 ], \"yy\" : [ 6, 7, 8 ] }"
toJSON(dd, dataframe='column')
# [1] "{ \"xx\" : [ 1, 2, 3 ], \"yy\" : [ 6, 7, 8 ] }"
toJSON(as.list(dd))
# [1] "{ \"xx\" : [ 1, 2, 3 ], \"yy\" : [ 6, 7, 8 ] }"
</code></pre>

<p>where the last three are identical. It's easy to force it to look the same either by using the <code>dataframe</code> argument to <code>toJSON</code> or by coercing the <code>data.frame</code> into a <code>list</code>.</p>

<p>Using OpenCPU's API, the calls look similar:</p>

<pre><code>$ curl http://localhost:7177/ocpu/library/base/R/list/json -H "Content-Type: application/json" -d '{ "xx":[1,2,3], "yy":[6,7,8] }'
{
        "xx" : [
                1,
                2,
                3
        ],
        "yy" : [
                6,
                7,
                8
        ]
}

$ curl http://localhost:7177/ocpu/library/base/R/data.frame/json -H "Content-Type: application/json" -d '{ "xx":[1,2,3], "yy":[6,7,8] }'
[
        {
                "xx" : 1,
                "yy" : 6
        },
        {
                "xx" : 2,
                "yy" : 7
        },
        {
                "xx" : 3,
                "yy" : 8
        }
]
</code></pre>

<p>If I want the <code>data.frame</code> itself to be JSON-ified column-based then I need to coerce it to a <code>list</code>:</p>

<pre><code>$ curl http://localhost:7177/ocpu/library/base/R/data.frame -H "Content-Type: application/json" -d '{ "xx":[1,2,3], "yy":[6,7,8] }'
/ocpu/tmp/x000a0fb8/R/.val
/ocpu/tmp/x000a0fb8/stdout
/ocpu/tmp/x000a0fb8/source
/ocpu/tmp/x000a0fb8/console
/ocpu/tmp/x000a0fb8/info

$ curl http://localhost:7177/ocpu/library/base/R/as.list/json -d "x=x000a0fb8"
{
        "xx" : [
                1,
                2,
                3
        ],
        "yy" : [
                6,
                7,
                8
        ]
}
</code></pre>

<p>Three questions:</p>

<ol>
<li><p>Is there a way to change the default behavior of the OpenCPU auto-JSON-ification to be column-based?</p></li>
<li><p>Is there a reason (besides "had to default to something") that it defaults to row-based? (So that I can better understand the underpinnings and efficiencies, not meant as a challenge.)</p></li>
<li><p>This is all academic, though, since most (if not all) libraries accepting the JSON output will understand and translate between the formats transparently. Right?</p></li>
</ol>

<p>(Win7 x64, R 3.0.3, opencpu 1.2.3, jsonlite 0.9.4)</p>

<p>(PS: Thanks, Jeroen, OpenCPU is awesome! The more I play, the more I like.)</p>