-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependencies.qmd
1368 lines (1033 loc) · 47.9 KB
/
dependencies.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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Dependencies {#sec-depends}
```{r}
#| eval: true
#| echo: false
#| include: false
source("_common.R")
```
```{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 `NAMESPACE` & `DESCRIPTION`", fold = TRUE,
contents = "
Managing dependencies:
- **Exports**: export objects from using `@export`. Exported functions are the functions that an app-package offers to the world (i.e., someone installs and loads the package, these are the functions they can directly use).
- **Imports**: import functions from add-on packages using a 'fully qualified variable reference' (i.e., `pkg::fun()`) in the code below `R/` and add the package name to the `Imports` field in the `DESCRIPTION`.\n
- If the object can't be imported using `::` (i.e., an operator), use the `@importFrom` tag from `roxygen2`\n
- If your code uses a lot of functions from another package (such as `shiny` in app-packages), use the `@import` tag from `roxygen2`\n
**Workflow:** List the add-on package in the `Imports` field of the `DESCRIPTION` file (i.e., with `usethis::use_package('pkg')`), then decide if you're going to the functions in `pkg` with `pkg::fun()` (preferred), `@importFrom`, or `@import`.
"
)
```
---
Dependencies are the must-have components for your app-package, and they can be divided into imports and exports.
- **Imports** are the functions we're borrowing from add-on/third party packages (i.e., any packages not automatically loaded in a new R session).
- **Exports** are the functions, data, and other R objects our app-package offers to users.
In this chapter we'll cover how to manage dependencies in your new app-package.
```{r}
#| label: shinypak_apps
#| echo: false
#| results: asis
#| eval: true
shinypak_apps(regex = "^06", branch = "06.1_exports")
```
Dependencies are handled with in the `NAMESPACE` directives (generated via `roxygen2` tags) and three fields in the `DESCRIPTION` file (`Suggests`, `Imports`, or `Depends`). Together, these files determine which functions and packages our app-package depends on, and which functions and object we make available to users of our app-package. This chapter will pick up where we left off with the [`05_roxygen2`](document.qmd) branch of `sap`.
Below is a folder tree of it's contents:
```{verbatim}
#| eval: false
#| code-fold: false
sap/
├── DESCRIPTION
├── NAMESPACE
├── R/
│ ├── mod_scatter_display.R
│ ├── mod_var_input.R
│ ├── launch_app.R
│ ├── movies_server.R
│ ├── movies_ui.R
│ └── utils.R
├── README.md
├── app.R
├── man/ # <1>
│ ├── mod_scatter_display_server.Rd
│ ├── mod_scatter_display_ui.Rd
│ ├── mod_var_input_server.Rd
│ ├── mod_var_input_ui.Rd
│ ├── launch_app.Rd
│ ├── movies_server.Rd
│ ├── movies_ui.Rd
│ └── scatter_plot.Rd # <1>
├── movies.RData
├── sap.Rproj
└── www/
└── shiny.png
4 directories, 21 files
```
1. The `man` folder now contains the help (`.Rd`) files for the functions in `R/`
```{r}
#| label: co_box_roxygen2_recap
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "b",
fold = TRUE,
look = "default", hsize = "1.10", size = "1.05",
header = "Chapter 4 recap: documenting functions with `roxygen2`",
contents = "
**Required `@tags` for all functions**:
- Make sure all functions have a documented title & description (`@title` and `@description` tags optional), function inputs and outputs (`@param` and `@return`), and demonstrations of how the function works (`@examples`)
**Shiny-specific documentation**:
- Use `@seealso` to link module UI and server functions, and `@family` to link functions within a similar topic (i.e., 'import data' or 'scatter plot')
- Provide shiny-specific information (use within the app, reactive state, more details about the `@param`s, etc.) in `@section` blocks.
See the [documentation](document.qmd) chapter for more information
"
)
```
[When in doubt...load, document, and install]{style="font-weight: bold; font-size: 1.20em"}
During development, you might lose track of the last `devtools` function you called (I know I do). If this happens, I've found loading, documenting, and installing helps to re-orient me to the current state of the package.
```{r}
#| label: dev_key_load_intro
#| echo: false
#| results: asis
#| eval: true
hot_key("L")
```
```{verbatim}
#| eval: false
#| code-fold: false
ℹ Loading sap
```
```{r}
#| label: dev_key_doc_intro
#| echo: false
#| results: asis
#| eval: true
hot_key("D")
```
```{verbatim}
#| eval: false
#| code-fold: false
==> devtools::document(roclets = c('rd', 'collate', 'namespace'))
ℹ Updating sap documentation
ℹ Loading sap
Documentation completed
```
```{r}
#| label: dev_key_install_intro
#| echo: false
#| results: asis
#| eval: true
hot_key("B")
```
```{verbatim}
#| eval: false
#| code-fold: false
==> R CMD INSTALL --preclean --no-multiarch --with-keep.source sap
* installing to library ‘/path/to/local/install/sap-090c61fc/R-4.2/x86_64-apple-darwin17.0’
* installing *source* package ‘sap’ ...
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (sap)
Restarting R session...
```
It's also satisfying to see all three functions execute without any errors!
## Identifying dependencies {.unnumbered}
The first step in managing dependencies is identifying which add-on packages `sap` relies on.[^depends-pkg-ns] The diagram below outlines the basic process for importing functions from add-on packages to use in our locally developed package, and then exporting those functions for people to use when they install/load our package.
```{=html}
<style>
.codeStyle span:not(.nodeLabel) {
font-family: monospace;
font-size: 1.75rem;
font-weight: bold;
color: #9753b8 !important;
background-color: #faf9ed;
padding: 0.2em;
}
</style>
```
```{mermaid}
%%| fig-cap: 'Handling package dependencies'
%%| fig-align: center
%%{init: {'theme': 'neutral', 'themeVariables': { 'fontFamily': 'monospace', "fontSize":"13px"}}}%%
flowchart TD
subgraph RPkg["R Package:<code>pkg</code>"]
fun("Has <code>fun()</code> function")
end
subgraph SapPkg["<code>sap</code> Package"]
sap("Uses <code>pkg::fun()</code> in local<br><code>foo()</code> function")
end
Users("Users install/load <code>sap</code> to use <code>foo()</code>")
RPkg -->|"import <code>fun()</code> from <code>pkg</code>"| SapPkg
SapPkg -->|"export <code>foo()</code> from <code>sap</code>"| Users
```
Our goal is to limit the dependencies to only those critical to the functioning of our app, because each additional dependency is a potential point of failure (should this package become unavailable or significantly change).
[^depends-pkg-ns]: I've made this process somewhat easier by explicitly namespacing all of the add-on package functions in `sap` (i.e., with `pkg::fun()`).
A great place to start is our `app.R` file:
```{r}
#| eval: false
#| code-fold: false
# pkgs <- c("shiny", "shinythemes", "stringr", "ggplot2", "rlang")
# install.packages(pkgs, quiet = TRUE)
# packages ------------------------------------
library(shiny)
library(shinythemes)
library(stringr)
library(ggplot2)
library(rlang)
# launch_app ------------------------------------
launch_app()
```
Ideally, we'll want to replace these calls to `libary()`, but first we have to make sure the functions we're using in these packages will be available in `sap`.
When we run the contents of `app.R`, we see the following:
```{verbatim}
#| eval: false
#| code-fold: false
> launch_app()
Error in launch_app() : could not find function "launch_app"
```
Why can't R find the `"launch_app"` function in `app.R`?
Let's recap what we've done so far:
::: {layout-ncol=2}
### [`app.R`]{style="font-size: 1.05em"} {.unnumbered}
1. The `app.R` file loads the necessary packages and calls `launch_app()`:
### {.unnumbered}
```{verbatim}
sap/
└── app.R
```
:::
::: {layout-ncol=2}
### [`R/`]{style="font-size: 1.05em"} {.unnumbered}
2. The `R/launch_app.R` file contains the code and `roxygen2` documentation for `launch_app()` function:
### {.unnumbered}
```{verbatim}
sap/
└── R/
└── launch_app.R
```
:::
::: {layout-ncol=2}
### [`man/`]{style="font-size: 1.05em"} {.unnumbered}
3. `roxygen2` generates the `man/launch_app.Rd` documentation file:
### {.unnumbered}
```{verbatim}
sap/
└── man/
└── launch_app.Rd
```
:::
The error above is telling us that despite having documentation for `launch_app()` in the `R/` folder *and* generating the corresponding `.Rd` file in `man/`, `launch_app()` isn't being **exported** from `sap`.
## Package exports {#sec-depends-exports}
The exact cause of the error above becomes more apparent when we try to explicitly namespace `launch_app()` from `sap`:[^rpks-depends-in-practice]
[^rpks-depends-in-practice]: Read more in the [Exports section of R Packages, 2nd Ed](https://r-pkgs.org/dependencies-in-practice.html#exports)
```{r}
#| eval: false
#| code-fold: false
sap::launch_app()
```
```{verbatim}
Error: 'launch_app' is not an exported object from 'namespace:sap'
```
```{r}
#| label: git_box_06.1_pkg-imports
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '75%',
branch = "06.1_pkg-exports",
repo = 'sap')
```
To make the `launch_app()` function available to users of our package, we need to export it by including the `@export` tag in the `roxygen2` comment block:
- **`@export`**: make function available to users of sap.
```{r}
#| eval: false
#| code-fold: false
#' @export my_func #<1>
#' my_func <- function() {
#'
#' }
```
1. Placed above the function we want to export (function name is not required)
### Export [`launch_app()`]{style="font-size: 1.00em;"} {.unnumbered}
We'll export `launch_app()` from `sap` by placing the `@export` tag above the function in `R/launch_app.R`:
```{r}
#| eval: false
#| code-fold: false
#' Launch the Movies Review Application
#'
#' Starts the Movies Review Shiny application, which provides a customizable
#' scatter plot interface for analyzing movie data.
#'
#' @return A **Shiny application** object.
#'
#' @section Details:
#' The application uses:
#' - **UI**: Defined in [`movies_ui()`].
#' - **Server Logic**: Defined in [`movies_server()`].
#'
#' @seealso
#' - [`movies_ui()`] for the user interface.
#' - [`movies_server()`] for the server logic.
#'
#' @family **Standalone Application**
#'
#' @examples
#' if (interactive()) {
#' launch_app()
#' }
#'
#' @export
launch_app <- function() {
shiny::shinyApp(ui = movies_ui, server = movies_server)
}
```
In `app.R`, we'll replace the calls to `library()` with a single call to `library(sap)`
```{r}
#| eval: false
#| code-fold: false
# packages ------------------------------------
library(sap)
# launch_app ------------------------------------
launch_app()
```
We'll document the package to generate the `NAMESPACE` changes:
```{r}
#| label: dev_key_all_01
#| echo: false
#| results: asis
#| eval: true
hot_key("D")
```
Now, when we run the code `app.R`, we see the following:
![`launch_app()`](images/dep_exported_app_fun.png){width=100%}
:::{.column-margin}
We've lost the Shiny icon (`www/shiny.png`) in the UI, but we'll address this in **@sec-resources**.
:::
`launch_app()` launches our application!
<!-- first dimension defines rows and the second columns -->
<!-- "layout="[[1,1], [1]]" translates to: create two rows, the first of which has two columns of equal size and the second of which has a single column. -->
The `NAMESPACE` file now contains a single export (`launch_app`), and when we enter `sap::` in the **Console**, we see the `launch_app()` function help file in the tab completion.
::: {#fig-dep_exported_namespace layout="[40,60]"}
![updated `NAMESPACE`](images/dep_exported_namespace.png){#fig-dep_exported_namespace width=100%}
![`launch_app()` from the `sap` namespace](images/dep_exported_launch_app.png){#fig-03_dependencies_namespace width=100%}
The `launch_app()` is now part of the `sap` namespace
:::
### What [`@export`]{style="font-size: 1.00em;"} does {.unnumbered}
We'll pause here to notice a few things about what `@export` does. When we documented our package, the code was automatically loaded before the `NAMESPACE` was updated with `export(launch_app)`.
```{bash}
#| eval: false
#| code-fold: false
> devtools::document()
ℹ Updating sap documentation
ℹ Loading sap #<1>
Writing NAMESPACE
```
1. Call to `devtools::load_all()`
`document()` will call `load_all()` to make sure all the changes in the `R/` folder are included in the updated documentation.
```{=html}
<style>
.codeStyle span:not(.nodeLabel) {
font-family: monospace;
font-size: 1.75rem;
font-weight: bold;
color: #9753b8 !important;
background-color: #faf9ed;
padding: 0.2em;
}
</style>
```
```{mermaid}
%%| fig-align: center
%%| fig-cap: 'What @export does'
%%{init: {'theme': 'neutral', 'themeVariables': { 'fontFamily': 'monospace', "fontSize":"13px"}}}%%
flowchart
subgraph R["<strong>R/ folder</strong>"]
Tag("Add <code>@export</code><br>to function<br>documentation")
end
subgraph NS["<strong>NAMESPACE</strong>"]
Exported(["<code>export(launch_app)</code>"])
end
subgraph Man["<strong>man/ folder</strong>"]
RdFile(["<code>.Rd</code> files created"])
end
Document[["Run <code>document()</code>"]]
Load("Calls <code>load_all()</code>")
Tag --> Document
Document -.-> Load --> NS & Man
```
We can confirm `launch_app()` has been exported with `ls()`, which returns "*the names of the objects in the specified environment*.
```{r}
#| eval: false
#| code-fold: false
ls(name = "package:sap")
```
```{verbatim}
#| eval: false
#| code-fold: false
[1] "launch_app"
```
#### The [`search()`]{style="font-size: 1.00em;"} list {.unnumbered}
`library(sap)` attaches `sap` to the search list. We can view all the attached packages in the string returned from `search()`:
```{verbatim}
#| eval: false
#| code-fold: false
"package:sap" %in% search()
```
```{verbatim}
#| eval: false
#| code-fold: false
[1] TRUE
```
What about the add-on/third-party package functions `launch_app()` relies on, like `ggplot2`? Let's check to see if `ggplot2` is also attached to the `search()` list:
```{r}
#| eval: false
#| code-fold: false
c("package:ggplot2") %in% search()
```
```{verbatim}
#| eval: false
#| code-fold: false
[1] FALSE
```
Why does this matter? Because if these packages aren't attached to the `search()` list, we can't call their functions directly (the way we would if we'd loaded the package with `library()`). For example, if we try to use `ggplot2` to build a plot (similar to the one we have in the app), we see the following:
``` r
ggplot(data = mtcars,
aes(x = disp, y = mpg)) +
geom_point()
# Error in ggplot(data = mtcars, aes(x = disp, y = mpg)) :
# could not find function "ggplot"
```
We can use the add-on/third-party package functions `sap` relies on, but we need to explicitly namespace these functions from their original package namespaces (i.e., using `pkg::fun()`):
:::{layout="[40,60]" layout-valign="top"}
``` r
ggplot2::ggplot(data = mtcars,
ggplot2::aes(x = disp, y = mpg)) +
ggplot2::geom_point()
```
![](images/dep_using_addon_funs.png){width=100%}
:::
We can use `ggplot2` if we explicitly namespace it's functions
```{r}
#| label: co_box_accessing_exports
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "g",
look = "default", hsize = "1.10", size = "1.05",
header = "Accessing add-on package functions",
contents = "
***What happens to add-on/third party functions that are exported from `sap`?***
When a user loads `sap` with `library(sap)`, any add-on/third-party package functions used in exports are available to users if they use `pkg::fun()` (or if they load the package themselves with `library()`).",
fold = TRUE
)
```
Access to add-on/third-party package functions has implications for the other functions in `sap`--for example, the `scatter_plot()` function uses `ggplot2` functions. But we're not exporting `scatter_plot()`, so when we attempt to run the examples, we see the following error:
::: {#fig-dep_non_exported_examples}
![Error in `scatter_plot()` examples](images/dep_non_exported_examples.png){#fig-dep_non_exported_examples}
Examples in `scatter_plot()` function without exporting
:::
[Examples for 'sap::scatter_plot']{style="font-weight: bold; font-size: 1.15em;"}
The message at the top of the **Help** pane is informative because it tells us that despite `scatter_plot()` being functional when we run `launch_app()`, it's not part of the package namespace (and thus, not accessible to users in the help file).
### Export [`scatter_plot()`]{style="font-size: 1.0em;"} {.unnumbered}
Let's add the `@export` tag to `R/scatter_plot.R` so it's exported from `sap`.
```{r}
#| eval: false
#| code-fold: false
#' Create scatter plot
#'
#' Custom [`ggplot2`](https://ggplot2.tidyverse.org/) function for building
#' scatter plots in `sap`.
#'
#'
#' @param df `data.frame` or `tibble`
#' @param x_var string variable mapped to `x` axis
#' @param y_var string variable mapped to `y` axis
#' Generate a Scatter Plot
#'
#' Creates a scatter plot using `ggplot2` with the specified data and
#' aesthetics.
#'
#' @param df *(data.frame)* The dataset containing the variables to plot.
#' @param x_var *(character)* Name of the variable for the x-axis.
#' @param y_var *(character)* Name of the variable for the y-axis.
#' @param col_var *(character)* Name of the variable for the color aesthetic.
#' @param alpha_var *(numeric)* Transparency level of points (0 to 1).
#' @param size_var *(numeric)* Size of points.
#'
#' @return A `ggplot` object representing the scatter plot.
#'
#' @section Details:
#' `scatter_plot()` is designed for use in Shiny applications but can also be
#' used independently.
#' It supports customization of transparency, size, and color aesthetics.
#'
#' @seealso
#' - [`mod_scatter_display_server()`] for integrating this function into the
#' scatter plot module.
#' - [`ggplot2::ggplot()`](https://ggplot2.tidyverse.org/) for details on
#' `ggplot2` usage.
#'
#' @family **Utility Functions**
#'
#' @examples
#' scatter_plot(
#' df = mtcars,
#' x_var = "mpg",
#' y_var = "hp",
#' col_var = "cyl",
#' alpha_var = 0.7,
#' size_var = 3
#' )
#'
#' @export
```
After documenting `sap`, the `NAMESPACE` is updated with the `export()` [directive](https://r-pkgs.org/dependencies-mindset-background.html#sec-dependencies-NAMESPACE-file):
```{r}
#| label: dev_key_all_02
#| echo: false
#| results: asis
#| eval: true
hot_key("D")
```
The contents of the updated `NAMESPACE` file and typing `sap::` in the **Console** now displays the `scatter_plot()` help file in the tab completion:
:::: {.column-body-outset-right}
::: {#fig-deps_scatter_plot_exported layout="[40,60]"}
![`@export` the `scatter_plot` function](images/dep_scatter_plot_exported.png){#fig-deps_scatter_plot_exported width='100%' fig-align='center'}
![`scatter_plot()`](images/dep_scatter_plot_namespace.png){#fig-deps_scatter_plot_exported width='100%' fig-align='center'}
`scatter_plot()` is now part of the `sap` namespace
:::
::::
Below, we confirm users can access the help file for `scatter_plot()` and run the examples:
![Running examples in `?scatter_plot`](images/dep_run_examples_scatter_plot.png){width='100%' fig-align='center'}
#### [`loadedNamespaces()`]{style="font-size: 0.95em;"} {.unnumbered}
We've already confirmed that `ggplot2` isn't attached with `sap` (and hence, it is not included in the `search()` list)
```{r}
#| eval: false
#| code-fold: false
c("package:ggplot2") %in% search()
```
```{verbatim}
#| eval: false
#| code-fold: false
[1] FALSE
```
However, we can access the functions we used the `pkg::fun()` syntax with because those functions are included in the loaded namespaces (which we can view with `loadedNamespaces()`)
```{r}
#| eval: false
#| code-fold: false
c("ggplot2") %in% loadedNamespaces()
```
```{verbatim}
#| eval: false
#| code-fold: false
[1] TRUE
```
```{r}
#| label: co_box_accessing_non_exported_funs
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "g",
look = "default", hsize = "1.10", size = "1.05",
fold = TRUE,
header = "Accessing non-exported functions",
contents = "
***What about functions that _aren't_ exported from `sap`?***
Functions that are not exported (i.e., do not include the `@export` tag) are still accessible after installing and loading a package using the `pkg:::fun()`
"
)
```
### What to [`@export`]{style="font-size: 1.00em"} {.unnumbered}
> *'Always err on the side of caution, and simplicity. It's easier to give people more functionality than it is to take away stuff they're used to'* - [What to export, R Packages, 2ed](https://r-pkgs.org/dependencies-in-practice.html#what-to-export)
When determining which functions to export, consider the question: "*When a user installs and loads `sap`, what functions do I want to be available?*"
In app-packages, I'll take the following general approach:
- Start by exporting the standalone app function (`launch_app()`)
- Then selectively export modules and/or functions that perform distinct tasks with potentially reusable functionality (i.e., generate specific UI components, perform data processing tasks, etc.).
It's rare that I don't export functions from app-packages, but I like to make sure users have the ability to get 'under to hood' and see how each part of an application works.
If you'd like to the **Low-key `@export`s with `@keywords internal`** box below for exporting functions without including them in your package index.
```{r}
#| label: co_box_low_key_exports
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "g",
look = "default", hsize = "1.10", size = "1.05",
header = "Low-key exports with **@keywords internal**",
contents = "
If you'd like a function to be exported, but not listed in your app-package index, you can use `@export` in combination with `@keywords internal`:
\`\`\` r
#' @export
#'
#' @keywords internal
\`\`\`
For example, adding `@export` and `@keywords internal` to `R/scatter_plot.R` will make the function accessible to users and generate a help file:
![](images/depends_keywords_internal_namespace.png){width='90%' fig-align='center'}
But if a user were to click on the **Index** for `sap` (at the bottom of the help file) `scatter_plot` would not be listed:
![](images/depends_pkg_index.png){width='90%' fig-align='center'}
",
fold = TRUE
)
```
## Package imports {#sec-depends-imports}
```{r}
#| label: git_box_06.2_imports
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '75%',
branch = "06.2_imports",
repo = 'sap')
```
Importing dependencies is slightly more involved than exports because imports are managed by both the `DESCRIPTION` *and* the `NAMESPACE`:
1. The `DESCRIPTION` file handles **package-level dependencies**, specifying which add-on packages our app-package uses.
2. The `NAMESPACE` manages **function-level access**, importing functions from add-on packages to be used in our app-package, and--as we've seen above--exporting functions from our app-package for others to use.
### Package-level depencencies {.unnumbered}
The `DESCRIPTION` file manages dependencies with three fields: `Depends`, `Imports`, and `Suggests`. **Most add-on packages belong under the `Imports` field (i.e., functions from these packages are used in the code below `R/`)**.[^package-remotes]
#### [`Depends`]{style="font-size: 1.00em"} {#sec-pkg-depends-description .unnumbered}
Packages listed under `Depends` are essential for our app-package to work. These packages will be attached *before* our package when `library(sap)` is called.
#### [`Imports`]{style="font-size: 1.00em"} {#sec-pkg-imports-description .unnumbered}
Packages listed under `Imports` are necessary for our app-package to work. These packages are loaded (but not attached) when our app-package is installed.
#### [`Suggests`]{style="font-size: 1.00em"} {#sec-pkg-suggests-description .unnumbered}
The `Suggests` field should include any packages that enhance our app-package, but aren't necessary for the basic functionality. This might include packages used in examples, vignettes, tests, etc.
```{r}
#| label: co_box_depends_hell
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "r",
look = "default", hsize = "1.10", size = "1.05",
fold = TRUE,
header = "Avoiding dependency hell",
contents = "Generally speaking, you want to keep your app-package lightweight (i.e., limit the number of add-on/third-party dependencies, other than `base`-R packages and `shiny`). Doing this ensures you'll be able to safely use this app-packages as a dependencies in the next app-packages. We'll cover tracking and exploring dependencies in @sec-entanglement.")
```
### Function-level access {.unnumbered}
Function-level access is managed using namespace-qualified references (or 'explicit namespacing') in the code below `R/`. The `NAMESPACE` can also be used to include add-on packages or functions with the `@import` and `@importFrom` tags.[^imports-rpkgs-importFrom-import]
1. **Namespace-qualified referencing**: Refer to add-on package functions using `pkg::fun()` syntax in the code below `R/`.
2. **Special imports**: `@importFrom` should be used when 1) *"You can't call an operator from another package via `::`"* 2) *"importing a function makes your code much more readable"* (**not** easier to write)
3. **Importing everything**: `@import` should be used if *"you make such heavy use of so many functions from another package that you want to import its entire namespace"*
[^package-remotes]: Additional fields exists (i.e., `Remotes`), but these are [special circumstances](https://r-pkgs.org/dependencies-in-practice.html#depending-on-the-development-version-of-a-package).
[^imports-rpkgs-importFrom-import]: Read more about this in the section titled, ['In code below R/' in R Packages, 2ed](https://r-pkgs.org/dependencies-in-practice.html#sec-dependencies-in-imports-r-code)
### Handling imports {#sec-depends-pkg-fun-syntax .unnumbered}
The workflow I use to manage add-on dependencies comes from the advice in the `roxygen2` documentation:
> *"if you are using just a few functions from another package, we recommending adding the package to the `Imports:` field of the `DESCRIPTION` file and calling the functions explicitly using `::`, e.g., `pkg::fun()`...*"
>
> *..."If the repetition of the package name becomes annoying you can `@importFrom` and drop the `pkg::fun()`"*. - [**Importing functions**](https://roxygen2.r-lib.org/articles/namespace.html#functions)
1. Include the add-on package to the `Imports` field with `usethis::use_package()`.
2. Refer to add-on functions using explicit namespacing (i.e., `pkg::fun()`) in the code beneath `R/`.
We have some special considerations for the imported add-on functions in our app-package:
#### Using [`@import`]{style="font-size: 1.0em; font-weight: bold;"} {#sec-depends-import .unnumbered}
A substantial portion of the code in `sap` comes from `shiny`, so we'll remove the explicit namespacing and place the `@import` tag in `R/launch_app.R`[^ms-imports]
[^ms-imports]: Using `@import` is not generally considered best practice, but it makes sense for app-packages: *...for Shiny apps, I recommend using `@import shiny` to make all the functions in the Shiny package easily available.* [Mastering Shiny, R CMD check](https://mastering-shiny.org/scaling-packaging.html#r-cmd-check)"
```{r}
#| eval: false
#| code-fold: show
#| code-summary: 'show/hide R/launch_app.R'
#' Launch the Movies Review Application
#'
#' Starts the Movies Review Shiny application, which provides a customizable
#' scatter plot interface for analyzing movie data.
#'
#' @return A **Shiny application** object.
#'
#' @section Details:
#' The application uses:
#' - **UI**: Defined in [`movies_ui()`].
#' - **Server Logic**: Defined in [`movies_server()`].
#'
#' @seealso
#' - [`movies_ui()`] for the user interface.
#' - [`movies_server()`] for the server logic.
#'
#' @family **Standalone Application**
#'
#' @examples
#' if (interactive()) {
#' launch_app()
#' }
#'
#' @import shiny #<1>
#'
```
1. Import entire shiny package namespace
#### Using [`@importFrom`]{style="font-size: 1.0em; font-weight: bold;"} {#sec-depends-import-from .unnumbered}
`.data` can't be exported using `::`, so we'll include `@importFrom` in `R/scatter_plot.R`. On the other hand, `ggplot2` has over 400 functions, so we'll add the package to the `Imports` field and use the namespace-qualified references.[^ggplot2-in-pkgs]
[^ggplot2-in-pkgs]: Read more about using `ggplot2` in packages in the section titled, '[Referring to `ggplot2` functions](https://ggplot2.tidyverse.org/dev/articles/ggplot2-in-packages.html#referring-to-ggplot2-functions)'
```{r}
#| code-fold: show
#| code-summary: 'show/hide R/scatter_plot.R'
#' Create scatter plot
#'
#' Custom [`ggplot2`](https://ggplot2.tidyverse.org/) function for building
#' scatter plots in `sap`.
#'
#'
#' @param df `data.frame` or `tibble`
#' @param x_var string variable mapped to `x` axis
#' @param y_var string variable mapped to `y` axis
#' Generate a Scatter Plot
#'
#' Creates a scatter plot using `ggplot2` with the specified data and
#' aesthetics.
#'
#' @param df *(data.frame)* The dataset containing the variables to plot.
#' @param x_var *(character)* Name of the variable for the x-axis.
#' @param y_var *(character)* Name of the variable for the y-axis.
#' @param col_var *(character)* Name of the variable for the color aesthetic.
#' @param alpha_var *(numeric)* Transparency level of points (0 to 1).
#' @param size_var *(numeric)* Size of points.
#'
#' @return A `ggplot` object representing the scatter plot.
#'
#' @section Details:
#' `scatter_plot()` is designed for use in Shiny applications but can also be
#' used independently.
#' It supports customization of transparency, size, and color aesthetics.
#'
#' @seealso
#' - [`mod_scatter_display_server()`] for integrating this function into the
#' scatter plot module.
#' - [`ggplot2::ggplot()`](https://ggplot2.tidyverse.org/) for details on
#' `ggplot2` usage.
#'
#' @family **Utility Functions**
#'
#' @examples
#' scatter_plot(
#' df = mtcars,
#' x_var = "mpg",
#' y_var = "hp",
#' col_var = "cyl",
#' alpha_var = 0.7,
#' size_var = 3
#' )
#'
#' @export
#'
#' @importFrom rlang .data #<1>
#'
```
1. Import a the `.data` operator from `rlang`
#### [`use_package('pkg')`]{style="font-size: 1.0em; font-weight: bold;"} {.unnumbered}
As an example, we'll add the [`bslib` package](https://rstudio.github.io/bslib/index.html) and update our app UI layout:
```{r}
#| eval: false
#| code-fold: false
usethis::use_package('bslib')
```
``` sh
✔ Setting active project to '/Users/mjfrigaard/projects/apps/sap'
✔ Adding 'bslib' to Imports field in DESCRIPTION
• Refer to functions with `bslib::fun()`
```
In `movies_ui()`, we'll change the `fluidPage()` to the `bslib::page_fillable()` and adjust move the data source information to the `bslib::card_footer()`:
```{r}
#| eval: false
#| code-fold: show
#| code-summary: 'updated movies_ui() bslib function'
#' User Interface for the Movies Review Application
#'
#' Creates the user interface (UI) for the Movies Review application, which
#' allows users to create customizable scatter plots based on movie data.
#'
#' @return A Shiny `tagList` object containing the UI elements.
#'
#' @section Details:
#' The interface is built using [`bslib`](https://rstudio.github.io/bslib/)
#' - **Page (fillable)**: [`bslib::page_fillable()`](https://rstudio.github.io/bslib/reference/page_fillable.html)
#' displays the app title.
#' - **Sidebar**: [`bslib::layout_sidebar()`](https://rstudio.github.io/bslib/reference/sidebar.html)
#' includes a logo and the variable
#' selection module.
#' ([`mod_var_input_ui`]).
#' - **Card**: [`bslib::card()`](https://rstudio.github.io/bslib/reference/card.html)
#' displays the scatter plot module
#' ([`mod_scatter_display_ui`]).
#'
#' @seealso
#' - [`movies_server()`] for the server logic of the app.
#' - [`mod_var_input_ui()`] and [`mod_scatter_display_ui()`] for the modules