From the course: Building Data Apps with R and Shiny: Essential Training

Single file apps with shinyApp

From the course: Building Data Apps with R and Shiny: Essential Training

Start my 1-month free trial

Single file apps with shinyApp

- [Instructor] There are two types of Shiny apps that you can build, Single-file or split-file apps. Let's take a look at single-file apps first. To do that we need to go to our exercise files. We'll navigate to our exercise files and click folder 02_01 and we'll open up our RStudio project. In our files tab, we can see we only have one file, app.R. Let's open that up and if you've ever seen a Shiny app before, this will look quite familiar to you. On line one, we're loading the Shiny library. Lines three through 11 we're defining a UI or user experience and lines 13 through 18, we're defining our Shiny Server function and then finally on line 22, we're running our Shiny app by using the function, shinyApp. Notice that our studio has figured out that it's looking at a file that contains a single-file of Shiny app and it's given us a button here called Run App. And if I click on that, it will run our Shiny app in a new window. And just to demonstrate to you it is a Shiny app, if I move this slider, then the chart changes. Okay, let's close this. So it's worth while noting that there's actually a snippet built into RStudio to make it easy to build these single-file Shiny apps. Let's bring up a new script file by pressing Control + Shift + N and in here we'll start typing shiny and we can see in the auto completion menu we have shinyapp and snippet and if we click that then we have a template Shiny app built for us. Now you know what I mean by a single file app. It's a single script file that contains all of the elements to build and run a Shiny app. Let's now take a look at the advantages and disadvantages of using these single-file apps. Single-file apps are very much ubiquitous. You see them absolutely everywhere. For instance RStudio is dedicated website for Shiny almost exclusively uses single-file apps in tutorials and examples. Similarly if you're looking for help on Stack Overflow, you're more than likely to see solutions using single-file apps. This is because it's much easier to write tutorials and solutions using single-file apps than split-file apps. The primary disadvantage of writing single apps is that very quickly, your file will become unwieldy and difficult to manage as you add more and more features to your Shiny app. It's important to keep a mental separation between what exists in the UI and the server components of the app. In Computer Science we call this separation of concerns and it's extremely relevant in Shiny apps because both the server and UI parts of the app depend on each other's history. It's important to remember that there can be some issues when deploying single-file apps to shinyapps.io or Shiny Server. I highly recommend that if you're writing an app that you intend to deploy, either to shinyapps.io or Shiny Server that you always start writing your apps using the split-file app structure. This will prevent a lot of headaches and frustration from trying to convert a single-file app into a split-file one.

Contents