Created
September 6, 2018 21:41
-
-
Save mpettis/a780bed132a5b13d2ceceb30546d1700 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(jsonlite) | |
#> | |
#> Attaching package: 'jsonlite' | |
#> The following object is masked from 'package:purrr': | |
#> | |
#> flatten | |
# Ref: https://stackoverflow.com/questions/7944809/assigning-null-to-a-list-element-in-r | |
# JSON with 'null' keyword gets a null when cast to list object. | |
json_with_null <- '{"a": 1, "b": "B", "c": null}' | |
json_with_null %>% fromJSON() | |
#> $a | |
#> [1] 1 | |
#> | |
#> $b | |
#> [1] "B" | |
#> | |
#> $c | |
#> NULL | |
# This works as well | |
lstt <- list(a=1, b="B", c=NULL) | |
str(lstt) | |
#> List of 3 | |
#> $ a: num 1 | |
#> $ b: chr "B" | |
#> $ c: NULL | |
# ! Does not work, removes whole entry | |
lstt[["c"]] <- NULL | |
str(lstt) | |
#> List of 2 | |
#> $ a: num 1 | |
#> $ b: chr "B" | |
# This however does NOT QUITE work | |
lstt[["c"]] <- list(NULL) | |
str(lstt) | |
#> List of 3 | |
#> $ a: num 1 | |
#> $ b: chr "B" | |
#> $ c:List of 1 | |
#> ..$ : NULL | |
# This does... | |
lstt["c"] <- list(NULL) | |
str(lstt) | |
#> List of 3 | |
#> $ a: num 1 | |
#> $ b: chr "B" | |
#> $ c: NULL | |
# Created on 2018-09-06 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment