Skip to contents

got_chars is a list with information on the 30 point-of-view characters from the first five books in the Song of Ice and Fire series by George R. R. Martin. The data was retrieved from An API Of Ice And Fire.

library(purrr)
(nms <- map_chr(got_chars, "name"))
#>  [1] "Theon Greyjoy"      "Tyrion Lannister"   "Victarion Greyjoy" 
#>  [4] "Will"               "Areo Hotah"         "Chett"             
#>  [7] "Cressen"            "Arianne Martell"    "Daenerys Targaryen"
#> [10] "Davos Seaworth"     "Arya Stark"         "Arys Oakheart"     
#> [13] "Asha Greyjoy"       "Barristan Selmy"    "Varamyr"           
#> [16] "Brandon Stark"      "Brienne of Tarth"   "Catelyn Stark"     
#> [19] "Cersei Lannister"   "Eddard Stark"       "Jaime Lannister"   
#> [22] "Jon Connington"     "Jon Snow"           "Aeron Greyjoy"     
#> [25] "Kevan Lannister"    "Melisandre"         "Merrett Frey"      
#> [28] "Quentyn Martell"    "Samwell Tarly"      "Sansa Stark"
map_dfr(got_chars, `[`, c("name", "gender", "culture", "born"))
#> # A tibble: 30 × 4
#>    name               gender culture    born                                    
#>    <chr>              <chr>  <chr>      <chr>                                   
#>  1 Theon Greyjoy      Male   "Ironborn" "In 278 AC or 279 AC, at Pyke"          
#>  2 Tyrion Lannister   Male   ""         "In 273 AC, at Casterly Rock"           
#>  3 Victarion Greyjoy  Male   "Ironborn" "In 268 AC or before, at Pyke"          
#>  4 Will               Male   ""         ""                                      
#>  5 Areo Hotah         Male   "Norvoshi" "In 257 AC or before, at Norvos"        
#>  6 Chett              Male   ""         "At Hag's Mire"                         
#>  7 Cressen            Male   ""         "In 219 AC or 220 AC"                   
#>  8 Arianne Martell    Female "Dornish"  "In 276 AC, at Sunspear"                
#>  9 Daenerys Targaryen Female "Valyrian" "In 284 AC, at Dragonstone"             
#> 10 Davos Seaworth     Male   "Westeros" "In 260 AC or before, at King's Landing"
#> # … with 20 more rows

The same got_chars data is also present as JSON and XML files. Accessor functions provide the local file path.

got_chars_json()
#> [1] "/home/runner/work/_temp/Library/repurrrsive/extdata/got_chars.json"
got_chars_xml()
#> [1] "/home/runner/work/_temp/Library/repurrrsive/extdata/got_chars.xml"

You can practice bringing data from JSON into an R list.

library(jsonlite)
#> 
#> Attaching package: 'jsonlite'
#> The following object is masked from 'package:purrr':
#> 
#>     flatten
json <- fromJSON(got_chars_json(), simplifyDataFrame = FALSE)
json[[1]][c("name", "titles", "playedBy")]
#> $name
#> [1] "Theon Greyjoy"
#> 
#> $titles
#> [1] "Prince of Winterfell"                                
#> [2] "Lord of the Iron Islands (by law of the green lands)"
#> 
#> $playedBy
#> [1] "Alfie Allen"
identical(got_chars, json)
#> [1] TRUE

You can practice bringing data into R from XML. You can get it into an R list with xml2::as_list(), but to get a list as nice as those above? That requires a bit more work. Such is XML life.

library(xml2)
xml <- read_xml(got_chars_xml())
xml_child(xml)
#> {xml_node}
#> <elem>
#>  [1] <url>https://www.anapioficeandfire.com/api/characters/1022</url>
#>  [2] <id>1022</id>
#>  [3] <name>Theon Greyjoy</name>
#>  [4] <gender>Male</gender>
#>  [5] <culture>Ironborn</culture>
#>  [6] <born>In 278 AC or 279 AC, at Pyke</born>
#>  [7] <died/>
#>  [8] <alive>TRUE</alive>
#>  [9] <titles>\n  <elem>Prince of Winterfell</elem>\n  <elem>Lord of the Iron  ...
#> [10] <aliases>\n  <elem>Prince of Fools</elem>\n  <elem>Theon Turncloak</elem ...
#> [11] <father/>
#> [12] <mother/>
#> [13] <spouse/>
#> [14] <allegiances>House Greyjoy of Pyke</allegiances>
#> [15] <books>\n  <elem>A Game of Thrones</elem>\n  <elem>A Storm of Swords</el ...
#> [16] <povBooks>\n  <elem>A Clash of Kings</elem>\n  <elem>A Dance with Dragon ...
#> [17] <tvSeries>\n  <elem>Season 1</elem>\n  <elem>Season 2</elem>\n  <elem>Se ...
#> [18] <playedBy>Alfie Allen</playedBy>