niko
niko

Reputation: 5281

Relist a list containing data frames

I have nested lists that I would like to to edit. To do so in a convenient way, I wanted to use unlist and relist. The problem is relist seems not the respect the underlying structure:

# Some list
my_list <- list(vec = 'string',
                df  = data.frame(X=0,Y=0,Z=0))
str(my_list)
# List of 2
# $ vec: chr "string"
# $ df :'data.frame':   1 obs. of  3 variables:
#   ..$ X: num 0
# ..$ Y: num 0
# ..$ Z: num 0

# Unlist to modify nested content
my_unlist <- unlist(my_list)
str(my_unlist)
# Named chr [1:4] "string" "0" "0" "0"
# - attr(*, "names")= chr [1:4] "vec" "df.X" "df.Y" "df.Z"

# Write some data
my_unlist[c("df.X","df.Y","df.Z")] <- c(0,1,1)
str(my_unlist)
# Named chr [1:4] "string" "0" "1" "1"
# - attr(*, "names")= chr [1:4] "vec" "df.X" "df.Y" "df.Z"

# Relist
my_relist <- relist(flesh = my_unlist, skeleton = my_list)
str(my_relist)
# List of 2
# $ vec: chr "string"
# $ df : Named chr [1:3] "0" "1" "1"
# ..- attr(*, "names")= chr [1:3] "X" "Y" "Z"

As you can see the list element df if not a data frame anymore when relisting.

Here is a manual work-around

# A manual work-around
my_relist$df <- setNames(data.frame(t(as.numeric(my_relist$df)), stringsAsFactors = FALSE), 
                         names(my_relist$df))
str(my_relist)
# List of 2
# $ vec: chr "string"
# $ df :'data.frame':   1 obs. of  3 variables:
#   ..$ X: num 0
# ..$ Y: num 1
# ..$ Z: num 1

but it doesn't help much, as I would like to perform this arbitrarily.

Ideally, I'd have a function that that fills my_list with the content of my_unlist while preserving the structure. So the expected output would be

my_relist
# $vec
# [1] "string"
# 
# $df
#   X Y Z
# 1 0 1 1

Edit

Here is some sample data along with a somewhat more detailed example

# Sample data: a simple json
test_json <- '{
  "email": "string",
"locations": [
{
  "address": {
  "city": "string",
  "country": "string",
  "houseNr": "string",
  "state": "string",
  "streetName": "string",
  "zipCode": "string"
  },
  "latitude": 0,
  "longitude": 0,
  "name": "string"
}
],
"phone": "string"
}'

# Reading the json sample
json <- jsonlite::fromJSON(test_json)

# Now we unlist json so that we can easily modify nested elements
json_unlist <- unlist(json)
# Let's add a few address components
fields_to_modify <- c("locations.address.houseNr","locations.address.streetName","locations.address.city","locations.address.country")
json_unlist[fields_to_modify] <- c("1", "Boulevard de Londres","Casablanca","Morocco")
# Now we want to convert it back to json
# First step: we must relist
attempt <- relist(flesh = json_unlist, skeleton = json)
attempt$locations
#      address               latitude              longitude                   name 
# "Casablanca"              "Morocco"                    "1"               "string" 
#                     <NA>                   <NA>                   <NA>                   <NA> 
#   "Boulevard de Londres"               "string"                    "0"                    "0" 
#       <NA> 
#   "string" 

Upvotes: 0

Views: 178

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269451

Using json from the question, note that since, for example, the following have the same meaning:

json[["locations"]][["address"]][["city"]]
json[[c("locations", "address", "city")]]

and assuming the use of dot in the naming of the unlist objects as shown in the question we can use Map like this:

setList <- function(List, Unlist) {
  nms <- names(Unlist)
  Map(function(x, y) List[[x]] <<- Unlist[[y]], strsplit(nms, "\\."), nms)
  List
}

setList(my_list, my_unlist)
setList(json, json_unlist)

Upvotes: 2

Related Questions