Transcript#

This transcript was generated automatically and may contain errors.

Welcome back to the Data Science Lab, everybody. My name is Libby. I do community stuff here at Posit, and I am joined by my co-host, Isabella Velazquez. Isabella, would you like to say hello?

Hi, everyone. Thank you so much for joining.

Isabella is going to be in the chat in Discord, putting so many links and resources. Please join the Discord. As a reminder, we don't chat in Zoom, we chat in Discord, and so make your way over there. There are links in the chat that Isabella will be continually putting in there every once in a while to help you get to the Discord. If you have any questions about getting to the Discord, or you have any questions today and you cannot get to the Discord, the Q&A in Zoom is open. Okay? All right. Today, we are talking about testing in Shiny for R. We are joined by our lab manager, Barrett Schloerke. Barrett, would you like to introduce yourself, and then go ahead and share your screen, and we can get started.

Sounds good. Hi, everyone. My name is Barrett Schloerke. I am on the Shiny team at Posit. I've been on the Shiny team since 2018, and I think I've been involved with the R language almost 20 years. Oh, man. Almost can buy its own alcohol, legally.

So, it's been lots of fun. You know, there's been fun dollar bets along the way of never guessing R would be on the internet in Shiny Live and other things, and it's fun to lose those. But today, we're going to talk about testing, and testing specifically for your Shiny applications. We're very excited. Isabella just put a poll in the chat asking if you've ever tested your Shiny apps. I would love to know. I'm going to vote yes, although my yes is like a soft yes, because I haven't done it very well. I will also say, I forgot, I wanted to do one piece of housekeeping, so I'm going to interrupt Barrett really quickly. One of our community members, Colin, had shared that his university,

there's a conference that I don't know much about, but I want to share about, that Colin shared about. The short name is OTESSA, O-T-E-S-S-A, which stands for Open Technology and Education Society and Scholarship Association, and there is a call for proposals open. So, we are going to share some of those links so that you can check it out, so that you can look up the online conference. I think it's May through June next year. It looks very, very interesting. So, go have a peek at that. Look for links in the Zoom chat and in the Discord chat. Okay, I'll stop interrupting. We'll go back to Barrett, who also, by the way, is the author of reactlog, if you've ever used it. That's the kind of Shiny testing that I have done. I feel like I haven't done it in a very long time, and reactlog actually has some upcoming updates in the future, so maybe Barrett can mention that, too.

Sounds good. Yes, we'll get back to reactlog at the end as a bonus.

I added in another poll in Discord, because I see a lot of, have I ever tested a Shiny app, of no. So, I added in a poll of, have you written testthat test? That way, I know how much background to go into.

Okay, so we have about half of people saying no, I have not tested my Shiny apps. I would say that you have tested your Shiny apps. If you have even gone so far as to take out a piece of your Shiny app and run it on its own and just make sure that it works, I think that that's considered testing. It's maybe not writing a Shiny test or a testthat, you know, piece of code, but if you've tested to make sure that something works, or if you've used reactlog, I think that that's considered a little bit of testing, too. You're looking at the reactivity, making sure that you don't have any sort of orphaned, you know, things in your app that you don't want to test, and sort of orphaned reactive processes that are, like, going nowhere, or maybe they're going multiple places that they don't need to go. I think that's a little bit of testing, too. Okay.

So, what do you think, Barrett? Mostly nos.

Yes, and I will say the poll on Zoom has 71% no for Shiny.

We're going to learn lots today.

Introduction to testthat

Um, okay. So, let's go and do some groundwork of it works so far. Yes, Nathan, that is a print statements is actually, like, my first debugger when trying to understand code. Print everywhere. Print all the things. Hello, one, two, three, four, all the stuff.

Okay. So, diving into it. testthat. testthat is a R package that is written so that you can write different unit tests for your R package, applications, or just other testthat code, which shinytest2 leverages for Shiny applications. One of the, you know, the building blocks that's there is expect equal. I think most of my unit tests within my packages are, hey, does the object that I have created with my function, does it equal the expected object that I know it should equal? And let's just making your faces bigger now that I'm sharing the screen. So, I don't use any of the other arguments. I just mainly have the object that I'm interested in and the expected.

