-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolem.qmd
712 lines (547 loc) · 28.1 KB
/
golem.qmd
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# golem {#sec-golem}
```{r}
#| eval: true
#| echo: false
#| include: false
source("_common.R")
```
```{r}
#| label: co_box_dev
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "r",
header = "Warning",
contents = "The contents for this section are under development. Thank you for your patience."
)
```
```{r}
#| label: co_box_tldr
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "b",
look = "default", hsize = "1.10", size = "1.05",
header = "TLDR   ![](images/goelm.png){width='10%'}",
fold = TRUE,
contents = "
<br>
- The `golem` framework has excellent documentation in the [Engineering Production-Grade Shiny Apps](https://engineering-shiny.org/) book and the [package website](https://thinkr-open.github.io/golem/).
- New `golem` app-packages come 'pre-packaged' with UI, server, and standalone app functions
- `dev/` scripts help guide app setup, development, and deployment
- Development is assisted by variety of `add_` and `use_` functions for creating modules, utility functions, js, css, etc\n
- `add_*` functions include a `with_test` argument for creating tests along with new modules and utility functions
- All `golem` package functions and files have consistent naming conventions (`mod_`, `fct_`, `utils_`, etc.)
- `golem` 'gives away' UI and server utility functions with boilerplate tests (for *free*!)
"
)
```
---
This chapter walks through building a version of the `sap` with the [`golem` framework](https://thinkr-open.github.io/golem/). The resulting app-package (`gap`) is in the [`18_golem` branch](https://github.com/mjfrigaard/sap/tree/18_golem).
```{r}
#| eval: false
#| code-fold: false
install.packages("golem")
library(golem)
```
The version/description of `golem` used in this Shiny app-package is:
```{r}
#| echo: false
pkg_info('golem', gt = TRUE)
```
:::: {.callout-tip collapse='true' appearance='simple'}
## [Accessing the code examples]{style='font-weight: bold; font-size: 1.15em;'}
::: {style='font-size: 0.95em; color: #282b2d;'}
I've created the [`shinypak` R package](https://mjfrigaard.github.io/shinypak/) In an effort to make each section accessible and easy to follow:
Install `shinypak` using `pak` (or `remotes`):
```{r}
#| code-fold: false
#| message: false
#| warning: false
#| eval: false
# install.packages('pak')
pak::pak('mjfrigaard/shinypak')
```
Review the chapters in each section:
```{r}
#| code-fold: false
#| message: false
#| warning: false
#| collapse: true
library(shinypak)
list_apps(regex = 'golem')
```
Launch an app:
```{r}
#| code-fold: false
#| eval: false
launch(app = "18_golem")
```
:::
::::
After checking out the `18_golem` branch, be sure to load, document, and install the application.
```{r}
#| label: dev_key_all_01
#| echo: false
#| results: asis
#| eval: true
hot_key("all")
```
## [`gap`]{style="font-weight: bold; font-size: 1.10em;"} (a golem app-package)
`gap` exports the `movies` data and the standalone app function, `run_app()`.
```{r}
#| eval: false
#| code-fold: false
library(gap)
gap::run_app()
```
```{r}
#| label: git_box_gap
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '75%',
branch = "18_golem",
repo = 'sap')
```
::: {#fig-golem_run_app}
![`gap` movies app](images/golem_run_app.png){#fig-golem_run_app width='100%' align='center'}
After loading, documenting, and installing `gap`, launch the movies with `run_app()`
:::
In the sections below, I'll note various features and workflows that differ from standard package (or app-package) development.
## [`dev`]{style="font-weight: bold; font-size: 1.10em;"} scripts
New `golem` apps have a `dev/` folder with scripts for setting up your application, development, and launching/deploying.
```{bash}
#| eval: false
#| code-fold: false
dev
├── 01_start.R
├── 02_dev.R
└── 03_deploy.R
```
The package website has each script available in vignettes: [01_start](https://thinkr-open.github.io/golem/articles/a_start.html), [02_dev](https://thinkr-open.github.io/golem/articles/b_dev.html), [03_deploy](https://thinkr-open.github.io/golem/articles/c_deploy.html)
## Set up
The `dev/01_start.R` file opens when the new `golem` app-package launches. Following the steps in `01_start.R` sets up the following files, folders, and options in your `golem` app-package:
- `DESCRIPTION`: `golem` has a custom function for entering many of the fields we covered in the [Packages](packages.qdm) chapter [^golem-description]
- `golem` has functions that automatically set multiple options in the `golem-config.yml` [^golem-options] and package dependencies [^golem-depends]
- `usethis` functions are called for a `LICENSE`[^golem-usethis-lisc], creating (and building[^golem-devtools-readme]) a `README`[^golem-usethis-readme] a `CODE_OF_CONDUCT.md`[^golem-usethis-code_of_conduct], adding a Lifecycle badge[^golem-usethis-lifecycle], and the `NEWS.md` file.[^golem-usethis-news_md] Many of these functions and files are covered in the [Whole App Game](whole_game.qmd) chapter.
- If you aren't using Git, there's an option to initiate a Git repo [^golem-usethis-use-git]
- The testing infrastructure is set up using the custom [`golem::use_recommended_tests()`](https://thinkr-open.github.io/golem/reference/use_recommended.html) function, which creates the necessary `testthat` folders we covered in the [Test suite](test_suite.qmd) chapter [^golem-start-tests], but also adds a collection of boilerplate tests.
- `golem` apps also have custom functions for creating a series of UI [^golem-start-ui-utils] and server [^golem-start-server-utils] utility functions (and their accompanying tests).
[^golem-description]: Fields are filled with [`golem::fill_desc()`](https://thinkr-open.github.io/golem/reference/fill_desc.html)
[^golem-options]: Options are set with with [`golem::set_golem_options()`](https://thinkr-open.github.io/golem/reference/golem_opts.html)
[^golem-depends]: Dependencies are installed with [`golem::install_dev_deps()`](https://thinkr-open.github.io/golem/reference/install_dev_deps.html)
[^golem-usethis-lisc]: Created using `usethis::use_mit_license()`
[^golem-usethis-readme]: Created using `usethis::use_readme_rmd()`
[^golem-devtools-readme]: Built using `devtools::build_readme()`
[^golem-usethis-code_of_conduct]: Created using `usethis::use_code_of_conduct()`
[^golem-usethis-lifecycle]: Created using `usethis::use_lifecycle_badge()`
[^golem-usethis-news_md]: Created using `usethis::use_news_md()`
[^golem-usethis-use-git]: Initialize Git using `usethis::use_git()`
[^golem-start-tests]: The `tests/` folder and `testthat` files are included with [`golem::use_recommended_tests()`](https://thinkr-open.github.io/golem/reference/use_recommended.html)
[^golem-start-ui-utils]: Create UI utility functions using [`golem::use_utils_ui()`](https://thinkr-open.github.io/golem/reference/utils_files.html)
[^golem-start-server-utils]: Create server utility functions using [`golem::use_utils_server()`](https://thinkr-open.github.io/golem/reference/utils_files.html)
## Development
After setting up the `golem` app-package with `dev/01_start.R`, the `dev/02_dev.R` file opens and contains the following functions/options for developing your application.
- **Package dependencies**: imports are managed with the [`attachment`](https://thinkr-open.github.io/attachment/index.html) package[^golem-attachment]
[^golem-attachment]: [`attachment::att_amend_desc()`](https://thinkr-open.github.io/attachment/reference/att_amend_desc.html) parses the code under `R/` and make sure the `DESCRIPTION` file is up-to-date
- **Code files**: new code files in `golem` apps can be created with a variety of helper functions.
<br><br>
- **Modules**: [`add_module()`](https://thinkr-open.github.io/golem/reference/add_module.html) adds a `.R` file with a `mod_` prefix (an optional test can be included with the `with_test = TRUE` argument)
<br><br>
- **Utility functions**: utility functions can be added with [`add_utils()` or `add_fct()`](https://thinkr-open.github.io/golem/reference/file_creation.html) (also include the `with_test` option for tests). `golem_utils_*` files contain commonly used UI and server functions.
<br><br>
- The primary app UI and server functions are stored in `R/app_ui.R` and `R/app_server.R`.
<br><br>
- The standalone app function is stored in `R/run_app.R.`
<br><br>
- `R/_disable_autoload.R` disables [`shiny::loadSupport()`](https://shiny.posit.co/r/reference/shiny/1.7.1/loadsupport) (an option we covered in the [Launch](launch.qmd) chapter)
<br><br>
- `R/app_config.R` contains configuration functions:
<br>
- `app_sys()` is a wrapper for `system.file()`
<br><br>
- `get_golem_config()` reads environment variables (i.e., [`GOLEM_CONFIG_ACTIVE`](https://engineering-shiny.org/golem.html#golem_config-vs-golem_options)) and the contents of `inst/golem-config.yml`:
```{bash}
#| eval: false
#| code-fold: false
R
├── _disable_autoload.R # <1>
├── app_config.R # <2>
├── app_server.R # <3>
├── app_ui.R # <3>
├── data.R # <4>
├── golem_utils_server.R # <5>
├── golem_utils_ui.R # <5>
├── mod_scatter_display.R # <6>
├── mod_var_input.R # <7>
├── utils_mod_scatter_display.R # <8>
├── utils_tests.R # <9>
└── run_app.R # <10>
1 directory, 11 files
```
1. Turn off `loadSupport()`
2. Configure app functions
3. App UI and server functions
4. Data documentation
5. `golem` utility functions
6. Scatter plot module
7. Variable input module
8. Scatter plot utility function (`scatter_plot()`)
9. Test utility function (`test_logger()`)
10. Standalone app function
- **External files**: adding external files is handled with [`golem_add_external_resources()`](https://engineering-shiny.org/golem.html#instappwww), which uses the same methods we covered in the [Resources](resources.qmd) chapter.[^golem-add-external-files]
<br>
- `WORDLIST`: includes the word '`golem`' and is an artifact from `spellcheck` argument in [`use_recommended_tests()`](https://thinkr-open.github.io/golem/reference/use_recommended.html). [^golem-wordlist]
<br><br>
- `app/` contains the application files. [^golem-inst-app]
<br><br>
- `extdata/` contains the external data files. [^golem-inst-extdata]
<br><br>
- `golem-config.yml` is used to set various configuration options. [^golem-inst-config]
[^golem-add-external-files]: [`golem_add_external_resources()`](https://engineering-shiny.org/golem.html#instappwww) is a wrapper for [`golem::add_resource_path()`](https://thinkr-open.github.io/golem/reference/add_resource_path.html), which is a wrapper for `shiny::addResourcePath()` (and `app_sys()` is a wrapper for `system.file()`).
[^golem-wordlist]: The [`use_recommended_tests()`](https://thinkr-open.github.io/golem/reference/use_recommended.html) is run in the `dev/01_start.R` file and if `spellcheck` is `TRUE`, creates the `tests/spelling.R` file and the `inst/WORDLIST` file.
[^golem-inst-app]: the `app/` folder is used to add external resources to the application (similar to the previous versions of `sap`).
[^golem-inst-extdata]: This contains the `RData` file for the original movies data and the exported `tidy_movies.fst` file.
[^golem-inst-config]: `golem` apps use a `golem-config.yml` file for setting various options. These are initially set with [`set_golem_options()`](https://thinkr-open.github.io/golem/reference/golem_opts.html) (and based on the [`config` package](https://rstudio.github.io/config/))
```{bash}
#| eval: false
#| code-fold: false
inst
├── WORDLIST
├── app
│ └── www
│ ├── favicon.ico
│ ├── golem-hex.png
│ └── shiny.png
├── extdata
│ ├── movies.RData
│ └── tidy_movies.fst
└── golem-config.yml
4 directories, 7 files
```
- **Data**: the data in `golem` app-packages function like the data folder and files in a standard R package we covered in the [Data](data.qmd) chapter.
<br><br>
- `data/`: contains the `movies.rda` file used in the application
<br><br>
- `data-raw/`: `movies.R` reads in `data-raw/movies.RData` and creates `data/movies.rda`
<br><br>
- `tidy_movies.R` reads in the `ggplot2movies::movies` data and creates the `inst/extdata/tidy_movies.fst` data.
```{bash}
#| eval: false
#| code-fold: false
data
└── movies.rda
1 directory, 1 file
```
```{bash}
#| eval: false
#| code-fold: false
data-raw
├── movies.R
├── movies.RData
└── tidy_movies.R
1 directory, 2 files
```
- **Documentation**: the `roxygen2` documentation in `golem` app-package files comes with boilerplate tags and descriptions similar to those covered in the [Documentation](document.qmd) chapter.
<br><br>
- `man/`: By default, modules created with [`add_module()`](https://thinkr-open.github.io/golem/reference/add_module.html) aren't exported [^golem-nord]
<br><br>
- `vignettes/`: Package vignettes in `golem` app-packages operate like vignettes in standard R packages.
[^golem-nord]: The `noRd` tag is added to module files created with `add_module()`, but you can export these functions by setting the `export` argument to `TRUE`. `@importFrom` is used to import `NS()` and `tagList()`.
```{bash}
#| eval: false
#| code-fold: false
man
├── app_server.Rd # <1>
├── app_ui.Rd # <1>
├── golem_add_external_resources.Rd # <2>
├── mod_scatter_display_server.Rd # <3>
├── mod_scatter_display_ui.Rd # <3>
├── mod_plot_ui.Rd # <3>
├── mod_var_input_server.Rd # <3>
├── mod_var_input_ui.Rd # <3>
├── movies.Rd # <4>
├── run_app.Rd # <5>
├── scatter_plot.Rd # <6>
└── test_logger.Rd # <7>
1 directory, 12 files
```
1. Primary UI and server functions ('pre-packaged' in `golem` apps)
2. External resources utility function ('pre-packaged' in `golem` apps)
3. Modules
4. Data documentation
5. The 'pre-packaged' standalone app function (only export from `golem` apps)
6. Plot utility function (exported from `gap`)
7. Test utility function
```{bash}
#| eval: false
#| code-fold: false
vignettes/
└── tests_and_coverage.Rmd
1 directory, 1 file
```
## Tests
`golem` applications provide *a lot* of boilerplate tests.
```{bash}
#| eval: false
#| code-fold: false
tests
├── README.md # <1>
├── spelling.R # <2>
├── testthat
│ ├── _snaps
│ ├── fixtures # <3>
│ │ ├── make-tidy_ggp2_movies.R
│ │ └── tidy_ggp2_movies.rds # <3>
│ ├── helper.R # <4>
│ ├── setup-shinytest2.R # <5>
│ ├── test-app-feature-01.R # <6>
│ ├── test-golem-recommended.R # <7>
│ ├── test-golem_utils_server.R # <8>
│ ├── test-golem_utils_ui.R # <9>
│ ├── test-mod_scatter_display.R # <10>
│ ├── test-mod_var_input.R # <11>
│ ├── test-shinytest2.R # <12>
│ └── test-utils_mod_scatter_display.R # <13>
└── testthat.R
4 directories, 15 files
```
1. Created from `covrpage` package
2. Created from spelling package
3. Test fixtures
4. Test helpers
5. Setting up `shinytest2`
6. `shinytest2` feature test
7. Created with: [`golem::use_recommended_tests()`](https://thinkr-open.github.io/golem/reference/use_recommended.html)
8. Created with: [`golem::use_utils_ui(with_test = TRUE)`](https://thinkr-open.github.io/golem/reference/utils_files.html)
9. Created with: [`golem::use_utils_server(with_test = TRUE)`](https://thinkr-open.github.io/golem/reference/utils_files.html)
10. Created with: [`golem::add_module(name = 'scatter_display', ` `with_test = TRUE)`](https://thinkr-open.github.io/golem/reference/add_module.html)
11. Created with: [`golem::add_module(name = 'var_input',` `with_test = TRUE)`](https://thinkr-open.github.io/golem/reference/add_module.html)
12. Test recording from [`shinytest2::record_test()`](https://rstudio.github.io/shinytest2/reference/record_test.html)
13. Utility function test (`scatter_plot()`)
- **Tests**: The testing framework for `golem` app-packages is set up with [`golem::use_recommended_tests()`](https://thinkr-open.github.io/golem/reference/use_recommended.html) in the `dev/01_start.R` script.[^golem-tests-recommended]
<br><br>
- A `tests/README.md` file is created by the [`covrpage`](https://yonicd.github.io/covrpage/) package [^golem-tests-covrpage-readme]
<br><br>
- `tests/spelling.R` adds functionality from the [`spelling`](https://docs.ropensci.org/spelling/) package [^golem-tests-spelling]
<br><br>
- `testthat`: The two `golem` utility function files (`golem_utils_server.R`, and `golem_utils_ui.R`) have accompanying tests files.
<br><br>
- The `with_test` argument creates test files for modules and utility functions. [^golem-tests-testthat]
[^golem-tests-recommended]: `test-golem-recommended.R` contains the recommended tests for `app_ui()`, `app_server()`, `app_sys()`, and `golem-config.yml`
[^golem-tests-covrpage-readme]: The `covrpage` package is not on CRAN, but the development version always seems to work. Create the `tests/README.md` file with `covrpage::covrpage()`.
[^golem-tests-spelling]: The [`spelling` package](https://docs.ropensci.org/spelling/) will spell check vignettes, packages, etc.
[^golem-tests-testthat]: `with_test = TRUE)` adds tests in the `dev/01_start.R` script. Code files created with [`golem::add_module()`](https://thinkr-open.github.io/golem/reference/add_module.html), [`golem::add_utils()`](https://thinkr-open.github.io/golem/reference/file_creation.html), and [`golem::add_fct()`](https://thinkr-open.github.io/golem/reference/file_creation.html) will also include a test file if `with_test` is set to `TRUE`.
### Unit tests
I've converted the tests from the previous chapters for the modules and utility functions in `gap`:
- `test-golem-recommended.R` contains tests for the functions included in your new golem app (`app_ui()`, `app_sys()`, etc.)
- `test-golem_utils_server.R` contains utility functions that might be useful in the `server`
- `test-golem_utils_ui.R` contains utility functions that might be useful in the `ui`
- The tests for `scatter_plot()` is in the `test-utils_mod_scatter_display.R` file.
### Module tests
- The communication between `mod_var_input_server()` and `mod_scatter_display_server()` are in `test-mod_var_input.R` and `test-mod_scatter_display.R`
### System tests
The two system tests are in `test-shinytest2.R` and `test-app-feature-01.R`:
- `test-shinytest2.R` is the initial resulting test from `shinytest2::record_test()` covered in [System tests](test_system.qmd)
- `test-app-feature-01.R` contains feature tests
```{r}
#| label: co_box_system_tests
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "r", size = '1.05', hsize = '1.10', fold = FALSE, look = 'minimal',
header = "`loadSupport()` warning with `shinytest2`",
contents = "
After setting up `shinytest2`, the `tests/testthat/setup-shinytest2.R` file contains a call to `shinytest2::load_app_env()`. This runs automatically with `shinytest2` tests and produces a familiar warning:
\`\`\`sh
Warning message:
In shiny::loadSupport(app_dir, renv = renv, globalrenv = globalrenv) :
Loading R/ subdirectory for Shiny application, but this directory appears
to contain an R package. Sourcing files in R/ may cause unexpected behavior.
\`\`\`
We covered this warning message in the [Launch](launch.qmd) chapter, and it's being addressed in a future release of [`shinytest2`](https://github.com/rstudio/shinytest2/issues/264)"
)
```
## Deployment
When you're ready to deploy your `golem` app, the `dev/03_deploy.R` file contains a 'pre deployment checklist' with multiple options for deploying your application.
### Posit platforms
- `devtools::check()` and `devtools::build()` are called.[^golem-devtools-deploy]
- `golem` has functions for RStudio Connect, shinyapps.io, and Shiny server,[^golem-deploy-posit-options] as well as options for deploying your application using `rsconnect::deployApp()`. [^golem-deploy-rsconnect-options]
### Docker
`golem` has multiple options for creating Docker files:
- [`golem::add_dockerfile()`](https://thinkr-open.github.io/golem/reference/dockerfiles.html) adds the following `Dockerfile`:
```{bash}
#| eval: false
#| code-fold: true
#| code-summary: 'show/hide Dockerfile'
#| include: true
#| code-overflow: scroll
FROM rocker/verse:4.3.2
RUN apt-get update && apt-get install -y libcurl4-openssl-dev libicu-dev libssl-dev libxml2-dev make pandoc zlib1g-dev && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /usr/local/lib/R/etc/ /usr/lib/R/etc/
RUN echo "options(repos = c(CRAN = 'https://cran.rstudio.com/'), download.file.method = 'libcurl', Ncpus = 4)" | tee /usr/local/lib/R/etc/Rprofile.site | tee /usr/lib/R/etc/Rprofile.site
RUN R -e 'install.packages("remotes")'
RUN Rscript -e 'remotes::install_version("glue",upgrade="never", version = "1.6.2")'
RUN Rscript -e 'remotes::install_version("rlang",upgrade="never", version = "1.1.2")'
RUN Rscript -e 'remotes::install_version("stringr",upgrade="never", version = "1.5.1")'
RUN Rscript -e 'remotes::install_version("knitr",upgrade="never", version = "1.45")'
RUN Rscript -e 'remotes::install_version("waldo",upgrade="never", version = "0.5.2")'
RUN Rscript -e 'remotes::install_version("shiny",upgrade="never", version = "1.8.0")'
RUN Rscript -e 'remotes::install_version("rmarkdown",upgrade="never", version = "2.25")'
RUN Rscript -e 'remotes::install_version("config",upgrade="never", version = "0.3.2")'
RUN Rscript -e 'remotes::install_version("spelling",upgrade="never", version = "2.2.1")'
RUN Rscript -e 'remotes::install_version("shinytest2",upgrade="never", version = "0.3.1")'
RUN Rscript -e 'remotes::install_version("ggplot2movies",upgrade="never", version = "0.0.1")'
RUN Rscript -e 'remotes::install_version("logger",upgrade="never", version = "0.2.2")'
RUN Rscript -e 'remotes::install_version("golem",upgrade="never", version = "0.4.1")'
RUN Rscript -e 'remotes::install_version("ggplot2",upgrade="never", version = "3.4.4")'
RUN Rscript -e 'remotes::install_github("rstudio/htmltools@a8a3559edbfd9dda78418251e69273fa9dfeb9bc")'
RUN Rscript -e 'remotes::install_github("r-lib/testthat@fe50a222c62cc8733b397690caf3b2a95856f902")'
RUN mkdir /build_zone
ADD . /build_zone
WORKDIR /build_zone
RUN R -e 'remotes::install_local(upgrade="never")'
RUN rm -rf /build_zone
EXPOSE 80
CMD R -e "options('shiny.port'=80,shiny.host='0.0.0.0');library(gap);gap::run_app()"
```
- [`golem::add_dockerfile_with_renv()`](https://thinkr-open.github.io/golem/reference/dockerfiles.html) creates a `tmp/deploy` folder and adds the following files:
```{bash}
#| eval: false
#| code-fold: false
#| include: true
deploy/
├── Dockerfile
├── Dockerfile_base
├── README
├── gap_0.0.0.9000.tar.gz
└── renv.lock.prod
```
- `README`
```{bash}
#| eval: false
#| code-fold: false
#| include: true
#| code-overflow: scroll
docker build -f Dockerfile_base --progress=plain -t gap_base .
docker build -f Dockerfile --progress=plain -t gap:latest .
docker run -p 80:80 gap:latest
# then go to 127.0.0.1:80
```
- `Dockerfile`
```{bash}
#| eval: false
#| code-fold: true
#| code-summary: 'show/hide Dockerfile'
#| include: true
#| code-overflow: scroll
FROM gap_base
COPY renv.lock.prod renv.lock
RUN R -e 'renv::restore()'
COPY gap_*.tar.gz /app.tar.gz
RUN R -e 'remotes::install_local("/app.tar.gz",upgrade="never")'
RUN rm /app.tar.gz
EXPOSE 80
CMD R -e "options('shiny.port'=80,shiny.host='0.0.0.0');library(gap);gap::run_app()"
```
- `Dockerfile_base`
```{bash}
#| eval: false
#| code-fold: true
#| code-summary: 'show/hide Dockerfile_base'
#| include: true
#| code-overflow: scroll
FROM rocker/verse:4.3.2
RUN apt-get update -y && apt-get install -y make zlib1g-dev git libicu-dev && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /usr/local/lib/R/etc/ /usr/lib/R/etc/
RUN echo "options(renv.config.pak.enabled = FALSE, repos = c(CRAN = 'https://cran.rstudio.com/'), download.file.method = 'libcurl', Ncpus = 4)" | tee /usr/local/lib/R/etc/Rprofile.site | tee /usr/lib/R/etc/Rprofile.site
RUN R -e 'install.packages("remotes")'
RUN R -e 'remotes::install_version("renv", version = "1.0.3")'
COPY renv.lock.prod renv.lock
RUN R -e 'renv::restore()'
```
- `gap_0.0.0.9000.tar.gz` is a compressed version of our app-package to deploy in the Docker container.
- `renv.lock.prod` is a JSON file with a list of packages used in our app-package.
You can read more details about deploying with Docker on the [Shiny Frameworks](https://mjfrigaard.github.io/sfw/) supplemental website.
[^golem-devtools-deploy]: This also includes a call to `rhub::check_for_cran()`, which may or may not be of concern for your application.
[^golem-deploy-posit-options]: These functions will create and `app.R` file to launch and deploy your application.
[^golem-deploy-rsconnect-options]: Includes boilerplate for `appName`, `appTitle`, `appFiles`, etc.
## Summary of `golem` features
`golem`s helper functions and `dev` scripts make application development fast--I was able to create `gap` quickly, and all of the supporting packages (`covrpage`, `attachment`, `spelling`) make the development process faster/easier:
1. The two modules (`mod_plot` and `mod_var`) are easily created with [`add_module()`](https://thinkr-open.github.io/golem/reference/add_module.html), the utility function with [`add_utils()`](https://thinkr-open.github.io/golem/reference/file_creation.html)
2. We can easily add the modules to the `app_ui()` and `app_server()`
3. App images are moved into `inst/app/www/`
4. The movies data was added to `inst/extdata/`, then read into `data/` folder with the `data-raw/movies.R` file.
5. For documentation, the [`attachment::att_amend_desc()`](https://thinkr-open.github.io/attachment/reference/att_amend_desc.html) function quickly captures any dependencies
6. Finally, I loaded, documented, and installed the `gap` package and ran the application with `gap::run_app()`
If you've followed along with the preceding chapters, the `golem` framework will be familiar. In essence, `golem` takes many of the package development steps we've covered and bundles them into wrapper functions (i.e., [`add_module()`](https://thinkr-open.github.io/golem/reference/add_module.html) is similar to running `usethis::use_r()` and `usethis::use_test()`, then adding an `roxygen2` skeleton).
## Dependencies
It's also worth noting that using the `golem` framework adds `golem` as a dependency:
```{r}
#| eval: false
#| code-fold: false
# in the 18_golem branch of sap
pak::local_deps_explain(deps = 'golem', root = ".")
```
```{verbatim}
gap -> golem
```
### [`gap`]{style="font-weight: bold; font-size: 1.10em;"} dependencies
```{r}
#| label: git_box_11d_inst-prod
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "standard",
fig_pw = '75%',
branch = "09d_inst-prod",
repo = 'sap')
```
For comparison, this is the `sap` dependency tree (note that using `devtools`/`usethis` doesn't make our app-package depend on these packages).
```{r}
#| eval: false
#| code-fold: false
# in the 09d_inst-prod branch of sap
pak::local_deps_explain(deps = 'devtools', root = ".")
```
```{verbatim}
x devtools
```
```{r}
#| eval: false
#| code-fold: false
# in the 09d_inst-prod branch of sap
pak::local_deps_explain(deps = 'usethis', root = ".")
```
```{verbatim}
x usethis
```
## Recap {.unnumbered}
```{r}
#| label: co_box_recap
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "g",
look = "default", hsize = "1.10", size = "1.05",
header = "RECAP   ![](images/goelm.png){width='10%'}",
fold = FALSE,
contents = "
<br>
The `golem` framework is a huge time saver if you're familiar with R package development (and you'd prefer if many of these steps were bundled and optimized for 'production grade' Shiny apps). However, if you're not familiar with package development, you might end up with app-packages that have bugs you can't diagnose or fix.")
```
```{r}
#| label: git_contrib_box
#| echo: false
#| results: asis
#| eval: true
git_contrib_box()
```