SWAPI är ett API med data över Star Wars filmerna som kan hämtas i JSON-format. Det finns ett R-paket rwars som kan användas för att hämta data från detta API, jag vet inte om det är till någon hjälp men det skall inte användas i uppgiften.
Ett exempel på datauttag ges av följande kod, läs mer på SWAPIs dokumentation.
library(httr)
library(jsonlite)
response <- GET("https://swapi.co/api/people/1/")
luke <- content(response, "text") %>% fromJSON()
str(luke)
## List of 16
## $ name : chr "Luke Skywalker"
## $ height : chr "172"
## $ mass : chr "77"
## $ hair_color: chr "blond"
## $ skin_color: chr "fair"
## $ eye_color : chr "blue"
## $ birth_year: chr "19BBY"
## $ gender : chr "male"
## $ homeworld : chr "https://swapi.co/api/planets/1/"
## $ films : chr [1:5] "https://swapi.co/api/films/2/" "https://swapi.co/api/films/6/" "https://swapi.co/api/films/3/" "https://swapi.co/api/films/1/" ...
## $ species : chr "https://swapi.co/api/species/1/"
## $ vehicles : chr [1:2] "https://swapi.co/api/vehicles/14/" "https://swapi.co/api/vehicles/30/"
## $ starships : chr [1:2] "https://swapi.co/api/starships/12/" "https://swapi.co/api/starships/22/"
## $ created : chr "2014-12-09T13:50:51.644000Z"
## $ edited : chr "2014-12-20T21:17:56.891000Z"
## $ url : chr "https://swapi.co/api/people/1/"
"https://swapi.co/api/people/1/") returnerar namn och ras (species) för rollfiguren. Om GET-anropet lyckas kommer response$status_code anta värdet 200 (lista över statuskoder), om den antar något annat värde skall funktionen stanna (stop) och ge lämpligt felmeddelande. Illustrera att detta fungerar genom att anropa funktionen med en felaktig url. Exempel:get_character("https://swapi.co/api/people/1/")
## Name Species
## 1 Luke Skywalker Human
"https://swapi.co/api/films/1/"), hämtar rolllistan. Illustrera genom att skriva ut rollistan för en film. Exempel:get_cast("https://swapi.co/api/films/1/") %>%
head(2)
## Name Species
## 1 Luke Skywalker Human
## 2 C-3PO Droid