Resources

Data visualization and plotting with Shiny for Python || Carson Sievert || RStudio

Shiny makes it easy to build interactive web applications with the power of Python’s data and scientific stack. Learn more about Shiny for Python: https://shiny.rstudio.com/py/ Check out our interactive Shiny for Python examples: https://shinylive.io/py/examples/ Content: Carson Sievert (@cpsievert) Producer: Jesse Mostipak (@kierisi) Editing and Motion Design: Tony Pelleriti (@TonyPelleriti)

image: thumbnail.jpg

Transcript#

This transcript was generated automatically and may contain errors.

And let's move on to something a little bit more exciting, like an app with a plot. So this application here is pretty similar, but now when I change this slider, it's going to change, I think, the number of bins that go into computing this histogram. So you can see as I increase the number of bins, this histogram is going to have more staggered to it.

So yeah, this one has a slightly more complex user interface. And notice as I make the window wider, this actually looks a little bit more like a sidebar than it does like an intro input panel or something like that.

So this sort of functionality is provided by this layout sidebar from the UI sub package. So with the layout sidebar, it really wants like a sidebar panel and a main panel. And so on the left hand side here, I have the slider similar to what we had before. Another output text verbatim on the right hand side in the main panel and then a plot underneath that.

Reactive calculations

And this example starts to introduce some reactivity concepts. So with this at reactive.calc, this is a decorator for defining a calculation that depends on some input values and then returns another value from some combination of input values. So this is like overly simplistic example that just reads the slider value and then multiplies it by two and gives this reactive calculation a name of R. So once I have this function defined as a reactive calculation, then I can read it just like I would read an input value inside of another reactive context like this rendered text output.

So this is, you know, for all real practical purposes, this is essentially the same as just, you know, putting this expression into the read of R. But for more sort of sophisticated reactive web applications, this is a really useful construct for, you know, encapsulating like a chunk of a reactive calculation, whether it's, you know, performing a data summary or, you know, computing a model from some data.

It's always a good idea to kind of put those steps into reactive calculations. Basically, it gives Shiny more information to do less work when it makes sense to do less work.

Basically, it gives Shiny more information to do less work when it makes sense to do less work.

So if you have like a big, you can define like a big reactive chain with like several calculations that depend on each other and if one of those calculations gets invalidated because the value is out of date for whatever reason, Shiny knows to not go back all the way to the top of that graph and recompute everything. It can kind of just say, I just need to do the last two calculations and then knows to do less work when it makes sense to. That is reactive calculations in a nutshell.

Rendering plots

And then to start showing you some different variations on rendering different types of objects in the UI, here's an at dot render plot. And these decorators can actually be used without parentheses or with parentheses. You can call it like a function. And that's particularly useful for something like render plot, which has additional arguments that you can specify like some alt text to put on this image to make it more accessible. Then, you know, you use it just like how you would a render text.

You define a function and you can give this function a name which should match an ID in the user interface. And then inside of this function definition, I can do something like use matplotlib to create a histogram where the number of bins in this histogram comes from the value of the slider. At least currently, the way render plot works, you're going to want to always return a matplotlib object and then we'll know how to render that object into an image.

Packages supported by render plot

I do also want to mention this render plot has the ability to understand more than just matplotlib objects. And I've actually whipped up this example app to demonstrate that. So I've at least laid out a few different packages here, but this is not... Render plot is not limited to just these packages. Essentially any package that builds on matplotlib or pill will be supported through render plot.

A lot of what a lot of these packages will do will essentially build on matplotlib like plot nine, which actually gives like a more like a ggplot2 type interface to creating graphics and they'll essentially like subclass a matplotlib object, meaning like they are a special form of a matplotlib object. So as long as your object kind of, you know, subclasses matplotlib in some way, render plot should be able to handle it.

So here's a plot nine graph that I've in the Shiny application, I've programmed to allow you to change like X variable, Y variable and color. We also support like Seaborn. You might be familiar with pandas plotting interface where you can essentially like from your data frame object called dot plot on that data frame object. And that will return, I think in like every case a matplotlib object. So you can just basically call dot plot and return that value and render plot should handle that.

There's also like HoloViews, which supports all these different, you know, it also has interactive visualization capabilities, but you can also use matplotlib with HoloViews, X-Array and GeoPandas. So lots of different packages out there that build, luckily build on top of matplotlib and you can all use with render plot.

So just to give you like a quick idea of what I mean there. So this is actually the source code for that application that we are looking at. So here's a render plot with Seaborn that's creating the scatter plot with the kernel depth density estimate on top of it. Plot nine was the more ggplot2 like interface here where I'm reading like an X input value and a Y input value. So whenever those change, Shiny will know to re-execute this function.

As I mentioned with pandas, there's like a dot plot. So you can call that and return whatever that returns. HoloViews it's a little bit less straightforward. You can use like this render function from HoloViews that takes in a backend argument. So as long as you specify, I want to use matplotlib, then you can use it with render plot. X-Array is kind of similar to pandas in the sense that it kind of has like a similar dot plot like interface from its special data structure. And that also returns matplotlib objects. Again, GeoPanda is a lot like pandas where you can just call like dot plot. And then MissingNo, I think also returns matplotlib object, this matrix function here.