Single file app
We can combine the code so far into a single file, e.g. called
What's new here is that we passed
returns the Shiny app object. This is passed to
runApp() via the implicit print method to run the app.
runApp() function can also take the
app.R file as argument because the file returns the app object:
Multiple file app
An alternative to having everything for our app in a single
app.R file, we can specify a directory where the
app.R file can be found.
However, when a directory is specified, we can have the Shiny app components
in separate files. This usually helps in maintaining the app when it grows more complex.
It is good practice to create a
global.R file with all the prerequisites
required by the app, i.e. loading libraries, scripts, data sets, running data
processing or defining functions. In our simple case we only load
the shiny package. Thus in our
global.R file we'll have the following line:
ui.R file contains the UI definition:
Similarly, we have a
server.R file defining the
We can now point
runApp() to this directory.
Next we will look at Shiny app deployment. Besides the officially recommended deployment options, such as shinyapps.io or the open source Shiny server, there are other possibilities. One particular option that we will review in depth is the deployment of Shiny apps via Docker and ShinyProxy. Read on!