And then let's look at the example for make my screen a little bit bigger. An example for this. So, if I have my object A that I made with my function, which is nothing. So, now I can say, hey, expect equal that my variable A is 10. If I run this in the code, it does in terminal or the R session, it will not throw, which is great. And that's the goal.

If it has a testthat, yep. So, we're good. But if I change it, say, like, oh, my something is not letting me type. If I change it and I say A is 11, then if we say expect equal, it does 11 equal to 10. No, I expected A to equal 10. It throws. It tells me some context around how bad of a mismatch it was, which is newer. Back in the old days, there wasn't even that. It was just, hey, this is wrong. Fix it. Now, waldo, with waldo compare underneath the hood, gives a much cleaner exposure as to what is actually failing.

Now, waldo, with waldo compare underneath the hood, gives a much cleaner exposure as to what is actually failing.

That's really nice when you have things like lists. I think I can do this here of, like, expect equal of a list of A equals 1, B equals 2. And I'll do a list of A equals 1, B equals 2, C equals 3. And let's see if it shows up. It will be, hey, length mismatch. Comparison on the first two components. So, it's nice that it tells me what's different. And then if I make this different here, it will also say, like, oh, component B of your list is different. So, it's not just that your object is wrong. It tells you exactly where.

I love it. This building block. Yes, yeah. waldo.

We had a question in the chat about what waldo was. So, I wanted to put it out there so that we catch it really quickly and just talk about it live. waldo helps you compare things. And if you are a teacher and you've never used waldo, super great to be able to compare, like, student outcomes to something that they should expect.

Snapshot testing

Okay, perfect. Thanks for getting that in there for me.

Okay. So, expectations. We're good there. Let's go back to the docs. So, we're comfortable now with expect equal. Another part of testthat, which is added more recently, is snapshot testing.

So, snapshot testing looks like, it's a unit test, but what ends up happening is that when you expect a snapshot on a value, let's say x, the expected value is not present. It's not our object comma expected. It's just our object. This is different in that the snapshot goes to, let me see if I have it in a package. So, in my package, I have snaps, and I have the expected value here goes into a folder of snaps. testthat snaps, and then your expected value lives here. These are ordered. So, if you delete a test, it will get upset saying that your snapshot file has been updated. And then I believe, let me see if I can find the corresponding file.

Test app download and expect snap. Nope, it's not app download. It's

And these are all in a testthat folder that gets created, right?

Correct.

Okay.

I don't have it inside shinytest2. So, this is a little bit embarrassing for my quick demo. So, I'll go to the docs. In the example, though, it, yeah. So, it'll make a test file like this where the values go inside the markdown. This is really nice for things like error messages, warnings, things that are a little bit softer. But it does get a little unwieldy if you're comparing against, let's say, a JSON object. And this JSON object can be really big.

So, if we were to use something like a JSON comparison, if I were to take, like, the penguins data frame and, you know, send it to JSON, do we have that one here? Do we have that one here? Library. Stop.

Give me a second. Install. So, penguins JSON light to JSON.

So, if I have a string comparison, like, this is not fun to put inside my expect equal. Like, no one wants to read that while testing code and comparing against it. So, instead, it would be better to compare against the current snapshot of this output, and it will save it to a file.

So, let me come here and I could say testthat expect snapshot of Palmer penguins, penguins, and JSON light to JSON. Yeah.

Yeah. And this snapshotting in this context that you're talking about is all strings, right? Comparing a string to a string?

Depends. There is actually different expect snapshots. So, let's see. Is it in the vignette or is it in the reference? And I'll just go there. Because a bunch of JSON could definitely be considered a string, right? Yes. There we go. Maybe I'll put it inside.

Where do I where can I set the addition quickly? Addition require.

Yeah, this is new to me, but also I'm demoing and not inside my normal package environment.

Everybody, are your candles lit? He is going rogue.

I am not doing well. Addition require expect. Man, talk about embarrassing. Okay.

It's okay. You can ignore my question about strings and snapshots. I think that's probably the easier thing to do.

Yeah. Let's do local edition 3. Ta-da. All right. So, what this did is and I hope it saved. No, it did not. Because it's not inside a test mode. So, I believe it just prints to the page.

