-
Notifications
You must be signed in to change notification settings - Fork 41
/
20-packages.Rmd
141 lines (97 loc) · 4.24 KB
/
20-packages.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
# Packages
> A package is just a lightweight set of conventions that unlock useful tools and workflows.
## Package Structure
The most simple package contains a `DESCRIPTION` file, housing metadata about the package (e.g. name and authors), and an `R/` directory where `.R` files with function definitions live.
Packages may also contain:
* A `data/` directory, which holds reference datasets.
* A `tests/` directory, which holds unit-tests that are used to ensure code works as expected.
* A `vignettes/` directory, which holds long-form documentation.
* More! Read Hadley Wickham and Jenny Bryan's **amazing** book, [R Packages](https://r-pkgs.org)
## Benefits
In general, packages are beneficial for easily sharing your work. Packages provide a common organizational structure and offer improved workflow for loading and launching Shiny applications.
## Converting an existing app
1. Call [`usethis::create_package("pick-a-path")`](https://r-pkgs.org/whole-game.html#create_package). This will create the package directory and skeleton.
2. Remove any `library()` or `require()` calls, in favor of `usethis::use_package("name")`.
3. If your app uses modules, place related ones in individual `.R` files with `usethis::use_r("module-category")`.
4. Wrap core app inside a function and place within a `.R` file, e.g:
```{r greeting-app, eval = FALSE}
greetingApp <- function() {
# define a user-interface with two elements: a text input with an ID,
# label, and initial value; define a textOutput that
# returns the input + greeting
ui <- fluidPage(
textInput(inputId = "nameInput",
label = "What is your name?",
value = "World"),
textOutput("name")
)
# define the server side logic to manipulate the inputs
server <- function(input, output, session) {
# define the output that concatenates the strings
# "Hello, " + user input + "."
output$name <- renderText({paste0("Hello, ", input$nameInput, ".")})
}
# run the application
shinyApp(ui, server)
}
```
5. Call `devtools::load_all()` and your function -- `greetingApp()` to say hello to your package!
## Example: shinysurveys
> shinysurveys provides easy-to-use, minimalistic code for creating and deploying surveys in Shiny.
### Installation
You can install {shinysurveys} via CRAN or GitHub and load it as follows:
``` {.r}
# Install released version from CRAN
install.packages("shinysurveys")
# Or, install the development version from GitHub
remotes::install_github("jdtrat/shinysurveys")
# Load package
library(shinysurveys)
```
### Demo
A survey made with shinysurveys package might look like this:
![](https://www.jdtrat.com/project/shinysurveys/shinysurveys-final-demo.gif)
You can run a demo survey with the function `shinysurveys::demo_survey()`.
### Let's take a look!
* Live walk-through of creating an app with shinysurveys, how everything is packaged together, and more!
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/IRkgCUvvFQ4")`
<details>
<summary> Meeting chat log </summary>
```
00:16:10 Russ Hyde: Hi everyone
00:25:15 Robert Overman: In the past the app itself had to be put into the inst folder of the package
00:27:41 Robert Overman: So now being able to put it into the R folder makes things a lot easier, than having to do the odd reference back to runapp having it find the source file location to pull the server and ui
00:37:53 Jonathan Trattner: https://github.com/jdtrat/shinysurveys
```
</details>
### Cohort 3
`r knitr::include_url("https://www.youtube.com/embed/CDSDAVye-Fk")`
<details>
<summary>Meeting chat log</summary>
```
00:46:57 Brendan Lam: Your tutorial was helpful, thanks!
```
</details>
### Cohort 4
`r knitr::include_url("https://www.youtube.com/embed/_POCL1OHvyw")`
<details>
<summary>Meeting chat log</summary>
```
00:33:30 Lydia Gibson: Hello
00:34:25 Trevin Flickinger: start
00:50:01 Trevin Flickinger: “Using @import is not generally considered best practice, but it makes sense here”
01:07:08 Lydia Gibson: Need to drop for another meeting. See you next week
01:08:22 Trevin Flickinger: Lydia dropped off the call
01:16:21 Trevin Flickinger: stop
```
</details>
### Cohort 5
`r knitr::include_url("https://www.youtube.com/embed/URL")`
<details>
<summary>Meeting chat log</summary>
```
LOG
```
</details>