Ride the carousel
I was browsing GitHub and the {slickR} package by Jonathan Sidi was suggested as a repo to explore.
{slickR} is an htmlwidget that generates carousels using the Slick JavaScript library by Ken Wheeler.
You’ve seen carousels before. They’re pretty ubiquitous for scrolling horizontally through images on websites. {slickR} provides a neat way of embedding these in R Markdown documents and Shiny apps. Plus, the elements in the carousel can be basically anything (iframes, plots, etc), not just images.
PokéDemo
I followed the excellent guidance on the {slickR} site to set up a basic carousel featuring Pokémon sprites1 from the Pokémon Black and White games, which I collected via veekun.com.
I’ve embedded the demo at the top of this page, but you can visit it on a dedicated page or see its source. These are probably best viewed on a desktop machine.
In fact, it’s three carousels that are stacked and synchronised: one with the front sprite, one with the back sprite and one that shows the number and name of the Pokémon. Click a sprite to go to the relevant page on Bulbapedia, the Pokémon wiki.
I’ve also set each page to display three ’mon at a time and that each page will advance by three. This is so that I can display one Pokémon evolutionary chain per page, since the max chain length is three (e.g. Bulbasaur > Ivysaur > Venusaur). I added a blank spacer pad where a chain was shorter than three (e.g. Rattata > Raticate > BLANK, or Farfetch’d > BLANK > BLANK).
The pages auto-advance in the demo above, but can be advanced manually in the standalone version by clicking the directional arrows, or by using your arrow keys if the widget has been selected.
It’s also possible to jump to a specific evolutionary chain by using the page ‘dots’ underneath the carousel. Here, you can click one of the tiny sprites to jump to the page that contains the evolutionary chain that starts with that Pokémon.
Carousel mechanics
Below is some illustrative code for the demo. The code for the full carousel demo is available from the matt-dray/pkmn_slickr repo.
Again, do check out the really helpful NBA example on the {slickR} website for more examples.
One carousel
Here’s an example of the code for an individual carousel that displays just the front-facing sprite:
front_carousel <- slickR(
obj = pkmn2$sprite_front_src,
objLinks = pkmn2$url,
height = 200, width = "100%"
) +
settings(
autoplay = TRUE, arrows = FALSE,
slidesToShow = 3, slidesToScroll = 3
)
Basically, You call the slickR()
function with arguments that define the obj
ect to display and also things like links (objLinks
) and the size of the elements. Here, pkmn2$sprite_front_src
is a vector of sprite image sources, for example.
You can then tweak the look and behaviour by adding settings()
with a +
(like {ggplot2}). Here, autoplay
advances the slides automatically; arrows
dictates whether to show manually-clickable advancement buttons; and slidesToShow
and slidesToScroll
control the number of elements seen on one ‘page’ of elements and how many elements to advance by.
Synced sandwich
I created two more of these {slickR} objects for the back sprite and the Pokemon name and number, with slight differences compared to the code above.
I then sandwiched all three together like this:
carousel <-
front_carousel %synch%
back_carousel %synch%
names_carousel
The %synch%
operator makes sure that advancing any of the carousels advances them all together.
Sprite dots
The third of the three carousels, the names_carousel
has the argument dots = TRUE
in its settings. This makes dots appear under the carousel to show which ‘page’ the carousel is currently displaying. You can also click these dots to jump to a given page.
You can change these dots to other things, like numeric values. I was able to use the guidance to replace the dots with sprites. This means you can click a mini sprite under the carousel to jump to the page where that Pokémon starts an evolutionary chain.
Stop, let me off
I have a lot of ideas for how to use this package, but for more on {slickR} in the meantime, check out:
- the {slickR} site
- the basic walkthrough
- how to use {slickR} in Shiny and other advanced things
- the package source
Pokémon and Pokémon character names are trademarks of Nintendo. Copyright is 1995–2020 Nintendo/Creatures Inc/GAME FREAK inc↩