Resources

It's all fun and games til your analysis code is finished (Alex Rossell Hayes) | posit::conf(2025)

It's all fun and games til your analysis code is finished: the player package in R Speaker(s): Alex Rossell Hayes Abstract: The player package is a set of games built entirely in R that you can play in the R console. This lighthearted, humor-filled talk will introduce the package and demo some of its games, including opportunities for audience participation. If selected for a full talk, this will also include a demonstration of how the package code comes together, including approachable introductions to concepts like object-oriented programming with R6 and message handling with cli. The talk will show how playing around with R can be one of the best ways to learn new techniques (some of which can even be carried over into more serious programming). player repo: https://github.com/rossellhayes/player posit::conf(2025) Subscribe to posit::conf updates: https://posit.co/about/subscription-management/

image: thumbnail.jpg

Transcript#

This transcript was generated automatically and may contain errors.

Has this ever happened to you? You've got your analysis code ready to go, you're really excited to see those results, you hit enter, and oh boy, this is gonna take a while. This is not the energy we're looking for in a lightning talk. What am I supposed to do while R does all this? You know, some people might say, why don't you look for a way to speed up your code? And to them I say, I don't really know how, I'm not an expert on efficiency or optimization. Instead I'd like to offer you another solution. What if instead of trying to find the ways to get the most efficiency out of our code, we just look at it as an opportunity to take a breather and have some fun?

Here to introduce the player package. It offers a little bit of an alternative to what you can do when R is taking its time crunching the numbers. You see, when I look at R, I ask myself a question. Is this a game? I think we can all agree the answer is no. But I'd like to argue that if you look at R with a lens for fun, you can get some inspiration. I'm sure we've all run code like this before. Go to install a package, and R tells you there are updates available. Enter some numbers to choose which ones you want to update. I'll update number four. Okay, enter one or more numbers. Is this a game? We know where this is going. It's not a game, but it's teaching us an important lesson.

It's not a game, but it's teaching us an important lesson.

R is interactive. Now, if you've ever tried to make your code reproducible or teach R to students, you've probably gotten bitten by that interactivity. It can be a real pain. When it comes to having fun, that's not a bug. It's a feature. We can take that same idea of entering a number to update some packages and apply it to one of the games in the player package, playing five-card draw poker. It gives us a hand, and we're gonna say, you know, I don't like that third card, the four. I don't need the fifth card, the six. Let's throw away the nine. I'm gonna hold on to the jack and king. Let's see what happens. All right, royal flush! I'll be honest, I might have put a cheat code or two into the player package.

Games in the player package

And it's not just numbers that we can use in that or interactivity. Let's look at another classic newspaper game, the jumble. In that one, we're gonna enter a whole word. Let's see what we get, and oh boy, okay. Looks like we got something a little bit NSFW. Definitely would not want my boss to be seeing me working with untidy data. All right, we got numbers. We got words. You know, we can use a magic 8-ball, even interact with a whole sentence. I'm gonna ask my question, the one I always ask. Will I find love? Let's give that magic 8-ball a shake. I'm crossing my fingers, and that's decidedly so. Oh, the player package never does me wrong.

So, using just a little bit of R's interactivity, you can see that we can have a whole lot of fun while R goes and crunches its numbers. Okay, but I know what you might be thinking. When you think of a video game, you think of immersive experiences, you think of high-definition graphics, and playing some of these games in the player package maybe feels a little bit more like playing on a receipt printer than an HDTV. Okay, okay, I hear you. We need to bring a little bit more life into the R console. Well, we know how to print to it. We know how to clear the console, and if you rapidly alternate between printing and clearing, well, you got yourself a zoetrope, baby.

Animation in the R console

Let's see it in action with a little bit of a throwback. 2048, the game that took the world by storm maybe about a decade ago. If you're not familiar, the idea is we're gonna use their WASD keys and mash these little number blocks together to get bigger numbers, hopefully getting up to 2048. Let's see it in action. As you can see, each time I enter a command, R is rapidly clearing the screen, bringing a new screen in, and it looks like we've got these blocks actually moving around. What's that? That's animation! We've achieved animation in the R console! Okay, maybe it's still not HD graphics, but did you know that the console could do that?

That's animation! We've achieved animation in the R console!

All right, let's get out of here and take a look back at that important analysis that we were running, you know, because there was important stuff to do in R. It's not all fun and games. If we look back at our code, let's see what it's got. Oh, reply hazy try again. Okay, not the best news we were looking for, but the good news is it means we're back to square one. It means we're gonna have some more fun and games while we wait for that code to run.