shiny applicatons are a GUI (graphical user interface) that can run a R-based application. You can then post this online and it will run from an R server (not your computer). Users can run your application from the web without having to install R. In fact they can run from any browser, for example a smart phone. Alternatively, you can just have it in a package and provide it as a way for users to run analyses when they use your package from R.
Here are two example shiny apps that we have at the NWFSC. They are simple but are intended mainly to allow users to run standard analyses or explore data. shiny applications can be quite sophisticated and ‘shiny’. Search online for ‘shiny demos’.
The U.S. Geological Survey (a U.S. federal agency) also it active in developing tools to be used by the public and serves these on its usgs-r.github.io organizational site. They have been active in using Shiny apps for data exploration.
install.packages("shiny")
That’s it. You made a shiny application. To stop the app, close the window or hit ESC at the command line in RStudio.
This is the code that specifies the graphical interface.
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
This part has the R code that does the analysis, makes plots, makes table, etc.
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
Go to the RStudio shiny gallery, scroll down to the “Start Simple” section and start there. Once you want to add some more sections to your app, look lower at the Widgets sections. The code for the examples is there.
You’ll also want to go through RStudio’s online tutorial for shiny apps.
Development of anything past a fairly simple shiny app is quite time-consuming. This is a task that I outsource.
NW Fisheries Science Center (where I work) has its own R Shiny Server because we has applications with proprietary data that cannot be put on non-NOAA server. In addition, many of our applications require specialized extra software for fitting Bayesian models. Thus our application need more than just R packages.
However, if your application does not have these special constraints, you can host online for free with http://www.shinyapps.io/.
Follow the instructions here. In summary:
Sign up for an account at http://www.shinyapps.io/
install the rsconnect package
install.packages("rsconnect")
library(rsconnect)
Login into your account at http://www.shinyapps.io/. Click on your user name in top right, and click ‘Tokens’
A window should popup with code that looks like
rsconnect::setAccountInfo(name="<ACCOUNT>", token="<TOKEN>", secret="<SECRET>")
Click ‘Copy to clipboard’.
Paste that code into the command line in R.
From R, click on the ui.R
or server.R
file. Click ‘Run App’. You should see a ‘Publish’ button in the top right. Click that. Select ‘shinyapps.io’ and log into your account.
When your app opens, copy the url to the app.
In your DESCRIPTION file, add ‘shiny’ to the list of packages in the ‘Imports:’ line.
Create a folder called ‘inst’ in your package.
Create a folder called ‘shiny’ in that.
Within that folder, put your folder with all your application files. ‘ui.R’, ‘server.R’, any data files, any function files.
Add the following code to a files called launchApp.R
to your R directory. Change “myapp” and “mypackage” to the right names.
#' @export
launchApp <- function(x="myapp") {
appDir <- system.file("shiny", "myapp", package = "mypackage")
shiny::runApp(appDir, display.mode = "normal")
}
Rebuild your package. If later you add more shiny apps, but their folder in the shiny directory too. You run them with launchApp("mynewapp")
.
library(mypackage)
runIt("myapp")
Replace ‘mypackage’ and ‘myapp’ with the right names.