-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-Shiny-basics.Rmd
104 lines (69 loc) · 4.01 KB
/
05-Shiny-basics.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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.
## 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.
### Install the `aws.s3` package
```{r eval=FALSE, include=TRUE}
# install package
install.packages('aws.s3')
# load package
library(aws.s3)
```
### 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).
### 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:
```{r Renviron-example, fig.cap = ".Renviron file example", echo = FALSE}
knitr::include_graphics('figures/05-Shiny-basics/fig1_access_key_example.png')
```
* Save and close the `.Renviron` file.
### 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.
```{r eval=FALSE, include=TRUE}
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`.
```{r eval=FALSE, include=TRUE}
# 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")
```
### 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
```{r eval=FALSE, include=TRUE}
## 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)
```