<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/jsonlite-0-9-22-distinguish-between-double-and-integer/"></div></div><div><a data-url="http://www.r-bloggers.com/jsonlite-0-9-22-distinguish-between-double-and-integer/" data-counturl="http://www.r-bloggers.com/jsonlite-0-9-22-distinguish-between-double-and-integer/" data-count="horizontal" data-text="jsonlite 0.9.22: distinguish between double and integer" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.opencpu.org/posts/jsonlite-0-9-22/"> OpenCPU</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <a target="_blank" href="https://www.opencpu.org/posts/jsonlite-0-9-22"><img alt="opencpu logo" src="https://www.opencpu.org/images/mariokart.jpg"></a></p>
<p>Today a new version of the <a target="_blank" href="https://cran.r-project.org/web/packages/jsonlite/vignettes/json-aaquickstart.html">jsonlite</a> package was released to CRAN. This update includes a few internal enhancements and one new feature.</p>
<h2>Doubles vs integers</h2>
<p>The new <code>always_decimal</code> parameter forces formatting of doubles in decimal notation. That is to include at least one digit right of the decimal dot. This allows us to distingish them from integers, if you need this.</p>
<div>
<pre><code><span>x</span> <span>&lt;-</span> <span>1</span><span>:</span><span>5</span>
<span>y</span> <span>&lt;-</span> <span>as.numeric</span><span>(</span><span>x</span><span>)</span>
<span>(</span><span>json_x</span> <span>&lt;-</span> <span>jsonlite</span><span>::</span><span>toJSON</span><span>(</span><span>x</span><span>,</span> <span>always_decimal</span> <span>=</span> <span>TRUE</span><span>))</span>
<span># [1,2,3,4,5] </span>
<span>(</span><span>json_y</span> <span>&lt;-</span> <span>jsonlite</span><span>::</span><span>toJSON</span><span>(</span><span>y</span><span>,</span> <span>always_decimal</span> <span>=</span> <span>TRUE</span><span>))</span>
<span># [1.0,2.0,3.0,4.0,5.0] </span></code></pre>
</div>
<p>By formatting doubles this way they naturally get parsed back into doubles. So we can roundtrip numbers between R and json without losing type:</p>
<div>
<pre><code><span>identical</span><span>(</span><span>x</span><span>,</span> <span>jsonlite</span><span>::</span><span>fromJSON</span><span>(</span><span>json_x</span><span>))</span>
<span># TRUE
</span>
<span>identical</span><span>(</span><span>y</span><span>,</span> <span>jsonlite</span><span>::</span><span>fromJSON</span><span>(</span><span>json_y</span><span>))</span>
<span># TRUE
</span></code></pre>
</div>
<p>You should only use this if you really need it. The json format itself does not specify number types, hence there is no guarantee that an arbitrary json parser will distinguish between integers and doubles. Indeed, most json parsers might simply parse any number into a double, which is totally correct as well.</p>
<p>Also setting <code>always_decimal = TRUE</code> introduces some performance overhead.</p>
<h2>Numbers in MongoDB and Mongolite</h2>
<p>The main motivation for this feature was to insert data from R into MongoDB using the <a target="_blank" href="https://cran.r-project.org/web/packages/mongolite/vignettes/intro.html">mongolite</a> package. Several users of mongolite had <a target="_blank" href="https://github.com/jeroenooms/mongolite/issues/38">requested</a> that it would be nice to retain number types, especially when reading the data from MongoDB back into a strong typed language such as C++.</p>
<p>The latest version of <code>mongolite</code> automatically takes advantage of this feature:</p>
<div>
<pre><code><span># Get latest mongolite
</span><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;jeroenooms/mongolite&quot;</span><span>)</span>

<span># Assuming you have a local `mongod` running
</span><span>library</span><span>(</span><span>mongolite</span><span>)</span>
<span>df</span> <span>&lt;-</span> <span>data.frame</span><span>(</span><span>x</span> <span>=</span> <span>1</span><span>:</span><span>5</span><span>,</span> <span>y</span> <span>=</span> <span>as.numeric</span><span>(</span><span>1</span><span>:</span><span>5</span><span>))</span>
<span>m</span> <span>&lt;-</span> <span>mongo</span><span>(</span><span>&quot;testnum&quot;</span><span>)</span>
<span>m</span><span>$</span><span>insert</span><span>(</span><span>df</span><span>)</span>
<span>out</span> <span>&lt;-</span> <span>m</span><span>$</span><span>find</span><span>()</span>
<span>identical</span><span>(</span><span>out</span><span>,</span> <span>df</span><span>)</span>
<span># TRUE
</span></code></pre>
</div>
<p>This makes it even more seamless to use MongoDB as a backend for storing data frames in R!</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/jsonlite-0-9-22-distinguish-between-double-and-integer/"></div></div><div><a data-url="http://www.r-bloggers.com/jsonlite-0-9-22-distinguish-between-double-and-integer/" data-counturl="http://www.r-bloggers.com/jsonlite-0-9-22-distinguish-between-double-and-integer/" data-count="vertical" data-text="jsonlite 0.9.22: distinguish between double and integer" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.opencpu.org/posts/jsonlite-0-9-22/"> OpenCPU</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/HnhLrN9ZE5k"></a></div>