6 Shiny basics

The purpose of this chapter is to guide you through various processes that you might need to go through to develop a Shiny R application.

6.1 Integrating Shiny and AWS data storage

This section aims to demonstrate how to utilise the Amazon Web Service (AWS) API to load data from an AWS bucket into the R environment. It then provides an example of how to use this protocol within a Shiny web application.

6.1.1 Install the aws.s3 package

# install package
install.packages('aws.s3')

# load package
library(aws.s3)

6.1.2 Getting your unique AWS access keys - refer to Sonia for help with this step

  • From https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html:
    • AWS access keys allow you to sign programmatic requests to the AWS CLI or AWS API.
    • Access keys consist of two parts: an access key ID (for example, AKIAIOSFODNN7EXAMPLE) and a secret access key (for example, wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY).
    • Like a user name and password, you must use both the access key ID and secret access key together to authenticate your requests.
    • Manage your access keys as securely as you do your user name and password (i.e. include the .Renviron file in your .gitignore file if this is a project you will be storing in a GitHub repository).

6.1.3 Setting up your .Renviron file

  • Once you have your access key, secret access key and AWS default region (see step 1), this is the file that will store this information.
  • Assuming that you are in your working directory/ same directory as your Shiny app.R file, run the command touch ./.Renviron from the command line to generate the .Renviron file.
  • Then open this file, either on the command line, vi .Renviron, or interactively on Rstudio.
  • To edit on the command line, use hit ESC followed by i to insert text, then to save and close, ESC followed by :wq.
  • Add your access key, secret access key and AWS default region, for example:
.Renviron file example

Figure 6.1: .Renviron file example

  • Save and close the .Renviron file.

6.1.4 Loading AWS objects

  • The following command will load your access keys from your .Renviron file in an anonymised format, meaning you can put this in the body of your Shiny app.R file.
Sys.setenv("AWS_ACCESS_KEY_ID"=Sys.getenv("AWS_ACCESS_KEY_ID"),
           "AWS_SECRET_ACCESS_KEY"=Sys.getenv("AWS_SECRET_ACCESS_KEY"),
           "AWS_DEFAULT_REGION"=Sys.getenv("AWS_DEFAULT_REGION"))
  • Now you can use the AWS API to access remote files using the aws.s3 package.
  • The following example uses the s3read_using function and calls read.csv on the object specified by the object argument.
  • Other reader functions can be used, such as readr::read_csv or fst::read.fst.
# load aws.s3 library
library(aws.s3)

# read in csv from AWS s3 bucket 
obj1 <- aws.s3::s3read_using(read.csv, object = "s3://your-bucket/dataframe.csv")

6.1.5 Shiny/AWS example

Loading data from AWS in a Shiny app.R file

  • app.R is the core server/UI file you will have if you are building a Shiny app.
  • This example app.R file includes all the necessary code to load data from an AWS bucket
## app.R ##

# load libraries
library(shiny)
library(aws.s3)

# load access keys
Sys.setenv("AWS_ACCESS_KEY_ID"=Sys.getenv("AWS_ACCESS_KEY_ID"),
           "AWS_SECRET_ACCESS_KEY"=Sys.getenv("AWS_SECRET_ACCESS_KEY"),
           "AWS_DEFAULT_REGION"=Sys.getenv("AWS_DEFAULT_REGION"))

# load data from AWS bucket, assign to global 
obj1 <<- aws.s3::s3read_using(read.csv, object = "s3://your-bucket/dataframe.csv")

# define server function
server <- function(input, output) {
  # server code
}

# define UI function
ui <- fluidPage(
  # UI code
)

# run shinyApp function 
shinyApp(ui = ui, server = server)