-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shinyshop.Rmd
166 lines (115 loc) · 5.13 KB
/
Shinyshop.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
title: "Shiny shop"
author: "Aaron Berdanier"
date: "May 5, 2017"
output:
html_document:
toc: true
toc_depth: 2
toc_float:
collapsed: false
smooth_scroll: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Shiny building blocks
```{r eval=FALSE}
# R ENVIRONMENT
library(shiny)
# UI, this is a function call, so contents are elements = need commas between
ui <- fluidPage()
# SERVER, this is a function definition, so contents are objects
server <- function(input, output){}
shinyApp(ui = ui, server = server)
```
# Our components
## In the R environment
We want to load our packages, maybe set up some data, e.g.:
```
library(shiny)
library(leaflet)
```
## In the UI
We want to include:
- site dropdown -- `selectInput()`
- number of widgets input -- `numericInput()`
- air temperature input -- `numericInput()`
- add data button -- `actionButton()`
- map with sites -- [leaflet](https://rstudio.github.io/leaflet/)
- graph with number v. temperature -- `plotOutput()`
- table with date, site, number, Temperature -- `tableOutput()` or `dataTableOutput()`
- download .csv button -- `downloadButton()`
## In the Server
We want to include:
- sites data -- maybe a `list()`?
- "add data" processing -- [aws bucket](https://github.com/cloudyr/aws.s3), [mongodb](https://github.com/jeroen/mongolite), [google drive sheet](https://github.com/jennybc/googlesheets), or [dropbox file](https://github.com/karthik/rdrop2)
- render leaflet -- [leaflet](https://rstudio.github.io/leaflet/)
- render graph -- `renderPlot()`
- render table -- `renderTable()` or `DT::renderDataTable()`
- render download -- [`downloadHandler()`](http://shiny.rstudio.com/gallery/file-download.html)
# Our development steps
1. Set up basic interface, spit out inputs in a table -- test
2. Set up our database interface -- test
3. Set up leaflet interface -- test
4. Combine them -- test
## 1. Basic interface
#### See above. To start, we'll make it just output user input into a table -- **test**
## 2. Database choose your own adventure
#### First, check out the [Remote Data document](Remotedata.html)
This will get you started on data storage
#### Then, we'll run a test
In a new R file, try this:
```
source("global.R")
current_time <- as.character(Sys.time())
test_data <- data.frame(addtime=c(current_time,current_time),
site=c("Site1","Site2"),
weather=c(2,3),
nwidgets=c(10,12.3))
save_db(test_data)
xx <- load_db()
print(xx)
```
Does it work?
## 3. leaflet interface
#### A basic map -- **test**
```
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
```
(from [RStudio](https://rstudio.github.io/leaflet/))
#### Then, we can [add a leaflet map to Shiny](https://rstudio.github.io/leaflet/shiny.html)...
## 4. Put it together
#### Some pseudocode -- **Actions and reactions**
* User clicks add data button
+ server adds data to database
+ redraw data table
+ redraw graph
* User clicks on a site on the map
+ filter table data
+ filter graph data
* User clicks on download data button
+ grab data in data table
+ download to file
# To shinyapps.io!
Getting your shinyapps.io set up is pretty easy in RStudio. We'll walk through an example. [The full reference is here](http://docs.rstudio.com/shinyapps.io/getting-started.html).
Once you link your account, deploying an app from RStudio is as simple as clicking a button...
![](ShinyPublish.png)
... or from the console with `deployApp()` (*note*: by default this deploys all files in the working directory...).
## Other hosting options
#### Hosting options at Duke
For those of you at Duke, you can get a free virtual machine that is set up to run a Shiny Server. [More information about that here](http://vm-manage.oit.duke.edu). Duke OIT also has a GitHub/Stevedore/Docker/Shiny system. Docker is the future of cloud computing. Maybe ask Matt Ross for guidance...
#### Hosting your own instances
If you really want to geek out, there are other awesome options for hosting Shiny apps.
You can host your app (and your data if you want) on AWS. [Here is an example of doing that](http://www.kimberlycoffey.com/blog/2016/2/13/mlz90wjw0k76446xkg262prvjp0l8u). [Here is another](https://edgarsdatalab.com/2016/08/25/rstudio-shiny-server-in-aws/).
I use [Digital Ocean for hosting](https://m.do.co/c/95a6044925d8) because it is easy to figure out their pricing (AWS pricing is confusing AF). There is a great [tutorial on setting up your own Shiny Server on Digital Ocean here](https://www.jasperginn.nl/shiny-server-series-pt1/).
# More reading
#### Shiny stuff
[The shiny cheat sheet](http://shiny.rstudio.com/images/shiny-cheatsheet.pdf) - this is **awesome**
[RStudio articles with examples](https://shiny.rstudio.com/articles/)
#### Interative visualization stuff
[The superpower of interactive datavis? A micro-macro view!](https://medium.com/@dominikus/the-superpower-of-interactive-datavis-a-micro-macro-view-4d027e3bdc71) - by Dominikus Baur - about blending *anecdote* and *statistics*. (Also, check out his "Death of Interactive Graphics")