-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshiny.Rmd
159 lines (109 loc) · 5.96 KB
/
shiny.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
---
title: "Shiny Applications"
output:
html_document:
toc: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
[shiny applicatons](https://shiny.rstudio.com/) 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.
## Fisheries examples
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'.
* [Fisheries Economics Data Explorer](https://dataexplorer.northwestscience.fisheries.noaa.gov/fisheye/NetRevExplorer/)
- Proprietary data
* [Steller Sea Lion Trend Estimation](https://dataexplorer.northwestscience.fisheries.noaa.gov/nwc/agTrend/)
- Needed specialized add-on software
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](http://usgs-r.github.io/) organizational site. They have been active in using Shiny apps for data exploration.
* [Lake Erie Fish Survey](https://lebs.shinyapps.io/western-basin/) - [code](https://github.com/taylorstewart/lebs-western-basin-app)
* [toxEval](http://usgs-r.github.io/toxEval/index.html)
## Make your first shiny app
1. Install the shiny package.
```
install.packages("shiny")
```
1. Click File > New > Shiny Web App...
2. Select a location to put the application folder.
3. Select "Multiple File". You can choose single file, but I will demo the separate case with a separate file for the interface code and 'workhorse' code.
4. Give you application a name.
4. When the files 'ui.R' and 'server.R' open, click 'Run App'.
That's it. You made a shiny application. To stop the app, close the window or hit ESC at the command line in RStudio.
## ui.R file
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")
)
)
))
```
## server.R
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')
})
})
```
## What can shiny apps do and how can I copy that?
Go to the [RStudio shiny gallery](https://shiny.rstudio.com/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](https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/) for shiny apps.
Development of anything past a fairly simple shiny app is quite time-consuming. This is a task that I outsource.
## Putting your application online
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/](http://www.shinyapps.io/).
Follow the instructions [here](https://shiny.rstudio.com/articles/shinyapps.html). In summary:
1. Sign up for an account at [http://www.shinyapps.io/](http://www.shinyapps.io/)
2. install the rsconnect package
```
install.packages("rsconnect")
library(rsconnect)
```
3. Login into your account at [http://www.shinyapps.io/](http://www.shinyapps.io/). Click on your user name in top right, and click 'Tokens'
4. A window should popup with code that looks like
```
rsconnect::setAccountInfo(name="<ACCOUNT>", token="<TOKEN>", secret="<SECRET>")
```
Click 'Copy to clipboard'.
5. Paste that code into the command line in R.
6. 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.
7. When your app opens, copy the url to the app.
## Adding a shiny app to your R package
1. In your DESCRIPTION file, add 'shiny' to the list of packages in the 'Imports:' line.
1. Create a folder called 'inst' in your package.
2. Create a folder called 'shiny' in that.
3. Within that folder, put your folder with all your application files. 'ui.R', 'server.R', any data files, any function files.
5. 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")`.
4. To run
```
library(mypackage)
runIt("myapp")
```
Replace 'mypackage' and 'myapp' with the right names.