And this is way too much stuff to talk about. So, then once I'm done, I'm going to go ahead and one more time.

Ryan says testthat now has you opt into the third edition to unlock extra features.

All right. So, it says I can't save it because it's testing interactively. But what this what it would do is this output would show up inside a snap file. Snapshot file. Where it would say the code that ran to create the snapshot was my JSON head of the data frame. And then it outputted some JSON string. And then that would be in the middle of my snapshot file. This is great because that text that I just highlighted could be very unwieldy and it could be very long and it's not something you want in your unit test where you want to be able to scan very quickly as to what's going on. If you were to put it inside another file so that you just have all of your values and you were to read them in and then just compare against your expected value, essentially at that point you're doing snapshot testing.

Snapshot testing

We can kind of push this a little bit more in that you just like I don't really care about the code. You can compare against the output. And we can put it in a file. Not a markdown file. We can actually save it as a CSV, a JSON file or even a parquet file or an image. Something that doesn't really serialize inside a markdown. So testthat would have expect snapshot file. And a snapshot file is the extension now of snapshot. Where is it? Snapshot file.

And that we give an existing file. And then instead of a value, we have the existing file. So this is really nice for something like an image. Right? So we could do png of demo.png plot 1 to 10. 1 to 10. Dev off. So now I have my png. And then I could say testthat. Expect snapshots file of demo.png. Like this is what I would write in my unit test. If I was writing a unit test. I'm going to guess it doesn't work because it's interactive. But that's what it would look like inside my test.

Great. Okay. So we have kind of set up expectations, comparisons. We've set up waldo to tell you what's going wrong. We've done snapshot. Extended it into a snapshot file. And I have demoed that snapshot files don't work. Because I'm running them interactively and not in a file.

How dare you? Well, good news is, Brian had asked in the chat, maybe we should have prefaced all of our polls by have you ever even made a Shiny app? Which is a really good point. If you've never made a Shiny app, I don't want you to worry today. Because Barrett's going to be showing the quintessential starter Shiny app, I think. Yes, exactly. Which is the old faithful app. Which is something that is a histogram with some really simple sliders. And so this will probably be at least a good intro to a Shiny app and interactive visualization as well as the testing part. So I don't want anybody to panic that the Shiny app will be overly complicated.

Oh, no. Can I run this one directly? We'll find out. No.

Okay. So now I have a test file that I'll be rerunning. Because if we do it interactively, obviously, it's a unit test, we want it to run again. So we need to save it in a file. Okay. So this then created my snaps folder. And now I have a test MD, where it said, Hey, here's the code. And then it works. So inside my test folder, there is no, like ugly comparison, I can do things quickly. It's no big deal. It gave me a warning said that it made the snapshot for me. And here's the value since it's plain text.

And if I run — Yeah, I was gonna say, let's make sure we understand what we ran to get there. We ran test file. Okay. Yeah, testthat test file. And then this one ran again, since the snapshot was perfect. No issues. But always to have fun. We change it. And see if the test fails. And now it's like, Hey, wait a second. Something broke.

Look, there is there and the output is wrong because wasn't what is it was expecting.

So you can just to finish this out. Yes, you can run snapshot review to look at things. Odd that it's not showing it, but it shows a diff viewer of the two things. And you can say, accept, deny, or ignore. And it's, it'll, it's, it's nice to review it a little bit. And I think we saw that with Jackie, where Jackie changed something, and she was able to like, click review and accept or decline it. And nor had asked in the chat. I'm presuming skip means a test that was told by the user to skip. Yes, there is a it's in the code, not just because I felt like not testing it, but it's like testthat skip. Usually I have things like skip on CRAN. That's usually what I'm running all the time. And you'll get a message.

When you run your test, that's like, Oh, skip on CRAN. Oh, it's not skipping on CRAN, but because it's not CRAN testing. So, skip.

Barrett, my favorite. Skip tests with the reason Barrett at this location.

Ta-da. Skipped is good in that it won't delete your snaps. Skipped is good in that it won't delete your snaps. If there are snaps for that test, it's a temporary skip versus like, the test does not exist anymore, then the snapshots may be removed. So I highly recommend if you use git, check in your snapshots as soon as possible. Otherwise, they may disappear and you have to validate again. That's not fun. I did not know that. That's great to know. Okay. Skip is important.

