• ## Summary

Search form
• 00:00

MARTIN JOHN HADLEY: OK, so we knowhow to embed HTML widgets into Shiny apps,so to have interactive charts, interactive maps,and interactive networks instead of Shiny apps.We also know how to add controls to our Shiny appsand for those controls to depend on data from our Shiny apps.What we need to learn how to do now is two things--

• 00:21

MARTIN JOHN HADLEY [continued]: first, how to control the order in which a Shiny app updates,and second, how to publish the thing to the webso that others can see it.So that's what we're going to do in this video.To follow along with me, I need you to find in the resourcesthe Reactivity and Publishing.rfile.And when he found it, double click it.

• 00:42

MARTIN JOHN HADLEY [continued]: That will open about our RStudio project.And we're ready to start playing with our Shiny app.So we can see we've got a server to R and a UI to rfile.Let's open both of those up.And before we look at the code, let's see the Shiny app itself.So I'll click on this Run App button here.

• 01:04

MARTIN JOHN HADLEY [continued]: And it's a slightly strange demonstration.But what I have here is I have a histogramand I have a slider that allows meto change the mean of the random data that'svisualized in this histogram.And then I have this text input here,which is called plot label.And that allows me to change the label of the plot.

• 01:24

MARTIN JOHN HADLEY [continued]: So let's do that.So I can call this Martin's plot.But you'll notice as I type, the datagets regenerated, whereas that's not what should happen.What should happen is when I change the slider,the data should change.But this control here should have no impact

• 01:46

MARTIN JOHN HADLEY [continued]: on the data being displayed.Well, let's have a look at the Shiny appand try and understand why this is happening.So in the Shiny app, we can see we'vegot my output object, which is calledhist, which is the result of vendor plot with histogram.Here we can see we generate with the function Rnorm 50 random

• 02:07

MARTIN JOHN HADLEY [continued]: numbers with a mean of input $mean and actually a standarddeviation of input$ mean as well divided by two.And then the main argument is input $label.The way that reactive expressions work in Shinyis this.Any time an input variable inside • 02:28 MARTIN JOHN HADLEY [continued]: of these brackets updates, the entire expressionis what we call invalidated.And then the Shiny app regeneratesthat code, reruns the code, and then sends the outputto the UI.So what happens is in our UI, we have a number of controls.And when they change, so when input$ mean or input $label • 02:49 MARTIN JOHN HADLEY [continued]: changes, that information is sent over to the server.The server goes, oh, my current value for vendor plotis now invalidated.I'll rerun that.I'll generate new random data and send it overto the UI as a plot.What we need to do is we need to separate out these componentsof the Shiny app. • 03:10 MARTIN JOHN HADLEY [continued]: And to do that, we need to know a little bitabout reactive expressions.So what I'm going to do is I'm goingto generate the random data outside of vendor plot.So I'm going to create a variable whichI'll call random data. • 03:31 MARTIN JOHN HADLEY [continued]: And I want this to update whenever input$ mean changes.So to do that, I'll use event reactive.The first argument of event reactiveis a vector of the input variablesthat you want this object to be dependent on.In this case, it's only input $mean. • 03:53 MARTIN JOHN HADLEY [continued]: So I type "input$ mean" and then a comma.And then I'll use braces because theremight be multiple things that I wantto happen in the body of my reactive event.But in this case, all I want to do is Rnorm 50 mean is equal

• 04:15

MARTIN JOHN HADLEY [continued]: to input $mean.And the SD, the standard deviation,is equal to input$ mean divided by two.OK, great.So this event reactive is going to update whenever input \$ meanchanges.And what it's going to do is it'sgoing to generate me new random numbers.

• 04:37

MARTIN JOHN HADLEY [continued]: So let's copy the name of our variableand put it inside of the body of render plot.Random data is a reactive object now.So if we wanted to call it, we wantedto run it and get its value, we would

• 04:57

MARTIN JOHN HADLEY [continued]: have to call it with open and close parentheses.This looks ugly as sin in my opinion.So what I always write is I always writethe name of my reactive event, assign it as itself.And now, I can take this name, and I

• 05:19

MARTIN JOHN HADLEY [continued]: can replace the first argument of hist with random data.Just get rid of that closing bracket there, save the file,see that all my brackets match, lovely.And now I'm going to run the app.

• 05:40

MARTIN JOHN HADLEY [continued]: So now what should happen is I should be able to typeinto a plot label and it updates only the title of my plot.So if I type "Martin John Hadley,"we can see it only updates the plot label.If I changed a mean, then we can see

• 06:02

MARTIN JOHN HADLEY [continued]: I get new data generated, OK?So now we've learned all three thingsto get a Shiny app working to be able to tell a story.We know how to embed interactive charts.We know how to create various different types of control,and how to have them dependent upon datadefined in your server.rfile.

• 06:23

