Transcript#
This transcript was generated automatically and may contain errors.
Let's go ahead and get started.
Welcome back to the Data Science Lab, everybody. My name is Libby. I do community stuff here at Posit, and I'm joined by my co-host, Isabella Velazquez. Isabella, would you like to say hello?
Hi, everyone. Thank you for joining. I'll be posting the links, and if you're watching this on YouTube, just know all the links are in the description and very, very helpful.
And today, we are going to be talking about testing in R. This is going to be a lab where we are having a discussion back and forth as a whole community, because I feel like testing is a really broad topic, but also something when I talk to a lot of people, they say, I don't do it.
And I am one of those people who went all through undergrad and grad school, learned all about, really focused on statistics, applied statistics and reproducibility, and not a lot on programming fundamentals and testing.
So I'd love to know in the chat, maybe we can put a poll, just an informal poll in the chat to ask, do you do testing in R?
And while we wait on the results of that poll, I would love to introduce our featured lab manager today, who's going to help us learn about testing and have this open conversation, Jackie Ziebert. Jackie, would you like to introduce yourself?
Yeah, I'm Jackie. I work as an R slash Shiny developer at the Ohio Colleges of Medicine, the Government Resource Center. We do public and private-facing dashboards, but I do unit testing on, I guess, the stat analysis that is used in our dashboards.
So we have a chat or a poll up in Zoom right now that says, do you do testing in R? Yes, no, and maybe.
Oh, there's a cheeky 18% saying maybe.
I was trying to think of a good third option.
I know, because sometimes there is a secret third option.
I'm going to vote. I can't vote in Zoom because I'm the host, but I'm going to vote in Discord and put my no in there, because I just don't do testing in R a lot.
But let's see what we have. I'm seeing a majority no from both of our polls.
A little over half of people say, nope, I'm really not doing testing in R.
Sean says, why test when you can debug in prod?
And Shivani says, I've only ever done dry run testing with pen and paper.
Ryan says, I don't do unit testing, but Claude does. Ryan, Claude writing tests for me makes me so mad. I have tried it.
Christy says, we do unit testing for everything we do in Python. Our R stuff is not unit tested, but we're working on it. OK, so we're going to have a unit testing episode on Python with Rich Iannone, who's the creator of the GT package and the point blank package. We know Rich. We love Rich. He's going to be with us to do Python.
It will be another open conversation of like, let's just talk about getting started with this. We're not aiming to become zero to 60 experts in testing in R or Python.
And then the last half of this month, we're going to do some testing in Shiny specifically. The reason we're breaking all of these up into testober, as we're calling it, is because for a lot of people, including myself, these are very, very new concepts. And testing in Shiny is its own beast.
If anyone has ever done like debugging in Shiny or testing in Shiny, you know that it lives in its own world and it has some very unique challenges and also some specific packages that go along with it.
I think our final count here for our polls, and I will stop yapping, is we are still majority no for both polls. About half of people say no, and about a third of people are saying yes. And then there's a cheeky 15% to 20% who are saying maybe because they're not quite sure and they need that third option.
So, Jackie, I would love it if we could talk just a little bit about why unit testing is important.
And I am going to share my screen really quickly and just share a very ugly function and ask you all if maybe you can relate to this ugly function.
So, this is a fake function. Well, actually, you know what? Parts of it run. I'll show you. This is what I call a monolith function. Have you ever written a function that looks like this?
It does all the things in one function, right? So, we have a function defined up here, and we are going to analyze some penguins data. It's going to do all the things, right? It's going to, like, summarize the data. Maybe it's going to clean the data. This data is pretty clean. We don't need to worry about that.
It's going to create, like, just... Sorry to interrupt you. Could you make it a little bigger, please?
Let's make it all extra big. So, it does all the things, right? And you're like, I'm just going to run this one function, and it's going to do all my work for me.
Why is this bad? Can anybody tell me in the chat why this might not be, like, the best thing to do?
Christy says, it's going to be hard to unit test the monolith. Because it's hard to tell where something has gone wrong.
Yeah, Frank says, if it fails, the question is, where has it failed? No Tobacco says, if it's broken, you might not know exactly right away. Exactly. It could break anywhere. Your function might just not work.
And you don't know where in your 130 something lines of code the thing specifically has gone wrong. And when I was putting together this fake function, by the way, we're going to get back to our stacked layout here. When I was making this fake function that technically does run, we're going to source this function. Because I'm not working in a package. This is just a script.
I can run this one thing. It tells me a warning message.
And it's literally a warning message about one of the like 40 things that this function does. It's about the plot that I have. I have a little box plot in there.
So not only could you have failures, but you could have warnings that tell you, hey, just so you know this thing happened. And you're not even really going to be clear on what it's warning you about.
And so two of the many things this function does is creates a plot. There it is. Pulling this out of a list that I rendered or I returned. And it creates a GT table. So great. But like unit testing, that's going to be a lot harder than we want it to be.
The case for modular functions
The better way, the way that Jackie's going to talk about, although Jackie's going to talk in the framework of a package because she's way cooler than I am. She does things in packages. Is a more modular way, right? We could have a folder that contains multiple scripts that contain multiple helper functions or one function per file as well, right?
Something to just handle the summary stats separately, the formatting for the table separately, the plotting separately, something to put it all together. This could even be in a totally different folder that just does like a config first. There are all kinds of ways to put this together, but this is going to be a lot easier. So this is the framework I want you to think in.
If you are used to writing monoliths, whether that's a shiny package monolith or a shiny application monolith or a file that's a monolith, we are going to try to think about modularity today.
If you are a monolith function writer or a script writer, don't feel bad. I don't want you to feel shamed. It's completely normal to sort of like migrate towards modularity over time. Okay, so I'm going to stop talking, but I'm hoping that all of that made sense.
And I already see a couple of questions, by the way, today in the discord chat. If you see questions with a little golf flag on them, that just means we see you. We see your question. We're marking it. If a check mark ends up on it, it means that we saw somebody answer it in the chat or we feel like we've answered it. If you see somebody else who has asked a question and we've missed it, put a golf flag on it for us. We would love that.
All right, Jackie, I would love it if you could take it away and talk to us a little bit about, before you get started, why modularity and testability is helpful for you in your job.
Yeah, for us, it's like the problem that we were having, we had a bunch of code, copy and paste into different dashboards. It wasn't really in a tidy package. So we had issues with, we fix a bug. We made a change to the code. Is the bug going to, we don't know if it's going to be an issue in the future, the same bug, but we fixed it now. It works, I guess. And just not understanding what the function was actually, I guess, doing. You put something in it. It spits something out. I didn't have a lot of confidence in the code was the main issue.
I didn't have a lot of confidence in the code was the main issue.
So what I decided to do is just refactored a bunch of the code, like the behavior stayed relatively dissimilar. You put in a data frame, it does some calculations, it spits some things out. And during that process, I broke things down into modular functions so I can understand what is the inputs, what are the outputs. Just having a much better understanding. And during that time, I was also writing unit tasks. How do we handle edge cases?
Well, I'm going to hop in with a couple of questions from the chat. We can add those in here.
Ryan had asked, does input validation count as testing? I say yes. And I think it's one of the most important forms of testing. Yeah. And it's also a very programmatic thing where you're like, I'm going to make sure that every time this runs, the input that I'm getting in is the right stuff.
The sneakiest, most insidious form of failure for me is when I have a live something, a dashboard, whatever it is, a shiny app that is accepting real-time data or maybe batch data four times a day or whatever. And suddenly, the output that I'm getting from all of my functions, it's not failing, but it doesn't look right. And I don't know why. Because somehow behind the scenes, something has changed.
And if you don't have input validation that's making sure that the data you're taking in is correct or just looks as expected, the form you expect, within the range you expect, then something could have just gone wrong silently, which can be terrible. Because I would rather have my dashboard not work than have my dashboard confidently tell somebody the wrong thing.
And it's definitely useful for the user. Someone's using that function. They don't know what's going on. Like, boom, they got the right error message right away.
Oh, yes. Having error messages that are correct and helpful is very, very good.
Nathan asked, can we? Nathan says, wait, we can have nested folders in the R folder of a package?
No. I would say maybe don't do what I did. I did two different versions of something. So I did a monolith version, version one, and then a modular version, version two. I would simplify. I would pick one of those, the modular one maybe, and just have functions inside of that that are your function files. Good question, though.
Don't do what I do. All right, Jackie. I'm going to have you share your screen. And we can take it away. And Isabella has lots of links to share as we go through and talk about stuff.
And Jackie's going to give us some example with some fun data today.
We're going to try not to get distracted by Jackie's data, because I love this data. Oh, we need to make our text bigger, though, for sure. Even bigger, OK. Oh, definitely a lot bigger.
I would say, yes, Jackie's screen is going to get a little cramped here. Everybody let us know how you are doing with text size.
Catherine says, OK, I'm OK right now. But if anybody else is like, this is a little too small, we can try to go a little bit bigger.
Setting up a package for testing
And I'm going to interrupt you really quickly and say, can you show people how to get their extra source column? Because this is a question I get all the time. I use an extra source column in RStudio as well. Oh, yeah. I love this.
Go to panes and console on the right. I believe that's what I did. I think it's just view add panes and then add source column. Add source. It's been a while since actually.
Yes. Yeah. And it adds a two page layout. It's really helpful when you're writing a unit test. You get the unit test on the left side and you got the function on the right.
Yes. All right. My preferred way and your console on the right.
Yeah, I know. As Libby mentioned, I like to do things on like a package. What that means is all you need is all your functions in our folder and a description file. Like, that's it. It's pretty simple. It doesn't have to be any CRAN specific. All you need is just. Yeah, the description file, the name and the package. I just said ds.lab. It doesn't like spaces and I didn't really fill this out.
That's OK. This is all for demonstration purposes only today.
And I was like, you can get like a basic structure by writing, by just writing, create package or I usually just go to file and new project. Then new package. I think you have to have tools installed in order to have that option.
Yeah, then it creates a little bit of structure for you in a little mini package. It's pretty nice.
OK, so like, like Libby kind of did, I did a little bit of a. I broke it down. I already have like this little dummy data of board games, have a bunch of R scripts and their own little functions. So the function name is also the name of the R script.
Doesn't matter what they do, that's just it is. It's a function. And since it all, they're all in dot R's, I can use just DevTools, but all to put all those functions into the environment. So that's what I'm going to do.
There's a few ways I just do a shift control all. Shift control all. What all look, I got these functions.
I was going to run over a little bit of analysis. Yeah, these board games here. I got this data set from tidy Tuesday in 2022. So these these board games are a bit outdated.
I mean, like, this is like, I love board games so much. I told Jackie, we're going to have to, like, put a muzzle on me because I'm going to just want to talk about board games this entire time, and this will not be productive. If anybody else out there. I don't know if you can see my board game shelf behind me, but there's a bunch of board games behind there.
OK, and so we put I put a little poll in the discord asking you if you ever use usethis. So as Jackie's going through and just kind of running her script here. Um, I want to point out, like, usethis the very top one in line 12 that she used create package for usethis also does like a billion other extremely useful things. And if you've never used it, I highly encourage you to check it out.
And then I wanted to point out the other thing that we're going to be using today, which you'll see in line 27 that Jackie has testthat we will we will ask that question in just a minute. But any other packages we might need to know about today, Jackie, besides those.
Um, not for unit testing, but there are other packages that I think are useful for when you're writing your functions, such as assert that and the cli package really good for like writing error messages.
OK, perfect. So we're going to assert that and Jackie and I say it differently. I feel I for writing useful error messages. I'm I'm sure many people have other packages that they used to. There is a lot of cool tools out that tools out there.
OK, I'm seeing about over half of people said yes, they use usethis.
Um, and Connor had asked, are these package development RStudio shortcuts available in Positron? I think that depends on whether or not we are talking about. Yes, I think you might have to enable RStudio bindings first, if I remember correctly, but they do work in Positron. I am trying Positron. The packages for sure do.
And also Positron has its own special like extra fun stuff for testing, actually, if you are testing a package. Um, so I would say there might be some slightly different things like the build pane in RStudio will be a slightly different experience from what you'll get in Positron. Um, but like, for example, when we had Jenny Bryan on the data science hangout, she was like the testing in Positron is like next level super good. And I was like, I'm just going to believe you, Jenny, because I have never done testing in RStudio versus Positron before.
the testing in Positron is like next level super good.
Okay, I would love to do more in Positron. That sounds cool.
Setting up the testing environment
Yeah, I mean, since there are some benefits of having all your stuff in our, in our script too, because you can use a bunch of these like usethis functions, which I'll go over in a bit.
Now I just want to go over a top 10 top board games. I know, can you see, I don't know if anyone's actually heard of these designers.
Um, so I wanted to just choose one of these functions, start some unit tests and see how that goes. All right, let's do it. So what we're doing in when we are unit testing, the unit part of unit testing means we are testing one part of something. So we're not going to test the entire package in one go. We're not going to look for test coverage, whether or not we have every single function or part of our entire package covered, we're just going to focus on adding a test to test one small part of our whole.
And we're going to use the testthat package. Yeah, actually, in combination with a few things, I'm sure.
So the before we do that, we have to kind of set up our little testing environment, just set up a little bit of like a folder structure. So what I like to do, you just do run this, use use test that. So I'm like, I'm going to, I got this package, I'm going to use testthat in my package.
It does a couple things. It creates a folder called tests in there and it creates this test set that are so it's just this is something like you should never have to edit, but it just tells you we have testthat we're going to run it for this package and the name of this package is ds.lab. Like, I don't ever touch this file inside that testthat folder is that's what we're going to have our unit test.
So let's just say I have I just I wrote this function, we'll go over it. I have this. So I have this function. It's in the R folder. I want to create unit tests for that. So it's already in the dot R folder or in the R folder. I can use use test. Like, awesome, it creates a test file for you. Like, awesome, it creates a test file for you already.
So it does. It's the name of the R script and it adds test right in front of it. It creates a little dummy test. Pretty cool. So, so useful. And this will give you a little glimpse into like what a test looks like. We are testing that multiplication works and we are expecting something and then there's all kinds of different expect functions.
Writing your first unit test
Yeah, so like a basic unit test is usually what is the result? And what is the expected behavior? So we expect two times two to equal four. So I can just click that button, run the test. Fail, zero, warning, zero, S is, oh my god, I'm blanking right now, okay.
One test, expect that, that's one unit test, one pass. Cool. S is skip, I think. So it's fail, one, skip, one pass. Skip, yes, that's it, that's it. Thank you. Because you can say, hey, I know this function is going to fail. Just skip it for right now because I want to build my dang package.
So Jackie could add in there like, hey, let's just like skip this thing. But notice that everything's showing up in a different pane of RStudio than you may have ever seen before if you've never built a package or done testing and that's the build pane.
So yeah, it might look like a little console down there, but that is actually a whole different pane.
So let's just say, let's just say, so people can like rate board games from like one to 10, using those integers. And then there's a column in the data that has like average rating. So it could be like 4.6, 9.9, and so forth. So we're just going to recode those. And let's just go through, recode it. So if you expect zero, what do we, what do you expect that to be?
Oh, by the way, 10 is usually a good game. Zero is like, no, that game sucks. I'm never playing that again. That's my rating for Scythe. Even though it's a top 10 game always. There's worse games, but I get, I gotcha. Libby doesn't like competitive games where people are mean to her. You like the co-op? I like co-op games. Yes. Pandemic, before the pandemic happened, pandemic, it was one of my favorite games. And then I never got to play with anybody again because of the actual pandemic.
I don't know. It's like a simple way of writing. You can actually just do this pretty interactively. So it's usually, test that. The first thing you do is like, works, when, in, but, is, zero is just basically a basic statement of what you're testing.
And inside of that is just, you're basically testing some output and what, and comparing it to the expected. So you are giving it your function that you made called categorize ratings. And you're feeding it an argument of zero. And you're making sure, I want to make sure that categorize ratings still works when it receives a zero as a rating.
So, I mean, expect that should equal this, right? Pretty simple. Oh, God, what did I do? Oh, I know why. It's because it's, when you use cut, it's going to be a factor. So we just want to convert that to a character. There we go.
So let's just say, we want to do this again. But, what if, we want two to be, likely it won't play this again.
And while Jackie is thinking, I will point everybody towards the Discord where Shelby has helped answer Connor's question. Because Connor had asked, like, are these package development, RStudio shortcuts available in Positron? And we were thinking like, okay, Positron has like a different experience. How do they compare?
Oh, I know what I did. That DevTools load all works by default with the keyboard shortcut, Shift Control L, and possibly a few more. And then that she also used custom shortcuts inside of her keybindings.json file. I too put custom keybindings. If anybody else has any more information, I know Nathan has created some custom keybindings. He might be able to share a blog post if he's willing to.
I, okay, I remember what I was gonna do. I was gonna like give an example. What are some of the arguments? We want to, to be likely, we'll play this again. I removed what I was gonna.
So I'm like, okay, we know something. We want the behavior to be this category. It's not doing that. So what we want to do is want to add right equals false to this. I'm going to save that. Control Shift L, run this. Okay, cool. That's running as expensive as expected. Wait, what did we do? I missed it. Oh, I'm sorry. Oh, right equals false all the way over on the left. I commented, yeah. Yeah, I was gonna write just a basic function.
What do we want this to look like? So then, all right, cool. That's working. Let's run all these unit tests. Now, I'm just like, okay, cool, still working. So like, you kind of want to test like that, the endpoints too. So let's do 10, 10.
We're kind of having a conversation with our function and going like, hey, do you still work when I do this to you? Do you still work when I do this with you? I want to point out that like, I can almost never think of all of the ways I can almost never think of all of the ways that I am able to break my own function beforehand. This often happens iteratively as I am using my app or whatever it is. I'll just break it along the way and then add tests. That is fine.
I can almost never think of all of the ways that I am able to break my own function beforehand. This often happens iteratively as I am using my app or whatever it is. I'll just break it along the way and then add tests.
I mean, oh, look, 10, it's broken. Let's just run. Let's see what happens when this tests. It's not what I would expect. Excellent always wants to play it. What the heck? All right. No, the actual is actually NA. Weird, I want it to be 10.
So the cool thing about when it's a failure, you can actually like, let's just say, that's not open. You could click on this. It actually brings you exactly where that test failed.
And when you're writing tests, you want them to break. You want to break them out pretty a lot. I guess it's a lot easier when you are something fails. You know exactly which tests fail. So it's OK if you're copying and pasting these unit has like a bunch of times.
You just want each thing that you're testing to be It's okay if you're copy and pasting these unit tests a bunch of times. You just want each thing that you're testing to be their own little environment and testing usually one thing at a time.
Yeah, same thing as your functions. You don't want your test to test a bunch of things. You want your test to just test anything. Yes, and unlike functions, it's okay to copy and paste your unit tests a bunch of times.
Let's just say we include include lowest equals true. Let's run the test again. Oh, thank God. All right, that's the expected behavior. Okay, so then what do we want? So let's just say some user puts in a negative, negative one.
Not completely outside of that. You know, it's going to be an A. Let's just say we're doing some analysis. We don't want NAs in this column. Okay. I was going to give you like a couple different ways, but that maybe we could write like a statement to the user, like, get rid of these NAs.
Aha, so we can yell at them. You get to yell at your user. Yes. Especially if it's you.
I also want to go over like another way that you can like assert that package. It also has a lot of like nice assertions that you can do. But you have to assert that is not an A. Oh, my gosh.
Assert. I'm not sure if this will work. I like that no NA exists, though. Is there? Oh, yeah, that's.
It goes, yeah, message, oops, message. Did everybody light their candles? Because we are live coding. I don't know.
All right, so let's just see if this assert that works. Okay, no. Let's go with. Let's put our if statement back in there, see if it works. Let's just see if this one will work.
What? Oh, if. Oh. So it's still counting it as the very lowest, right? Yeah, I don't know. Sure. What am I doing wrong? It's I'm sure somebody knows. Oh, because I'm testing in a I'm in a.
You are testing negative. Oh, my God. I'll put his NA if rating. Oh, my gosh. If rating.
Oh, I want to test the NA. I'm going to do it right. Oh, my gosh. If rating. Greater than. 10. Or. I'm doing the double works. It's either that. Or. That. Rating is. One through. 10. It was like mutually exclusive to.
We did it. Okay. Outside of one to 10. Like how.
There is. It's a good package. It's. If this then stop, I can get a bit.
I would like to point out to everybody. That you can change your vote. A couple of people have gone up and change their votes and are no longer voting.
I feel like a lot of people, you know, are testing the code. It's just a way of just really formalizing this and writing it down.
I feel like a lot of people, you know, are testing the code. It's just a way of just really formalizing this and writing it down.
Testing error messages
Sweet. Okay, cool. So then I also want to go over like two ways. How do we like, how can we test. This error message to know that shows up, there's like two ways you can do that. You do expect. Instead of writing this, we expect an error when this function is read, and we expect the error message.
Gosh, I can't remember which one this I just go. Here. I'm just gonna run this. Sweet, nothing. Yeah. So expect an error when the input is negative one and we expect the error message to be rating is outside one to 10.
Perfect. And I wanted to point out, Nathan had asked a question. What's nice about using assert that as opposed to using that like, hey, stop, if you didn't do this, and just like have it give you a message syntax. I think that the main thing is just that it, it, I would say packages automates, whatever it is, make sure that you have a clear and clean message and you don't have to do it all separately. Like you could write that if statement without a stop message in there and you could forget that. And then your user would like to have no clue why you were stopping. Well, it could just fail.
It also makes sure it actually, it tests for it if it isn't, but it actually tests. How do I put it? See here, like you would tell us, like, if it's not an integer. First is an integer. It's a different way of saying how you can test for something. Does that make sense?
When you use assert that your messages sort of like baked into the. Your function. Yes. Yes. Yeah. Yeah.
I'm sure we have a question from my chat, which is like, Sharon your MythBusters meme is right on target. The only difference between screwing around and science is writing it down.
Yeah, this is another cool way to test this. This is like, if you use an LLM, they're like, use this way to test your air messages. Like, okay.
Snapshot testing
We can expect a snapshot. What it, what this does. It'll actually take a snapshot, like a string and it'll save that string somewhere. And when that, if that string were ever to change, it'll let you know. So it'll make more sense if I, I just run these tests here.
So let's just say, oh, oh yeah. Yeah. I did it right. I have to do air equals. You're clicking the run tests button at the top, right? I did. Okay. Just making sure everyone is seeing what Jackie is doing. You can also do shift control T.
If you run the run tests, it runs this specific test file. Right. But if you're pressing the test button, it runs all your tests, everything. So I just been clicking this, this button here. It also loads all your packages or your functions into the environment before you run the test. As well. So sometimes you might have category this into your environment, but this actually puts those functions into the environment first. Like when you're loading a package.
So the first thing it did. So I did a warning, right? So the first thing it does, it says adding a new stop and snapshot for this code and that's it. It's just letting me, you know, it create a snapshot for you. So if you go into your, like the test folder and you click on snapshots, it creates like this MD file. You never added this, but that's just how it kind of stores.
This is a name of the test. And this is what I don't know. It auto generates this for you. Error in this and that's the error message. So that's pretty cool. Let's just say I'm going to change the error message to something.
Oh, we got a comment slash question about our error message. Yeah. Amelia says, I think maybe it should be zero to 10 instead of one to 10 because zero isn't allowed. Is allowed, right? Is zero an allowed rating for you? There we go. Good catch. Thank you.
So then I changed. Good. Good segue. So I changed the error message. I'm going to, I save this. I'm going to do it. Then I'm just going to run tasks. It should load by category that, that change into the environment. I'm going to run the test. Sweet. It failed as expected, but that's only because our message change. It's like, do you want to reveal this? Sure. I'm going to click on review, review this. It's like, this is what changed. I think in the backend test that uses waldo. So that's how it gives you like these outputs of what has changed.
And I'm going to go ahead and accept that. Sweet. I like that change. Good catch, Amelia. And waldo is also a super fun package. It just has like really basic stuff that lets you compare things to each other. So if you've ever been like, are these the same thing, but you're looking for a specific way in which they're the same or different. Yeah. Go check out waldo.
So the snapshot that we took and that we compared to was a string and that string in this instance was our error message, but snapshot testing can be done on all kinds of things, right? Yeah. So I've been using it for like, so I write a bunch of like JavaScript for the e-charts. So I've been writing like unit tests and it'll take the snapshot of the Java, the string, and it'll just save it for you. So if I make any changes that makes that string, although we're good. So you can use it for. You can use it for many other things.
But that's okay. We don't have to cover everything. This is, this is, this like a lab is definitely a, let's get you interested in unit testing, get you like over the hump of like, I don't know what this is. And I've never done it.
And Amelia, if you're up for it, let's see if we can, if we can answer it on the fly. Amelia says, can you embed values into a string? So you can embed values into a string. You can embed values into a string. You can embed values into a string. Okay. So let's see if we can answer this on the fly. Amelia says, can you embed values in the error message? And the value being like, you know, Calling it in brackets, like an actual value from here, our session.
So like input outside of minimum rating to maximum rating. If you've defined minimum rating is zero and you've defined max. I mean, you could. Or like, like, are you able to use like glue inside of it? Like a glue. I'm not sure. Maybe I'm sure there's a package out there for that. I'm not sure if a cert that could have something like that. But that sounds. Nice. I don't know. Somebody go test using like glue inside of your messages. Your error messages. I don't know.
I'm sure someone watching this video months from now is going to be like, ah, yelling at the screen. Cause they know. And I don't, but that's okay. There's so much.
Introducing cli
I don't use apply a whole lot, but I'll just give you like an example. Like if you have, if you see like a nice. Message. Do you want to describe a client? cli is command line interface. As a. As an abbreviation cli.
Or not abbreviation. What is it called an acronym? And then the cli package is one that I. I've never used and have major FOMO about, but it. I know that it helps you with having clear. Error messages, things that show up in your like. Console. When you do things the wrong way.
Oh, it meals in the chat. He's going to help with cli. Emil says. For messages. Like it has cli underscore abort as a function, which is similar to. rlang. And it has all sorts of things. So lots of helpful things built in to make your. Your error messages. Slash potentially warning messages. Make more sense and be more useful to your user.
Isabella also put in a link to its progress bar because you can have it. Like if you've ever done something in R and you have like a little progress bar. That's loading and interactively like telling you how long things are happening. That could be a cli.
And then Ryan had mentioned. That a lot of rlang functions could be good helpers for, for these checks. Yes. Correct. rlang is very mysterious and powerful. Like the internal. Ah, okay. rlang functions. Yeah.
I think that cli might sort of be a little bit more comprehensive. rlang. And more well-described, but probably doesn't cover every single thing. Yeah, I'm not sure what, which one specifically rlang. I'm curious.
Reusing tests and mocking
We had another question from Jonathan that says, how do you think about grouping tests so that they can be reused in other contexts? Often I write lots of very specific tests, but I want to use the same concepts for the next project. Good question. Do you ever reuse your tests, Jackie? Or like pack them up?
No, I haven't. Good question. If anybody has, and you have a good example of that, let us know. Because in programming, we're like the dry principle, right? Like you do not repeat yourself, but we're sitting here like copying and copying and copying these. It's okay for unit tests, but it's a fault. I don't, I don't have an answer response for that. James had a good response, which was you could, but then there's another point of failure that would benefit from testing. You'd be testing unit tests.
Oh, Elliot, this is a great point. So Elliot said some of his favorite things about testthat are the mocking functions, which allow you to substitute code for a function that you don't want to actually run inside your test. Like if you ping an API within a function, then in your test, you can tell it to swap that function call for a dummy data set that replaces what the function would have downloaded if you don't want to go ping that API a million times during testing.
Like if you ping an API within a function, then in your test, you can tell it to swap that function call for a dummy data set that replaces what the function would have downloaded if you don't want to go ping that API a million times during testing.
Yeah. I know it works something like this. And I think it's dot package. And if it's like DS dot lab, like we expect this output to be. It works something like this. So for testing a function inside that function, like the child function has run API instead of running that function, the child function, we're going to say the output of that is going to be this. And dot package is my this current package. You can also be other packages, functions from other packages as well. Perfect. Something like that. Yeah.
There was also a good resource in the chat, by the way. With R. From Riffamonis project. Oh, yeah, that's a good one. You can mock like environment variables, file paths. Other things. The local mock bindings. I realized they actually just beefed it up recently. Like before, I was like, don't use this. It's in. We're testing this function. It's new.
Oh, Ryan's teaching us things about cli. So cli and initialism. Well, cli and acronym initialism. The first letters of a word pronounced letter by letter, like FBI, or an acronym, the first letters of words pronounced as a single word like NASA. Thank you, Ryan.
I like to use cli sometimes when I'm just running some analysis, like report, and it spits out certain things along the way. For me, it's like I can just like do data checks or something like that. Prints out progress of things. We love progress bars.
Also, there was a little bit of conversation in the chat about custom assertions. And there are custom expectations inside of testthat, which Emil has linked for us, which is great.
The ensure add-in
Jackie, in the last few minutes, would you mind sharing like how you use AMI? That's right. Yeah. So I forgot. So one way is through a package called Ensure. And it's by Simon Couch. In the back end, it uses Elmer. And you can use it right inside the IDE. So I have it already installed. I have my API key already like connected. So what you would do is you would highlight a function. I go to add-ins. And what it will do is go to the bottom. It'll open up like the test file, and it'll start spinning out unit tests for that function.
So I'll just go here. And it only works if you're not on Zoom. Okay? I swear. As many things in life. I'm probably doing something wrong. Oh, I closed the test file. I don't know. It should. There we go. It did work. See? It'll spit out a bunch of unit tests for you.
Whoa. Wait. Okay. Can you run that back and say it like one more time? What you use and what you did? You just highlight the function. And you just go to add-ins, ensure, and it tests our code. It'll open up the test folder. Don't type any keys because it'll mess it up. You just want to wait. And you needed to close it because we already had it open, right? I don't. Yeah, that's what I did. There must be some function behind the scenes. I don't know. That's like open the file. And so it added it in after our cursor, whatever we had for that. Yes. It'll just spit it out at the bottom.
Did it add useful stuff? Or did it do anything that was just really similar to what we did? So it tested some other number values, like 4 and 6, and made sure that its categories were correct. It tested a rating above 10. It usually spits out more unit tests than this. I'm kind of surprised. Is there anything else under there, like if you scroll? Let's see. There's only three, so.
Maybe we can go to, do you have another folder of, or file of functions that we could test? Oh, gosh. Ensure against and just see what Ensure does. Sure, this is a basic. Deplier code, all right. Yeah, very basic, summarize something. All right, so it's opened our test file. Did it read it? Let's see. Or is it still thinking? Oh. It's thinking. There you go.
So, like, it's really nice of giving you, like, a test little data set that you can use. You don't want to, you generally don't want to run tests against, like, an already data, like, a data set you already have, because you're testing the code, not that data set. So it's generally better to write your own little dummy data set. So if you have an enormous data set, testing can end up taking a really long time when you don't need it to. So another good thing is, like, if you do want to use a chunk of your data set, just use, like, five rows of it. Extract those five rows, put them into a little data frame, and then just use that. Yeah, that'll pass. I was doing this, like, a year ago. I was writing, like, family tests. I'm like, am I doing something wrong or are they doing something wrong?
So that's really useful. This add-in is really useful. Simon writes all kinds of amazing things. If you're ever looking at a package and you're like, is this a useful thing that I've never seen before that's really obscure? It's probably Simon. Simon or Garrick.
Yeah, I think on the back end, I think he wrote, like, a little, like, like a skills to write unit tests that you can probably edit. This is bound with Elmer, and which model are you using to power Elmer? It says to use CloudSonic 5. Okay. So does that mean that you in the background have already set up Elmer and connected it to CloudSonic 5? I set up an API key, yes. Just in case anybody is wondering. I did. I have a paid subscription and set up the API key. There you go. That means that if you have a local model that you are finding really useful, you could set that up, bind that with Elmer behind the scenes and have that operate your ensure package.
Positron agent skills
Okay. I know we only have two minutes left, but Isabella says that she has a skill for testing R packages that she found in the positive skills folder. Isabella, you want to point that out? I don't know if you wanted to share your screen. Oh, sure. Take it away. We have a ton of skills that you might not know exist. Maybe Isabella can share a little bit. We often get questions of like, how do we find those? Where are they?
So this one is from the positive dev team. I just put it in the chat. And so as you can see here, it's organized by different folders. And if you click into one folder, the one I clicked into to find this one is our lib. There's one R package development. And then so skill files are markdown files that give instructions to your AI agent to do something like, particularly if you have something repeated, for example, testing R packages at the very top is like the name, the description, and that's what this agent looks for to invoke the skill, like decide whether or not to use it.
And so the really nice thing about that is like, it's not going to like versus if you have an agents.md file, which is always going to be read by your agent. Skills are only read when it decides like, oh, this is an appropriate thing to invoke right now. And then below that, like it has like the information for your agent. And usually you write skills using AI for AI. So maybe not the most like human friendly thing, but, you know, it's, it lets your, your assistant know exactly what to do with this particular skill. When you're like, I'm writing R package test this, and then I'll load it in and then, you know, check it against whatever you have written in your actual markdown file.
Nice. And Connor had asked, does Posit Assistant know to go get these skills automatically, or do we have to point to them? Yeah. So I believe the ones from this repo are already automatically installed in Posit Assistant. If there's a whole bunch of other skills, I think if you go to agentskills.io somewhere, there is like some repository of like a whole host of other skills. And usually what I do is like, I just give Posit Assistant the repo URL and I'm like, read this in. And then it adds it. You can have skills on the project level. So if you only need it for a particular project or at the user level, so if you know that you're going to be using a skill across different projects.
Perfect. And also Garrick Aiden-Bouie joined us for an entire episode of the lab on agent skills, where he talks about that entire repo. So if you're curious about that, we will put that link in the discord chat as well. So you can go watch that episode because Garrick is just amazing.
Everybody, please say thank you to Jackie for having this like interactive conversation with us about let's just like expand our brains a little bit, do a little bit of exploration into unit testing so that the about half of us who don't do unit testing in R can now start exploring doing that. And we know about the testthat package. We learned about the usethis package. We learned about cli. We learned about using ensure if we want to, as an add-in in RStudio powered by an Elmer binding, so many freaking cool things. Jackie, thank you for hanging out with us. Thank you for being a part of the data science lab crew. I hope you had a good time.
Thank you. Absolutely. All right. You can find Jackie on discord. She is around. And then also usually in the chat at like the data science lab on Thursday, or the science hangout on Thursdays to part of the whole data science lab and hangout crew. Thank you everybody for hanging out with us. We love you. We'll see you on Thursday at the hangout or next Tuesday at the lab. Bye.