I am experiencing a bizarre issue. A few senior R devs have worked on this for hours with no discernible solution and need an R expert to help!
A dataframe that was created in R output from models contains 0 values that yield a -0
when translated to json via jsonlite::toJSON
.
I have distilled this to its most basic form:
> df
# A tibble: 1 × 1
value
<dbl>
1 0
> df |> jsonlite::toJSON()
[{"value":-0}]
Is it actually 0?
> df[[1]]
[1] 0
> df[[1]] == 0.0
[1] TRUE
> identical(df[[1]], 0.0000000)
[1] TRUE
Let's dput it!
> df |> dput()
structure(list(value = 0), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"))
Does the dput also yield a -0
? No!
structure(list(value = 0), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")) |> jsonlite::toJSON()
[{"value":0}]
So I can't reprex with dput. It does work to reprex with an RDS:
> df |> saveRDS('value.rds')
> readRDS('value.rds') |> jsonlite::toJSON()
[{"value":-0}]
How did I end up with this corrupt type of 0
that causes this issue in jsonlite? I can't attach the RDS to this forum, it doesn't accept RDS. Is there anything I can do to further debug?
> df$value[[1]] |> jsonlite::toJSON()
[-0]
> df$value[[1]] |> class()
[1] "numeric"