So I highly recommend if you use git, check in your snapshots as soon as possible. Otherwise, they may disappear and you have to validate again.

Yeah. It's a little spooky. Yes. Like, let's just do and say, what's it? Expect equal. True. True. Right? Save. And if I do it, does it maybe not with test file, but with test package. Oh, yep. Look, all the snaps gone. Folder's gone. Doesn't exist. Wow. The whole folder's gone. Okay.

Yeah. So, we need to be careful about that when moving things. So, if you use git, commit before you just start nuking tests.

Testing a Shiny app with shinytest2

Okay. Awesome. Okay. So, shiny stuff, right? We got half our time left. This is the old faithful app. I'm not going to go into it too much. But skimming the code, we have a page with a sidebar. Inside that sidebar is going to be a single slider input. And then outside of the sidebar will be a single plot output that will display a histogram of old faithful's waiting times. So, it's just a slider and a histogram that are paired together. Okay.

There she blows. Old faithful is a geyser in Yellowstone National Park, by the way, if anybody is not familiar. Still ticking. Still hasn't blown up all of Wyoming. So, yeah. So, here's histogram. And as I change, then the bin count of the histogram changes. From shiny's point of view, that's the goal. Change the input, the output automatically updates.

Okay. So, as Libby kind of talked about earlier, this is yeah, we shouldn't jinx it. As Libby talked about earlier, the you did like I just demoed some manual testing. I know let's say we know that if we go to 45, there is a gap near 60. Like that's a nice little marker versus like the other ones, you know, it's filled in when it's a low value. So, if we put it to 45, then we can expect a gap there. Let's try to make that our test. It's nice manual test, but one of the things we have to remember is that like writing a normal R, regular R code, you are trying to, I would say, dummy proof your code for you in six months when you've forgotten everything. Or if you work on a team, you're trying to dummy proof your code so that as team members add new features.

I know I don't do the best at putting down all of the neat things that I've done, but if I have a unit test to verify that they're there, then I can make sure that they don't regress or they don't disappear over time. And this is great because, you know, my memory is not the best. And especially as we're moving faster, we need, you know, we need to have these regression tests. I would also like to argue that unit testing applications or your code, especially your code, is really important because this manual test, yes, I fully agree that by the time I hit the play button, I come over here and I change it to 45.

Great. I can see the histogram. There's the gap. I'm happy. Great. Rounding up. This took 10 seconds if I know what I'm doing. Rounding up. But this was one feature. How many features does your app have? I would like to wager typical apps are in the hundreds. There's small, little, minute features. This is blue. This is a title. This is number of bins. This updates. The 50 disappeared when, you know, we got close to it. There's a bunch of little things that happen. We have a sidebar. The sidebar can collapse and shrink, and I can click the button. Like, I'm already now at 20 features, and if I was to retest each little thing to make sure it happens at 10 seconds apiece, all of a sudden manual testing becomes no fun. Especially if you're doing it for every code update.

Some people might like it for job security. I find that mind numbing and hard to manage. Plus, I forget because my working memory is, what, five, six items? So, it takes a lot of effort for me to remember all 20.

I find that mind numbing and hard to manage. Plus, I forget because my working memory is, what, five, six items? So, it takes a lot of effort for me to remember all 20.

Yeah. So, yeah. I forget to even test. I'll get to that part here because shinytest2 makes this easy. In that shiny test, this is, I'm gonna give you guys cake and then we're gonna walk through how that cake is made. We have a thing called record test and it defaults to the the app in the current directory and let's just do that. Let's record a test. This will open up a browser for me. There we go and it is my shiny application here and what we did is we set slider to 45 and then I'm going to click a button that I can say expect some values which will do some

JSON comparisons and then let's also take a screenshot just for fun and then let's save this because we're done testing for right now. Let's save it. I don't need a random seed right now because histograms are deterministic but you could set a random seed so that your, you know, our norm and our UNIF are all deterministic but I'll save test and exit. This will then take it, run the test that it made from the recording. It'll tell you that it's warning, hey, I've made new snapshots because we're actually going to store some of the expected values as JSON and the screenshot as a PNG and this is all handled manually. This is why I kind of fumbled beforehand doing it myself in the consoles because the recorder does it for me like I don't, I don't do it myself but what's neat is now next to my app.r I now have a test, shinytest2 just happened to be called and I have a file or a section called DSLabShinyTest2 which is what the name was called and it set the inputs to 44 then 45. We call expect values and then there's a screenshot if I go to the app and I even change this, hello DSLab, save, and then we can test this.

