knitr::opts_chunk$set( collapse = TRUE, comment = "#>", include = T, dpi = 300 ) if (file.exists("../.Rprofile")) source("../.Rprofile")
library(LegoR) library(dplyr) library(tidyr) library(ggplot2)
https://brickset.com/ contains data on historical lego sets as well as current sets. Unlike Lego.com, we can access Brickset data using an API (application programming interface). This does require registering for a brickset account and requesting an API key. All functions for the brickset.com data start with the brickset_
prefix.
brickset_setup() # guides you through the account setup process
Once you have your credentials, you can save them to your Rprofile using brickset_save_credentials()
. This will also save the credentials as global variables.
brickset_save_credentials("your_username", "your_password", "your_api_key")
Then, you can access brickset's data by authenticating. You may have to periodically reauthenticate depending on your internet configuration, but most functions should refresh the authentication automatically.
brickset_auth()
As with the Lego store, sets on brickset are organized by theme.
themes <- brickset_get_themes()
We can see what themes existed at the beginning...
# Oldest themes arrange(themes, yearfrom)
Or the themes that have been around the longest...
# Longest running themes arrange(themes, desc(yearto - yearfrom)) %>% head(10)
Most of the functions described in the API documentation have been wrapped; the exception is functions which concern a user's personal collection.
Some themes have sub-themes:
# Themes with sub-themes filter(themes, subthemecount > 0) %>% arrange(desc(subthemecount))
(brickset_get_subthemes("Duplo"))
Not all themes are available in all years:
(brickset_get_years("Pirates"))
We can get sets as well, searching using the set search api: https://brickset.com/api/v2.asmx?op=getSets
brickset_auth() # At this point I had to reauthenticate sets_2015 <- brickset_get_sets(year = 2015) sets_2015
Note that the setNumber
parameter is not equivalent to the lego set number, it is an internal Brickset number.
(brickset_get_sets(setNumber = 71040)) (disney_castle <- brickset_get_sets(query = 71040))
We can also get reviews and instructions for a set:
disney_castle_instructions <- brickset_get_instructions(disney_castle$setid)
The first link is here and shows the full instruction set for building a disney castle.
disney_castle_reviews <- brickset_get_reviews(disney_castle$setid) disney_castle_reviews %>% gather(key = "ratingType", value = "ratingValue", overallrating:valueformoney) %>% mutate(ratingValue = factor(ratingValue)) %>% ggplot(aes(x = ratingType, fill = ratingValue)) + geom_bar(position = "stack") + scale_fill_brewer(palette = "BuGn")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.