MARTIN JOHN HADLEY [continued]: And now we know how to control the updatingof your reactive events.Now we want to publish our Shiny appto the web, which means we need to registerfor an account at Shinyapps.io.So let's head over to our web browser and to Shinyapps.io.

• 06:47

• 07:10

• 07:33

MARTIN JOHN HADLEY [continued]: to define your Shiny apps.So I'm going to choose Sage Interactive Viz.I'll get rid of those periods.So all of my URLs will be SageInteractiveViz.shinyapps.ioslash the name of my app.So, I'll save that setting.

• 07:53

MARTIN JOHN HADLEY [continued]: And I get a really nice greeting from Shinyapps.io.It says, hi, you must be new here.And the reason for that is Shinyapps.iocan tell that I've never connected RStudioto my account.So what it advises me to do is to installthe rsconnect package.The rsconnect package is what we need in order

• 08:16

MARTIN JOHN HADLEY [continued]: to deploy apps from the local machine to the web.So let's copy this code.Let's go back to RStudio.Just close my Shiny app for now.And let's type into my console "install.packages rsconnect."

• 08:41

MARTIN JOHN HADLEY [continued]: There we go.So I've installed a package.Now let's see what else I'm beingtold with the nice greeting.Well, I'm being told that I need to authorize my account.So we need to give RStudio the credentialsnecessary to publish Shiny apps to Shinyapps.io.Now, I don't want you to see my secret token

• 09:03

MARTIN JOHN HADLEY [continued]: because if you did see my secret token,that would mean that you could log into my account.So, what's going to happen is my super secret tokenis going to be masked out so you can't see it.But I'm going to click Show Secret,and I'm going to copy the text here.

• 09:23

MARTIN JOHN HADLEY [continued]: Then I'm going to hide the secret from you.And I'm going to go back to RStudio.And we're going to go to the Menu bar.We're going to select Tools.We're going to get to Global Options.And we're going to go down to Publishing.

• 09:44

MARTIN JOHN HADLEY [continued]: We're going to connect our account by selecting Connect.We're not going to use RStudio to connectbecause our RStudio connects very fancy and notwhat we're dealing with here.We're dealing with Shinyapps.io.I'll select that.And then I'll paste in my secret, which again, willbe masked so you can see it.And then press Connect Account.

• 10:06

MARTIN JOHN HADLEY [continued]: It goes and handshakes with Shinyapps.io.And provided the handshake has gone well,then we'll have connected Shinyapps.io our RStudioinstallation.And now we can simply press OK.Now let's run our app again.To publish the app, I select this button here, Publish.

• 10:29

MARTIN JOHN HADLEY [continued]: Because I've set my Shinyapps.io account up here,it's telling me that my destination accountis Sage Interactive Viz.And it's chosen automatically a title for my visualization,for my Shiny app, which is Reactivity and Publishing.I'm going to choose First Shiny App as the name for my Shiny

• 10:50

MARTIN JOHN HADLEY [continued]: app.Here it lists all of the fires which are going to be sent up.We only have a UI to R and a server to rfile here.But if you had data files, you wouldneed to deploy all of those to Shinyapps.io as well.You would also need to ensure that youload all of the libraries inside of your server.r

• 11:11

MARTIN JOHN HADLEY [continued]: and your UI.r files.That's because they're all going to beinstalled onto a version of R on Shinyapps.io in your domain.So, all we need to do now is click Publish.And then we wait for up to 60 secondswhile RStudio looks through your package,

• 11:34

MARTIN JOHN HADLEY [continued]: sees all the libraries that you're using,goes away to Shinyapps.io, installs them.When they're all installed and everything's OK,it then grabs all the files of your local machineand pushes them up to Shinyapps.io.So we can see currently we're at the Installing Packages stage.Now we're installing our files.

• 11:56

MARTIN JOHN HADLEY [continued]: And within a couple of seconds, weshould get a message saying that the current version in the appis being stopped.And then what it will do is it will launch our web browser.So in that dialog window before, we that littletick box which said Launch Browser.So that's what will happen when we're finished.

• 12:17

MARTIN JOHN HADLEY [continued]: So here, we have my web browser with my interactive Shiny appshowing.So we can see it works.I can move my slider along.I can change my label.And it doesn't generate new data.And here, I'll show the URL for my Shiny app.So if you go to this URL, you will beable to visit this Shiny app.

• 12:39

MARTIN JOHN HADLEY [continued]: So that's the very basics of reactivityand how to publish Shiny apps to Shinyapps.io.

### Video Info

Episode: 18

Publisher: SAGE Publications Ltd

Publication Year: 2017

Video Type:Tutorial

Keywords:

### Segment Info

Segment Num.: 1

Persons Discussed:

Events Discussed:

Keywords:

## Abstract

Martin John Hadley, Data Visualization Specialist at University of Oxford, explains how to control the order in which a Shiny app updates and how to publish to the web, including a discussion of reactive expressions and events, creating a shinyapps.io account, and connecting this account to an RStudio account.