You can use normal testthat functions but shinytest2 made this easier of test app. So we have record, test, and test app.

And Nathan had a question about that in the chat as well. He asked if record test includes a call to run app and maybe same question for test app. Both, both will. Let me finish the screenshot and then I'll go back to the run app. So since I changed the title, this now says hello DSLab down here instead of hello Shiny. So let's see if, oh good, it does work. All right, so this time the snapshot came up as something to check and if I toggle it, I can click and look, we can see the title changes. And this is so awesome because one, we can verify that there's a gap at 60, so that's great. But two, we see that our title changed and this is why Libby put in socials that a screenshot is worth a thousand tests.

a screenshot is worth a thousand tests.

Because how many things are being tested at this exact moment with this one screenshot test? That is a Barrett quote, not me. Barrett says the screenshot is worth a thousand tests. I love it. I think that the, the little toggle thing, I don't know how you opened it when it first, when you first opened this test, was it there? Oh, it was in difference, difference and then toggle. Difference will highlight red where the pixels are different, so you can kind of ignore the foggy one, which is nice, but you're like, what is that word? I can't read that. And then you go, oh, toggle. I like to click really fast and then it tells me where it's at. If you need it, you can also do a slider, which is really useful when things like your font size changes and it's like two pixels that are upset. I didn't know this existed. And from the like faces that I'm seeing from people, they're like, I also didn't know this existed. I didn't know you could do this. So this is snapshot reviews within testthat it is, this is this particular viewer is not shinytest2. It's this is testthat snapshot reviewing.

I like the new thing. So I'll say, except I like that one as well. So I'll say, except and then it's it. So now that's there. If I rerun, oops, if I rerun the tests with test app, it should pass because it'll say, hello, DS lab, which is what we changed our app to. So we have now tested, verified, adjusted, and then re-verified the test work. So now you can forget about it. You can go away. And then when you come back six months later, if you try to change things, like it'll stop you a little bit and, you know, give you a little bit of friction, which I think is nice because you don't want to lose all the features that you've done.

Headless browser testing with chromote

Sweet. And Hubert has a question on the present test. He says, can these tests run headless headless? He means headless browser. Y'all pretty sure. Yes. So they to answer both the run app and the headless one, we actually launch a background R process that runs the shiny app in its own R process, not the foreground. So any of your variables and things will not poison the shiny app. And then that app is then viewed by chromote, which runs a headless Chrome browser to view the application and render all the magical things that it does.

We did some niceties in there so that things like when you set your inputs to a certain value, we wait until the outputs update. So before moving on and releasing control. So we don't have to sit here and say, like, wait for plot to update. Like, no one wants to be doing that. That's annoying. So anytime you set inputs, we wait for the first output to be updated.

Nice. And we had wanted to verify in the chat what exactly headless means when we're talking about headless browsers. I'm not the best person to explain, but I'm going to try. A browser is rendering a bunch of, like, HTML, right? Which is values and text that you can actually check without having to have an image pulled up on a screen. And so you can actually test a bunch of stuff behind the scenes to make sure that all of those values exist without the head being that, like, image on the screen for the UI. Is that right, Barrett?

Mm-hmm. So a proof of concept. I have started my app here. I know there's only one input and output. Let's actually do let's adjust this. I don't want to do it on the fly. I'll just I know it'll be boring. So set inputs of bins equals equals six, right? So we'll do this. And if I do app dollar get values, this should show that we have a big plot. Lots of things about the plot. Whoa. It's not just how about we do it's too much stuff about plots. But we do get value input equals bins. And it'll say, oh, look, I know. But it's like, but where is the app? Well, you can be app dollar view. And it opens up. And this is actually what chromote believes is happening underneath the hood for your application.

