Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fromJSON: string "NA" replaced by NA #402

Open
choogland opened this issue Sep 13, 2022 · 1 comment
Open

fromJSON: string "NA" replaced by NA #402

choogland opened this issue Sep 13, 2022 · 1 comment

Comments

@choogland
Copy link

Hello,

I have embedded data.frame into lists. See example:

tests <- list(DOB_NA_E1_C1 = list(v.1.0 = structure(list(version = "v.1.0", 
    keys = "98", values = "NA"), row.names = 1L, class = "data.frame")), 
    BLD_REQUIRED_E5_C8 = list(v.1.0 = structure(list(version = c("v.1.0", 
    "v.1.0", "v.1.0", "v.1.0", "v.1.0"), keys = c("1", "2", "3", 
    "4", "9"), values = c("Any amount", "1 µL", "5 µL", "10 µL", 
    "Do not know")), row.names = c(NA, 5L), class = "data.frame")))

> str(tests)
List of 2
 $ DOB_NA_E1_C1      :List of 1
  ..$ v.1.0:'data.frame':	1 obs. of  3 variables:
  .. ..$ version: chr "v.1.0"
  .. ..$ keys   : chr "98"
  .. ..$ values : chr "NA"
 $ BLD_REQUIRED_E5_C8:List of 1
  ..$ v.1.0:'data.frame':	5 obs. of  3 variables:
  .. ..$ version: chr [1:5] "v.1.0" "v.1.0" "v.1.0" "v.1.0" ...
  .. ..$ keys   : chr [1:5] "1" "2" "3" "4" ...
  .. ..$ values : chr [1:5] "Any amount" "1 µL" "5 µL" "10 µL" ...

toJSON(tests) is OK, "NA" is stored as expected (string)

> toJSON(tests)
{"DOB_NA_E1_C1":{"v.1.0":[{"version":"v.1.0","keys":"98","values":"NA"}]},"BLD_REQUIRED_E5_C8":{"v.1.0":[{"version":"v.1.0","keys":"1","values":"Any amount"},{"version":"v.1.0","keys":"2","values":"1 μL"},{"version":"v.1.0","keys":"3","values":"5 μL"},{"version":"v.1.0","keys":"4","values":"10 μL"},{"version":"v.1.0","keys":"9","values":"Do not know"}]}}

but back to R with fromJSON will replace "NA" by NA which is not what is expected:

> fromJSON(toJSON(tests))
$DOB_NA_E1_C1
$DOB_NA_E1_C1$v.1.0
  version keys values
1   v.1.0   98     NA


$BLD_REQUIRED_E5_C8
$BLD_REQUIRED_E5_C8$v.1.0
  version keys      values
1   v.1.0    1  Any amount
2   v.1.0    2        1 µL
3   v.1.0    3        5 µL
4   v.1.0    4       10 µL
5   v.1.0    9 Do not know

By using simplify "NA" is kept but not the structure:

> fromJSON(toJSON(tests), simplifyVector = F)
$DOB_NA_E1_C1
$DOB_NA_E1_C1$v.1.0
$DOB_NA_E1_C1$v.1.0[[1]]
$DOB_NA_E1_C1$v.1.0[[1]]$version
[1] "v.1.0"

$DOB_NA_E1_C1$v.1.0[[1]]$keys
[1] "98"

$DOB_NA_E1_C1$v.1.0[[1]]$values
[1] "NA"




$BLD_REQUIRED_E5_C8
$BLD_REQUIRED_E5_C8$v.1.0
$BLD_REQUIRED_E5_C8$v.1.0[[1]]
$BLD_REQUIRED_E5_C8$v.1.0[[1]]$version
[1] "v.1.0"

$BLD_REQUIRED_E5_C8$v.1.0[[1]]$keys
[1] "1"

Any suggestions to keep the string "NA" and the structure?
Thanks

@medeirosramos
Copy link

one solution is create a param like nullValue, for exemple:
fromJSON(json, nullValue = NA)
fromJSON(json, nullValue = "NA")
or
fromJSON(json, nullValue = "0")

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants