Inserting characters in a nested list from a JSON request

145 views Asked by At

I have the following list mylist in R I am getting back from the foursquare api which I'm trying to modify before running the function jsonlite::rbind.pages() because there is a nested dataframe which has 0 input values returned. When I try and run the function rbindpages it returns 3 results as opposed to 4 eliminating the dataframe with 0 values. How would I go about inserting values into this so that when I utilize rbind.pages() i receive all 4 results? The purpose is to use cbind() afterwards to merge with other vectors.

here is the ouput from the console:

[[1]]
                        id  name pluralName shortName                                          icon.prefix icon.suffix primary
1 4bf58dd8d48988d1fa931735 Hotel     Hotels     Hotel https://ss3.4sqi.net/img/categories_v2/travel/hotel_        .png    TRUE

[[2]]
                        id     name pluralName shortName                                            icon.prefix icon.suffix primary
1 4bf58dd8d48988d10f951735 Pharmacy Pharmacies  Pharmacy https://ss3.4sqi.net/img/categories_v2/shops/pharmacy_        .png    TRUE

[[3]]
data frame with 0 columns and 0 rows

[[4]]
                        id            name       pluralName       shortName                                              icon.prefix
1 52e81612bcbc57f1066b7a32 Cultural Center Cultural Centers Cultural Center https://ss3.4sqi.net/img/categories_v2/building/default_
  icon.suffix primary
1        .png    TRUE

here is the dput:

mylist = list(structure(list(id = "4bf58dd8d48988d1fa931735", name = "Hotel", 
                    pluralName = "Hotels", shortName = "Hotel", icon = structure(list(
                      prefix = "https://ss3.4sqi.net/img/categories_v2/travel/hotel_", 
                      suffix = ".png"), .Names = c("prefix", "suffix"), class = "data.frame", row.names = 1L), 
                    primary = TRUE), .Names = c("id", "name", "pluralName", "shortName", 
                                                "icon", "primary"), class = "data.frame", row.names = 1L), structure(list(
                                                  id = "4bf58dd8d48988d10f951735", name = "Pharmacy", pluralName = "Pharmacies", 
                                                  shortName = "Pharmacy", icon = structure(list(prefix = "https://ss3.4sqi.net/img/categories_v2/shops/pharmacy_", 
                                                                                                suffix = ".png"), .Names = c("prefix", "suffix"), class = "data.frame", row.names = 1L), 
                                                  primary = TRUE), .Names = c("id", "name", "pluralName", "shortName", 
                                                                              "icon", "primary"), class = "data.frame", row.names = 1L), structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
     structure(list(id = "52e81612bcbc57f1066b7a32", name = "Cultural Center", 
                    pluralName = "Cultural Centers", shortName = "Cultural Center", 
                    icon = structure(list(prefix = "https://ss3.4sqi.net/img/categories_v2/building/default_", 
                                          suffix = ".png"), .Names = c("prefix", "suffix"), class = "data.frame", row.names = 1L), 
                    primary = TRUE), .Names = c("id", "name", "pluralName", 
                                                "shortName", "icon", "primary"), class = "data.frame", row.names = 1L), 
     structure(list(id = "4bf58dd8d48988d1e0931735", name = "Coffee Shop", 
                    pluralName = "Coffee Shops", shortName = "Coffee Shop", 
                    icon = structure(list(prefix = "https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_", 
                                          suffix = ".png"), .Names = c("prefix", "suffix"), class = "data.frame", row.names = 1L), 
                    primary = TRUE), .Names = c("id", "name", "pluralName", 
                                                "shortName", "icon", "primary"), class = "data.frame", row.names = 1L))

I noticed when hunting for a conditional filter, running nchar(mylist[3]) I get '6' so my thought was to run something like the following to pinpoint the "0 data frame" and then insert characters. Just a concept though so any help is appreciated!

 mylist = lapply(mylist, function (x) {
       if (nchar(x) == 6) {

        x== data.frame(
           list(id = "4bf58dd8d48988d1e0931735", name = "Not avaliable", 
                pluralName = "Not avaliable", shortName = "Not avaliable", 
                icon = "https://ss3.4sqi.net/img/categories_v2/food", prefix = "https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_", 
                suffix = ".png")
         )
       } else {
         x
       }
     })

EDIT************** using the suggestion from the comment I got the following:

[[11]]
[[11]][[1]]
                        id  name pluralName shortName                                          icon.prefix icon.suffix primary
1 4bf58dd8d48988d1fa931735 Hotel     Hotels     Hotel https://ss3.4sqi.net/img/categories_v2/travel/hotel_        .png    TRUE


[[12]]
NULL

[[13]]
[[13]][[1]]
                        id        name   pluralName   shortName                                              icon.prefix icon.suffix
1 52e81612bcbc57f1066b7a33 Social Club Social Clubs Social Club https://ss3.4sqi.net/img/categories_v2/building/default_        .png
  primary
1    TRUE

I ran jsonlite::rbind.pages() and got Error: all(vapply(pages, is.data.frame, logical(1))) is not TRUE

1

There are 1 answers

0
LoF10 On

Going off of what Conway suggested, made a few tweaks to his answer and it worked:

  mylist = lapply(mylist, function (x) { 
    if (length(x) == 0) { 
      x<- data.frame( list(id = "00000000000000000",
                           name = "Not avaliable", 
                                pluralName = "Not avaliable",
                           shortName = "Not avaliable", 
                             icon.prefix = "https://ss3.4sqi.net/img/categories_v2/food", 
                           icon.suffix = ".png",
                           primary = "TRUE") ) 
      } else { 
        x <- x } })