So it sees six. If I kind of adjust this window here. If I oh, man, tiny screen. If I say app dollar set inputs bins equals 45. Because that's what we did. And try to get off of there. 45. There's the app running in the background. This is actually a headed view of the headless browser. I think that works only because it's Chrome. There are other browsers that took away all the machinery to make the headless browser faster. Like if you're familiar with agents browser or anything like that. But I enjoy chromote because of the amount of screenshot testing. And, you know, your user is most likely going to use Chrome as well.

Yeah, this is very cool. And we're going to talk a little bit more about headless browsing tomorrow, not tomorrow, next week as well, when we're talking about testing Shiny for Python apps. So if you are curious about even more diving into this rabbit hole.

Yeah. And Amelia had asked, does it use chromote by default? I believe so. It is we use chromote by force. We don't really have much option. And within chromote, if it's a Chromium based browser, you can change the browser that's being used. But we cannot use shinytest2 with, let's say, Safari. That's not going to work.

And if you're open to one more about chromote, Andy has a question here. Does the headless chromote instance tear down properly after testing is complete? Thinking about headless automated testing and whether chromote teardown is successful and doesn't lead to temp file bloat. He says, I have PTSD from headless chromote files creating half a terabyte of temp files and maxing out drive space. That's...

Is that a good question? Let me know about that later. Or in an issue, because if that's actually doing that, we don't want that to happen, like temp file dumping. Also, the computer should be updating it or clearing out that temp folder a little bit. But I guess that requires a restart. So, good question, Andy. Yeah, great one. We'll catch it on the back side of the lab and see if we can figure it out. It doesn't sound like it's a persistent thing that's happening that Andy is seeing, but more of just a question of like, I've experienced this before with chromote. Yeah. You can do this, though.

So, like, you can get access to the Chrome session if you want to do more chromote specific things yourself. It's useful. But, yeah, the temp file. I'm sorry that that happened. I like how Barry just has this stored in his brain. He's like, yeah, you can just type this code that I'm pulling out of my brain on the fly, and it'll be right and work. Autocomplete is wonderful. Still, you had to know it existed, right? In order to go back and pull it out of your memory.

Reviewing what we've built and writing tests

Okay. So, we have created a test using our live little browser view. We've created a snapshot, and we compared and toggled between two realities with just changing a title. What's next in our demo?

I think you're stating, you know, the story, like, I'll forget to even write the test. Um, this is where I will lean on the, like, I had, what was it, the fear of chaos gods instilled in me when I was little because my dad had, what, 128 kilobyte hard drive or whatever it was at the time that, you know, first computers had. And he had written up a whole document over a few days and never hit save. And autosave didn't exist. He lost it. So, it was, you know, if you ever have time to rest or to lean, you know, save your files. And so, this kind of goes towards the, what was it, McDonald's of if you have time to lean, you have time to clean. And so, I extended it in the past with if you have time to rest, you have time to record a test. So, hopefully, that stays in your mind.

if you have time to rest, you have time to record a test.

Yeah. I mean, I think everyone's lost progress, right? Unless you were raised 100% on Google Docs, you've lost something before. Actually, as you were talking about that, I was having flashbacks to, like, not catching the save code on the screen while playing Aladdin for Sega or something. And, like, not getting my progress saved because, you know, video games didn't used to have memory save either. You would just get a little code to get back to where you were. This is a long-standing problem for me. Oh, Lauren says, I've even lost documents with Google Docs. So, it happens. Even if you have a machine just, like, remembering for you.

Yeah. I, for apps, I think that I would miss all of the things that should be tested, right? If it's left up to me, the human being, I'm not going to catch all the things by hand that I should test. And that's why recording screenshots is very useful. Because with just a couple of screenshots, you can get pretty far. For the few people in the room that are package authors, shinytest2 did get updates. So, you don't need to install your development package to run your tests. And I added some magic in the last release of shinytest2, like, recently. Not a few years ago, but recently. It's also, if you write Shiny applications, there is built-in support for Rhino and Gollum. That should work out the gate. In your application, you just need to say, library my package. Or your package name in your tests. Or, sorry, in your app. If it's not already there. And then, for an example, with a package, you have test folder, test that. I have an app. That is, if I had a function, test package.

