Reactive Web Dashboards with Shiny Transcripts
Chapter: Hello Shiny
Lecture: Adding user interactions
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Okay, so that's great, we have the data in there, but it's not over here in our dashboard.
0:06
And to get it into the dashboard or the application, we need to use these render decorators.
0:12
There are lots of render decorators, so if we start with pulling this in, you can see
0:17
we have data tables, plots, tables, text, UI, image, various different things.
0:23
And the important thing is that these need to match the output of the function that we're calling.
0:30
So we're going to define a function called data, and then we're just going to return the data frame.
0:38
So because this render decorator is, we're returning a data frame, and this render decorator
0:44
is expecting a data frame, then we get this nice looking table output in our application.
0:50
But if I was returning some text, I'm going to get something like this, an error that
0:56
says, ""Hey, I don't know how to render things of class string,"" so you need to either use
1:00
a pandas data frame or something that has a to pandas method. So you'll get this nice, fairly informative error, but this is something that when you're
1:08
getting started and trying to match these things up, it might cause you some trouble. But let's return that data frame.
1:17
And so now we have this rendering a data frame. And again, it's a little bit interactive, but maybe this is kind of a little stock,
1:24
I can sort and filter this dashboard. But if I want to include another interaction, I need to use another object from Shiny Express, which is input.
1:36
And inputs are things which let the user interact and generate some value.
1:42
So again, if we go to UI, we have this, in the UI object, we have various different inputs that we have.
1:51
And so there's radio button, select, select eyes, slider. And I'm going to use a slider here.
2:00
And we're going to use the slider to give the body mass of this, and then filter the
2:04
data frame based on the body mass, which is over here, body mass G. The inputs all have a couple of arguments which are important to be aware of.
2:14
The first is an ID, and that's the ID that's going to let us refer to these inputs in our render function and use them.
2:21
The label is what's going to be shown to the user, usually, for most of them. And then there's, for each one, there's different specific values.
2:28
So for example, in a slider, there's a min and a max value. But for a text input, there wouldn't be a min and a max value because it's not a numeric.
2:35
So we'll start this with, say, 2,000. The max value, say, is 8,000. And the set value is going to be 6,000. And let's see where that goes.
2:44
Now we have a slider. And the slider is, you know, we can move the slider. But it's not doing anything for us.
2:52
Like, this is not connected to any particular thing. So in the render function, we can get the value of that slider by calling input_mass.
3:00
And I'll say print_input_mass. Now if I look here, when it's initialized, the value of that function is--or the value
3:13
of that object is going to be 6,000. And if every time I move it, this render function is going to fire and print out the current value.
3:23
And one important little thing about this is that all of the inputs in Shiny are callable objects. So you need to have brackets at the end of them.
3:29
If I try doing this and I just have input_mass, that's going to refer to kind of like the reactive object it's called. But it won't give me the value.
3:38
Like, the value is returned by calling that object. So if you ever sort of have your application not behave the way you think it should be
3:45
behaving, and you see this Shiny reactive value thing that maybe you don't totally understand, Just check that you've included all the braces there.