stattleship / stattleship-r

Stattleship R Wrapper
https://api.stattleship.com/
MIT License
48 stars 14 forks source link

Nested Lists on Feats #5

Closed Btibert3 closed 8 years ago

Btibert3 commented 8 years ago

We need to be mindful of nested lists on feats.

This might require that we throw together some utility functions to help avoid this for the end user if the "core" datasets will have further nested lists.

  tmp_feats = stattle(TOKEN, ep="feats", query=list(game_id=tmp_slug), walk=T)
  x = do.call("rbind", lapply(tmp_feats, function(x) x$feats))

the do.call will fail because, I believe, how jsonlite parses lists into dataframes. Simply, it will put nested lists into a single column in the parsed dataframe.

For example,

  xx = tmp_feats[[1]]$feats
  str(xx)

yields

'data.frame':   40 obs. of  27 variables:
 $ id                   : chr  "e6d0736f-6bb2-4b3b-a941-18a3ca313ecd" "84919bf3-660d-4eb0-b3fb-6defd1f179e3" "fa1cf11a-d41a-439e-8e25-50efacd68e88" "06adadf4-d8d2-4c9d-84ed-9ee159ed1f88" ...
 $ created_at           : chr  "2015-11-30T06:35:33Z" "2015-11-30T06:35:33Z" "2015-11-30T06:35:33Z" "2015-11-30T06:35:33Z" ...
 $ updated_at           : chr  "2015-11-30T06:35:33Z" "2015-11-30T06:35:33Z" "2015-11-30T06:35:33Z" "2015-11-30T06:35:33Z" ...
 $ accomplished_vs      : chr  "had 1 hit vs. the Jets" "had 1 blocked shot vs. the Jets" "had 1 hit vs. the Jets" "had 3 hits vs. the Jets" ...
 $ accomplishment       : chr  "had 1 hit" "had 1 blocked shot" "had 1 hit" "had 3 hits" ...
 $ actual               : chr  "1.0" "1.0" "1.0" "3.0" ...
 $ category             : chr  "single" "single" "single" "single" ...
 $ game_id              : chr  "ed39fe61-ac5e-4879-99bd-a2eace456109" "ed39fe61-ac5e-4879-99bd-a2eace456109" "ed39fe61-ac5e-4879-99bd-a2eace456109" "ed39fe61-ac5e-4879-99bd-a2eace456109" ...
 $ humanized_stat_type  : chr  "hits" "blocked shots" "hits" "hits" ...
 $ level                : chr  "typical" "typical" "typical" "typical" ...
 $ name                 : chr  "hits" "blocked_shots" "hits" "hits" ...
 $ opponent_abbrev      : chr  "WIN" "WIN" "WIN" "WIN" ...
 $ opponent_location    : chr  "Winnipeg" "Winnipeg" "Winnipeg" "Winnipeg" ...
 $ opponent_name        : chr  "Winnipeg" "Winnipeg" "Winnipeg" "Winnipeg" ...
 $ opponent_nickname    : chr  "Jets" "Jets" "Jets" "Jets" ...
 $ sentence             : chr  "Brad Marchand had 1 hit" "Joonas Kemppainen had 1 blocked shot" "Joonas Kemppainen had 1 hit" "Zach Trotman had 3 hits" ...
 $ sentence_vs          : chr  "Brad Marchand had 1 hit vs. the Jets" "Joonas Kemppainen had 1 blocked shot vs. the Jets" "Joonas Kemppainen had 1 hit vs. the Jets" "Zach Trotman had 3 hits vs. the Jets" ...
 $ subject_id           : chr  "09c6fba9-2661-4879-b13e-94b3d7f1d692" "6490fd63-fcb5-482d-ad37-ff2153ce94e8" "6490fd63-fcb5-482d-ad37-ff2153ce94e8" "e833c9b7-f3c8-41e4-addd-6fa4ba584823" ...
 $ subject_name         : chr  "Brad Marchand" "Joonas Kemppainen" "Joonas Kemppainen" "Zach Trotman" ...
 $ subject_team_name    : chr  "Boston" "Boston" "Boston" "Boston" ...
 $ subject_team_abbrev  : chr  "BOS" "BOS" "BOS" "BOS" ...
 $ subject_team_location: chr  "Boston" "Boston" "Boston" "Boston" ...
 $ subject_team_nickname: chr  "Bruins" "Bruins" "Bruins" "Bruins" ...
 $ subject_type         : chr  "Player" "Player" "Player" "Player" ...
 $ verb                 : chr  "had 1 hit" "had 1 blocked shot" "had 1 hit" "had 3 hits" ...
 $ vs                   : chr  "vs. the Jets" "vs. the Jets" "vs. the Jets" "vs. the Jets" ...
 $ subject              :'data.frame':  40 obs. of  2 variables:
  ..$ type: chr  "player" "player" "player" "player" ...
  ..$ id  : chr  "09c6fba9-2661-4879-b13e-94b3d7f1d692" "6490fd63-fcb5-482d-ad37-ff2153ce94e8" "6490fd63-fcb5-482d-ad37-ff2153ce94e8" "e833c9b7-f3c8-41e4-addd-6fa4ba584823" ...

The problem, I believe, is that the column subject has nested data, something that jsonlite introduced. Let me dig into the docs, I think I recall seeing a flatten option.

dthyresson commented 8 years ago

I had to handle this when I build some ruby results.

players.detect {|p| p.id == (item.player_id || item.subject_id)}

Would it help if I included player_id and a team_id (that's the other subject type) in the Feat JSON?

Btibert3 commented 8 years ago

I want to try out an option in jsonlite that flattens nested data. It might do the trick and keep things simple.

dthyresson commented 8 years ago

I added those keys for player and team to see if it helps. It's deployed to production now.

-- david

On Dec 11, 2015, at 09:02, Brock Tibert notifications@github.com wrote:

I want to try out an option in jsonlite that flattens nested data. It might do the trick and keep things simple.

— Reply to this email directly or view it on GitHub.

Btibert3 commented 8 years ago

Fixed on push https://github.com/stattleship/stattleship-r/commit/cfad53f6f1b42ab43bb3339347492912e39aae55