App formats and launching apps
By: Winston Chang
You may have noticed that there are several different ways that Shiny apps are defined and launched. Sometimes you’ll see the
shinyServer() in the
server.R file, sometimes not, and the same goes for
ui.R. Sometimes there isn’t even a
server.R file at all.
This article provides an overview of the different ways of defining and launching Shiny applications.
Most early Shiny examples will include a
ui.R file like the following:
For applications defined this way, the
server.R file must return the server function, and the
ui.R file must return the UI object (in this case, the UI object is created by
fluidPage()). In other words, if the files contained other code (like utility functions) you must make sure that the last expression in the file is the server function or UI object.
For more information, see the article about two-file apps.
Prior to Shiny 0.10, the
ui.R files required calls to
shinyUI() respectively. Older Shiny application examples might look like the following. These are the same as in the previous example, except that the code is wrapped in
As of Shiny 0.10, calling these functions is no longer needed.
As of Shiny 0.10.2, applications can be created with a single file,
app.R, which contains both the UI and server code. This file must return an object created by the
This method is more appropriate for smaller applications; for larger applications, you may find that having separate
server.R files makes your code easier to manage.
In the server code for some examples, you might see code like this:
In other examples, you might
session as a third argument to the server function:
session argument is optional. It’s only needed if you want to use advanced features of Shiny – some functions in Shiny take the
session variable as an argument.
You may also see some older examples that take
clientData as an argument to the server function.
clientData provides information about the connection and the visibility of various components on the web page (see the client data article for more).
However, it is no longer necessary to use
clientData as an argument, because if you have
session, you can access the same information client data with
session$clientData. For the sake of consistency, we recommend using
Ways of calling
There are several different things that may be passed to
runApp() to launch an application.
If your application resides in a directory
myapp/, you could launch it with:
Shiny app object
If you’ve created a Shiny app object at the console by calling
shinyApp(), you can pass that app object to
Additionally, if you simply type
app at the console and press Enter, R will launch the app. This is because when you run code at the console, R will call
print() on the return value, and for a Shiny app object, the
print() method calls
runApp() on the object. So you could do the following to launch the app:
Another way to launch an app is by giving
runApp() a list with the ui and server components. This is an older style that predates the Shiny app object method above.
If you have questions about this article or would like to discuss ideas presented here, please post on RStudio Community. Our developers monitor these forums and answer questions periodically. See help for more help with all things Shiny.