Test that. No. There we go. I have an app.r. And what it will do is it has access to both, like, there's an internal value and an external value in the package. And it's just this is proving that the app works as expected. That you don't need to install it. Because I know for years, package authors needed to install before testing. And I'm so happy to make that not a requirement now. I just want to spread that word. Can you read Elliot's Libby?

Yeah. Elliot had a question that says, do you have tips for writing tests for non-UI functions? I'm thinking of functions that do data wrangling, for example. Would you write those and include them in the project the same way you would for a package? And I think that this probably is more along the lines of what we talked to Jackie about, which is just using test that tests. But it's a good question in the context of a Shiny app. Because we are gonna have things like data wrangling and ETL and stuff like that that we have written that exist in our Shiny app that maybe have nothing to do with the UI.

Yes. I would revert back to doing expect snapshot or expect snapshot file within testthat. I would actually put those testthat tests, like, data assertions inside your data wrangling file to, like, you know, make sure no one is older than 200 years old. Make sure no one's age is negative. Make sure everyone's weight is positive. Like, some sanity unit tests in there. And if it fails, it'll throw and your ETL will stop immediately. I think that's a good thing if you have really bad data or, like, unexpected values in your data. So however you approach it is good. But it's just standard test that testing. Perfect. And I think that covers Nathan's as well. Because he was asking if the data changes, the snapshot wouldn't work, right? You do want it to stop and not just, like, keep going if it's failed. So having those test fails are important.

All right. Sweet. We have about 10 minutes left. What else do we want to talk about? Let's see. My item is working for reactlog. Oh, tiny screen. I'm, like, wondering what the size of Barrett's screen in person is.

This is a good poll. Isabella, pick a number. And then Libby, say over or under.

Like, any number?

Yeah. For a TV. How big is my monitor here?

Oh. I don't know. I don't know what a normal size monitor is.

I think I would guess that you have one of those, like, ultra wide ones that's, like, 50 inches wide. Nathan guessed 43.

43. Nathan gets a clean testthat testing bill. Because it is a 43 inch TV.

No way. Nathan, how did you do that? He's the only one who guessed you.

It's a TV, not a monitor.

Yeah, I have a.

Reactlog overview

Okay. We're going to talk about reactlog, y'all. Okay. If you have not used reactlog, I'm so excited for you to see this. Also, I told Barrett that I crashed my computer one time by trying to run reactlog on, like, the most complex shiny app ever that was not one that I had written. It was somebody else's app. It was too complex. And I absolutely almost broke my computer. So, hopefully, we will look at a reactlog for, like, a way smaller app.

Yes. So, this is what reactlog looks like. We have our inputs. We have our reactive expressions and our outputs. Reactlog is there to expose what is happening inside your application according to what shiny believes or shiny says is happening. It may not agree with what you are running. This is important to distinguish because it only is telling what's happening, not if it's right or wrong. When your app begins, all the input values are sent by the client. And then the client also then turns around and says, hey, these three outputs need to be calculated. Can you start working on them? And it will walk through step by step where it will say, oh, start calculation. I need this value. Oh, start calculation. I need that input.

And then it recurses up. But the nice part is it shows off that when we go to our next output, who says, oh, I just need this calculation here. And that since this is already done, it doesn't recalculate that whole tree anymore. It will show that it just adds a little dependency and it moves on. The flip side is it will also show you when an input changes how much work needs to be done when that input changes. This is important because if you have a dashboard that is dependent on something like your maximum time of your data, if that max time updates, your whole dashboard will probably need to be refreshed or recalculated, not necessarily refreshed. But if you just, you know, change a filter, maybe it's just that part and not the whole application.

So, now we can see if this input changes, all of these get invalidated. And then there's no more relationships. And then that loop just starts over again where it says, hey, I need to be recalculated. And then off we go. And it goes back up the tree, getting the values.

In this case for I think this one is a select. And then I output a plot given the diamonds information. This is an example of the app. All right. So, there we have a selector. I choose a variable. We display the plot. This is the reactlog of it where as we initialize the app, the output needs information. So, we get the input value. Then it updates the input value. It is invalidated. So, now we start invalidating downstream. Invalidation is done. Start calculation. Now getting dependency and done calculation. Complete. I'm glad I could do that. That was a lot. Was that just like just you changing the variable in the slider each time? That was just the single change of the variable. The initialization and then the single change.

