Deploying a Shiny App with Shiny Server on an AWS EC2 Instance

Danny Morris

2019/07/06

Prerequisites

Launching the EC2 Linux Instance

  1. Launch an EC2 Instance by selecting the RStudio Server Amazon Machine Image for the appropriate region.

  1. Select the appropriate instance type. For demonstration purposes, a t2.micro is chosen

  2. Select “Review and Launch”, then select “Launch”

  3. Create a new key pair. Provide an appropriate key pair name, e.g. “rstudio-server-shiny”, then download the key pair.

  4. Select “Launch Instances”

Connect to the EC2 Linux Insance

  1. Naviage to the EC2 Dashboard and confirm the instance is running.

  1. Select the instance then click “Connect”

  2. Open Git Bash and SSH into the Linux instance

cd Downloads
chmod 400 rstudio-server-shiny.pem

ssh -i "rstudio-server-shiny.pem" ubuntu@ec2-18-224-95-116.us-east-2.compute.amazonaws.com

Explore R Interactively

Once connected the EC2 Linux instance, you can explore the R installation interactively within Git Bash.

# Launch R
R -

# R version
R.version

# Packages installed with the AMI
library(dplyr)
installed.packages() %>% row.names()

# Quit the R session
quit()

There are 139 packages installed with this AMI. Notable packages pre-installed include tidyverse, shiny, rmarkdown, Rcpp, DBi, and many more.

Explore Additional Installations

apt-cache showpkg shiny-server
apt-cache showpkg rstudio-server

which rstudio-server
which shiny-server

which python

pip list

Launch RStudio in the Browser

  1. Locate and click on the Security Group to which the instance belongs

  1. Configure the Security Group to allow inbound HTTP (port 80) traffic

  1. Copy and paste the instance’s Public DNS into the browser to launch RStudio Server. According the AMI instructions, the username is “rstudio” and the passwrod is the Instance ID

Create Simple Shiny App

  1. Once connected to RStudio Server in the browser, open a new R script and save the following code in a file called app.R. Place this file in ShinyApps/sample-apps/basic-app/.
## app.R ##

server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", 
                  min = 10, max = 500, value = 100)
    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)

  1. Click “Run App” to test the app locally (the EC2 Linux instance).

Launch App

  1. In the instance’s Security Group, add a Custom TCP rule to open port 3838 so Shiny Server can be accessed.

  1. Launch a sample app.
sudo /opt/shiny-server/bin/deploy-example default
  1. Copy our app folder to Shiny Server
sudo cp -r /home/rstudio/ShinyApps/sample-apps/basic-app/ /srv/shiny-server/sample-apps/
  1. Open the app at http://ec2-18-224-95-116.us-east-2.compute.amazonaws.com:3838/sample-apps/basic-app/