rrjsonio

RJSONIO rounding error


I have a problem with the toJSON function in RJSONIO package in R. Values seem to be rounded when reading from a data frame.

I would like to understand whether this is a bug or something deeper within R.

Here is a reproducible example:

sal <- structure(list(a = c(1250.00, 1250.04, 1250.08, 1250.12, 1250.16, 1250.2, 1250.24), 
                      b = c(1, 1.06, 1.12, 1.18, 1.24, 1.3, 1.36)),
                 .Names = c("a", "b"), row.names = c(NA, 26L), class = "data.frame")

When I run toJSON function from RJSONIO:

library(RJSONIO)
RJSONIO::toJSON(sal)
[1] "{\n \"a\": [   1250,   1250, 1250.1, 1250.1, 1250.2, 1250.2, 1250.2 ],\n\"b\": [      1,   1.06,   1.12,   1.18,   1.24,    1.3,   1.36 ] \n}"

For comparison, I get this result using toJSON function from rjson:

library(rjson)
rjson::toJSON(sal)
[1] "{\"a\":[1250,1250.04,1250.08,1250.12,1250.16,1250.2,1250.24],\"b\":[1,1.06,1.12,1.18,1.24,1.3,1.36]}"

The RJSONIO package appears to be rounding the values. For example 1250.04 is rounded to 1250.

Here is my relevant sessionInfo()

R version 3.2.1 (2015-06-18)
Platform: i686-pc-linux-gnu (32-bit)
Running under: Ubuntu 14.10

other attached packages:
[1] rjson_0.2.15   RJSONIO_1.3-0

Can anyone please suggest why this is happening?


Solution

  • There's a digits parameters for that in RJSONIO:toJSON

    library(RJSONIO)
    toJSON(sal, digits = 6)
    [1] "{\n \"a\": [    1250, 1250.04, 1250.08, 1250.12, 1250.16,  1250.2, 1250.24 ],\n\"b\": [       1,    1.06,    1.12,    1.18,    1.24,     1.3,    1.36 ] \n}