If we did this for a bigger app, such as Cran Whales, which has a date picker, four tabs, plots, you know, a whole minute 16 of demos, we can see actually that its graph is much more complicated.

And much more complicated in what is happening. And here the date, if the date changes, then for all the downloads in this demo app here, if this date changes, then of course all the graphs should change because they're displaying download information given the date. So, that's why the date is all the way to the left and all the outputs are on the right.

Can I ask what are some of the for anybody who hasn't used reactlog, straight from Barrett, who helped make reactlog, what are some of the decisions that it can help you make when you look at your React graph? When you're like, okay, I have my app. Maybe my app has become a little convoluted. What are some of the decisions that it can help you make? I know on my end, I have like found abandoned things where I'm like, oh, this no longer attaches to anything. But then also it might be, I don't need this, or this is hooked up to the wrong thing, or maybe this is feeding into too many things and it's causing too much processing to happen and I need to cache something.

Yes. Let's go back to the abandoned part. These are actually multiple outputs and intermediate objects that are not utilized in the recording. So, they're calculating and then they're just going nowhere. They're not even calculating. They're just defined. Okay. So, like, I don't want to call it a zombie, but we can think of it as a zombie. They're just kind of existing, not doing much. But if you have more and more and more of these, like, it, you know, it takes time to set them up. It's not zero, but it's, or it's fast, but it's not zero. So, if you have thousands of these that are just dangling, like, eh, maybe trim a few.

So, if you have thousands of these that are just dangling, like, eh, maybe trim a few.

There are extra items where, in this case, Joe made a intermediate one of whales here. This needs a input, a date, an event reactive for date. It uses a bunch of different things. This whales took time to process. You can't quite see it. Like, reactlog wasn't really built for for showing, like, time duration. It was just more to build the relationships. If you are interested in time and memory usage, I totally suggest looking at profvis.

Oh, that's another good one.

Profvis is a wonderful application where you can run selected code and then it will produce a flame graph of execution time and also memory. Not in this demo, but it also shows memory.

Today has been good for finding out about little things that you maybe didn't know about, like waldo, reactlog, profvis. Definitely go check them out yourself, for sure. Since we don't have time to go through all of them in detail. Although, they do be great examples of, like, if you have a good example of how you use something like waldo or profvis or even reactlog, let us know, because we might be able to do a little show and tell.

Reactlog update sneak peek

Oh, nugget, nugget for the road. Let me find it.

This is work in progress. Do not hold us to any promises. But this might be coming down the pipe for reactlog.

Yay, reactlog update.

We might have a thing where we can, you know, take a group of items and just collapse it down. We, you know, that way we could do something like this for an output plot. Because if you look at a plot in reactlog, this plot here, downloaders hour, has a plot object, these client datas, extra client datas. No one cares about that. They just care about, you know, render plot. That's the important part. You don't need to look that far under the hood of Shiny. Instead, it would be really neat if we could have that be, like, oh, a output plot. Look, it's just there. You don't need to know anything, like, by default. But if you really want to, you can open it up yourself to see what's happening underneath.

Also, very slick UI.

Right. Shout out to Karan. He just went to town. And it's so much nicer and faster than old reactlog. But we still get our progress bar at the bottom. And you can go forward and backwards in time. So, still ironing out some minor details to match reactlog. But this will be coming.

Well, thank you so much for the demo. I'm, like, thank you. We're so lucky as a Shiny community and an R community and a Python community to have people like you who are working on making things better actively. When I first mentioned reactlog to Barrett for this episode, I didn't even know that he made it. He was, like, that's my package. I was, like, oh, my gosh, I didn't even know. Thank you so much. It's an amazing package. We have lots of, like, exploding head emojis today for all the cool stuff. Thank you for helping us dip our toe into what testing can look like in Shiny on a simple, simple basis without being scary. I really appreciate it.

No scaries for our testober. Come back next week, everybody. Hang out with Barrett and us again. And we're gonna talk about Shiny for Python, which is a little bit of a different beast. We test in different ways. We use different resources. We're gonna talk more about headless browsing, I think. And see you guys in a week.