-1

I have a matrix mat of lat/lon coordinates that are accurate to the 12th decimal place. My objective is transform the matrix into a JSON that keeps those digits intact.

> head(mat)
          [,1]     [,2]
[1,] -122.6790 45.51459
[2,] -122.6789 45.51458
[3,] -122.6789 45.51457
[4,] -122.6789 45.51457
[5,] -122.6788 45.51456
[6,] -122.6788 45.51455

> print(head(mat), digits = 15)
                  [,1]             [,2]
[1,] -122.678966434126 45.5145902219176
[2,] -122.678937015334 45.5145823126032
[3,] -122.678907596543 45.5145744032888
[4,] -122.678878177752 45.5145664939744
[5,] -122.678848758960 45.5145585846600
[6,] -122.678819340169 45.5145506753456

However, simply using toJSON() saves only 4 digits.

> toJSON(mat)
[[-122.679,45.5146],[-122.6789,45.5146],[-122.6789,45.5146],[-122.6789,45.5146]...

How does one keep these significant digits when converting to JSON in R?

iskandarblue
  • 7,208
  • 15
  • 60
  • 130

1 Answers1

1
library(jsonlite)
toJSON(mat, digits = 12)
lampros
  • 581
  • 5
  • 12