简化没有for循环的嵌套列表

I have a list (imported from an API JSON) with a complex and nested structure that I need to tidy. I only manage to do it with for loops, which does not seem optimal : lengthy to write, long to compute with many elements and possible errors if new variations appear. Is there a way to obtain a similar results with tidyverse functions such as map(), unnnest(), flatten(), squash() or equivalents in base R? I haven't found any example in StackOverflow with similar combination of issues (pointed below the example). Here is the reproducible example :

metadata <- list(
  table1 = list(
    attribute1 = "tb1_att1",
    level_to_disard = list(
      attribute2 = "tb1_att2",
      columns = list(
        column1 = list(
          col_name = "tb1_col1_name",
          col_type = "tb1_col1_type"
        ),
        column2 = list(
          col_name = "tb1_col2_name",
          col_type = "tb1_col2_type"
        )
      ),
      tags = c("tag1", "tag2", "tag3"),
      irrelevant = list(irrelveant1 = "blabla", irrelevant2 = "blibli")
    )
  ),
  table2 = list(
    attribute1 = "tb2_att1",
    level_to_disard = list(
      columns = list(
        column1 = list(
          col_name = "tb2_col1_name",
          col_irrelevant = "bloblo"
        ),
        column2 = list(
          col_name = "tb2_col2_name",
          col_type = "tb2_col2_type"
        )
      ),
      tags = c("tag1", "tag3")
    )
  )
)

str(metadata)
# Output in console:
List of 2
 $ table1:List of 2
  ..$ attribute1     : chr "tb1_att1"
  ..$ level_to_disard:List of 4
  .. ..$ attribute2: chr "tb1_att2"
  .. ..$ columns   :List of 2
  .. .. ..$ column1:List of 2
  .. .. .. ..$ col_name: chr "tb1_col1_name"
  .. .. .. ..$ col_type: chr "tb1_col1_type"
  .. .. ..$ column2:List of 2
  .. .. .. ..$ col_name: chr "tb1_col2_name"
  .. .. .. ..$ col_type: chr "tb1_col1_type"
  .. ..$ tags      : chr [1:3] "tag1" "tag2" "tag3"
  .. ..$ irrelevant:List of 2
  .. .. ..$ irrelveant1: chr "blabla"
  .. .. ..$ irrelevant2: chr "blibli"
 $ table2:List of 2
  ..$ attribute1     : chr "tb2_att1"
  ..$ level_to_disard:List of 2
  .. ..$ columns:List of 2
  .. .. ..$ column1:List of 2
  .. .. .. ..$ col_name      : chr "tb2_col1_name"
  .. .. .. ..$ col_irrelevant: chr "bloblo"
  .. .. ..$ column2:List of 2
  .. .. .. ..$ col_name: chr "tb2_col2_name"
  .. .. .. ..$ col_type: chr "tb2_col1_type"
  .. ..$ tags   : chr [1:2] "tag1" "tag3"

请注意,属性位于不同的级别,某些元素(在列表中命名为“无关”)必须被丢弃,表2缺少“ attribute2”,表2的第1列缺少“ type”。 这是带有for循环和预期结果的解决方案。

# Define a function to extract column information
extract_cols <- function(x){
  fields <- tibble()
  if (length(x) == 0) {
    return(fields)
  } else {
    for (i in 1:length(x)) {
      fields <- add_row(fields)
      # Extract name
      fields$name[i] = ""
      # Extract type if present of return empty string
      if (any(names(x[[i]]) == "type")) {
        fields$type[i] = x[[i]][["type"]]
      } else {
        fields$type[i] = ""
      }
      return(fields)
    }
  }
}

# Create an empty tibble for the tidy metadata. It could also be a list.
library(tibble)
meta <- tibble()

# for (i in 1:1) {
 for (i in 1:length(metadata)) {
  meta <- add_row(meta)
  meta$attribute1[[i]] <- metadata[[i]][["attribute1"]]
  meta$attribute2[[i]] <- ifelse(length(metadata[[i]][["level_to_disard"]][["attribute2"]]) > 0,
                          c(metadata[[i]][["level_to_disard"]][["attribute2"]]), "")
    metadata[[i]][["level_to_disard"]][["attribute2"]]
  meta$cols[[i]] <- extract_cols(metadata[[i]][["columns"]])
  meta$tags[[i]] <- metadata[[i]][["level_to_disard"]][["tags"]]

}
str(meta)
# Output in console:
tibble [2 × 4] (S3: tbl_df/tbl/data.frame)
 $ attribute1: chr [1:2] "tb1_att1" "tb2_att1"
 $ attribute2: chr [1:2] "tb1_att2" ""
 $ cols      :List of 2
  ..$ : tibble [0 × 0] (S3: tbl_df/tbl/data.frame)
 Named list()
  ..$ : tibble [0 × 0] (S3: tbl_df/tbl/data.frame)
 Named list()
 $ tags      :List of 2
  ..$ : chr [1:3] "tag1" "tag2" "tag3"
  ..$ : chr [1:2] "tag1" "tag3"

Is there a more straightforward way to obtain this result? The output could be a list, a tibble or a dataframe, as long as it is simplified with a similar structure than 'meta'enter code here above.

评论