mario icon indicating copy to clipboard operation
mario copied to clipboard

argh, R seems to export a single-element list to JSON as that element itself

Open pgbovine opened this issue 4 years ago • 6 comments

Not how R5-D4 and Biggs Darklighter have chr [1] (a list of 1 element) in films:

> starwars %>% select(name, films) %>% slice(5:10)
# A tibble: 6 × 2
  name               films
  <chr>              <list>
1 Leia Organa        <chr [5]>
2 Owen Lars          <chr [3]>
3 Beru Whitesun lars <chr [3]>
4 R5-D4              <chr [1]>
5 Biggs Darklighter  <chr [1]>
6 Obi-Wan Kenobi     <chr [6]>

However, in the JSON, their entries show up as strings and NOT as a list of a single string (which is what's actually represented). This loses vital information:

      "rhs": {
        "col_names": ["name", "films"],
        "data": [
          {
            "name": "Leia Organa",
            "films": ["The Empire Strikes Back", "Revenge of the Sith", "Return of the Jedi", "A New Hope", "The Force Awakens"]
          },
          {
            "name": "Owen Lars",
            "films": ["Attack of the Clones", "Revenge of the Sith", "A New Hope"]
          },
          {
            "name": "Beru Whitesun lars",
            "films": ["Attack of the Clones", "Revenge of the Sith", "A New Hope"]
          },
          {
            "name": "R5-D4",
            "films": "A New Hope"
          },
          {
            "name": "Biggs Darklighter",
            "films": "A New Hope"
          },
          {
            "name": "Obi-Wan Kenobi",
            "films": ["The Empire Strikes Back", "Attack of the Clones", "The Phantom Menace", "Revenge of the Sith", "Return of the Jedi", "A New Hope"]
          }
        ]
      }

This issue may be related to https://github.com/seankross/mario/issues/2

pgbovine avatar Nov 04 '21 06:11 pgbovine

This is a matter of knowing how to manipulate jsonlite:

starwars %>% 
  select(name, films) %>% 
  slice(5:10) %>% 
  mutate(films = I(films)) %>% 
  jsonlite::toJSON(pretty = TRUE)
[
  {
    "name": "Leia Organa",
    "films": ["The Empire Strikes Back", "Revenge of the Sith", "Return of the Jedi", "A New Hope", "The Force Awakens"]
  },
  {
    "name": "Owen Lars",
    "films": ["Attack of the Clones", "Revenge of the Sith", "A New Hope"]
  },
  {
    "name": "Beru Whitesun lars",
    "films": ["Attack of the Clones", "Revenge of the Sith", "A New Hope"]
  },
  {
    "name": "R5-D4",
    "films": ["A New Hope"]
  },
  {
    "name": "Biggs Darklighter",
    "films": ["A New Hope"]
  },
  {
    "name": "Obi-Wan Kenobi",
    "films": ["The Empire Strikes Back", "Attack of the Clones", "The Phantom Menace", "Revenge of the Sith", "Return of the Jedi", "A New Hope"]
  }
] 

seankross avatar Nov 22 '21 06:11 seankross

the force is strong with this one

pgbovine avatar Nov 22 '21 16:11 pgbovine

@seankross - can i bump this up again? fixing this will also fix some of the problems with the recent wreck-it crashes. check out group_indices here:

 "rhs": {
          "col_names": ["species", "bill_length_mm"],
          "data": [
            {
              "species": "Chinstrap",
              "bill_length_mm": 58
            }
          ],
          "group_data": {
            "col_names": ["species"],
            "group_indices": 1,   # <-- it should be [1] - a list of size 1, but it's a single number 1 here
            "group_keys": [
              {
                "group_id": 1,
                "key": {
                  "species": "Chinstrap"
                }
              }
            ]
          }
        }
      }

pgbovine avatar Dec 09 '21 16:12 pgbovine

note that if you fix this, i think most of your test output files will stay the same, since this is a rare occurrence.

here's a test case that triggers this trace:

library(dplyr)
library(palmerpenguins)

penguins %>%
  select(species, bill_length_mm) %>%
  group_by(species) %>%
  arrange(desc(bill_length_mm), .by_group = TRUE) %>% 
  filter(bill_length_mm == max(bill_length_mm))

pgbovine avatar Dec 09 '21 16:12 pgbovine

Fixed in 0a1a45d for the "group_indices" json case but not in the case of list columns, which I will handle when I implement some type checking.

seankross avatar Dec 10 '21 01:12 seankross

nice! welcome to the desert of the real (it's 1000000 edits like these, each seemingly small, that will make tdt into something lasting!) https://www.youtube.com/watch?v=IuwLrXX63UY

pgbovine avatar Dec 10 '21 15:12 pgbovine