You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/namespace.Rmd
+85-25Lines changed: 85 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -19,49 +19,109 @@ This is a little frustrating at first, but soon becomes second-nature.
19
19
20
20
## Exports
21
21
22
-
For a function to be usable outside your package, you must **export** it.
23
-
By default roxygen2 doesn't export anything from your package.
24
-
If you want an object to be publicly available, you must explicitly tag it with `@export`.
22
+
In order for your users to use a function[^1] in your package, you must **export** it.
23
+
In most cases, you can just use the `@export` tag, and roxygen2 will automatically figure out which `NAMESPACE` directive (e.g. `export()`, `exportS3method()`, `exportClasses()`, and `exportMethods()`) you need.
25
24
26
-
Use the following guidelines to decide what to export:
25
+
[^1]: Including S3 and S4 generics and methods.
27
26
28
-
-**Functions**: export functions that you want to make available.
29
-
Exported functions must be documented, and you must be cautious when changing their interface.
27
+
### Functions
28
+
29
+
Functions should be exported if you want your users to available.
30
+
If you export a function, you must also document it, and since other people will use it, you need to be careful if you later change the function interface.
30
31
31
-
-**Datasets**: all datasets are publicly available.
32
-
They exist outside of the package namespace and must not be exported.
32
+
```{r}
33
+
#' Add two numbers together
34
+
#'
35
+
#' @param x,y Two numbers
36
+
#' @export
37
+
add <- function(x, y) {
38
+
x + y
39
+
}
40
+
```
33
41
34
-
-**S3 classes**: if you want others to be able to create instances of the class `@export` the constructor function.
42
+
### S3
43
+
44
+
S3 generics work like regular R functions so export them using the advice above: if you want users to call this function, export; if it's purely for internal use, don't export it.
45
+
46
+
```{r}
47
+
#' Take an object to bizarro world
48
+
#'
49
+
#' @param x A vector.
50
+
#' @export
51
+
bizarro <- function(x, ...) {
52
+
UseMethod("bizarro")
53
+
}
54
+
```
35
55
36
-
-**S3 generics**: the generic is a function, so `@export` if you want it to be usable outside the package.
56
+
While S3 methods are regular functions with a special naming scheme, their "export" works a bit differently.
57
+
S3 methods are exported only in the sense that when you call the generic with the appropriate method; a user can't directly access the function definition by typing the name of the generic.
58
+
A more technically correctly term would be to say that the method is **registered** so that the generics can find it.
59
+
You must register, i.e. `@export`, every S3 method regardless of whether or not the generic is exported, and roxygen2 will warn you if you have forgotten.
37
60
38
-
-**S3 methods**: every S3 method *must* be exported, even if the generic is not.
39
-
Otherwise, the S3 method table will not be generated correctly and internal generics will not find the correct method.
40
-
See below for instructions on how to export a method for a generic in a suggested package.
Typically, you will write methods for generics that are either defined in the current package or a package that is a hard dependency[^2] of your package.
101
+
Sometimes, however, you will want to write a method for a suggested dependency.
102
+
In this case, `@export` will not work because it relies on being able to see the generic.
103
+
Instead, use `@exportS3Method`. This will use "delayed" method registration, which means the method will only be registered when the suggested package is loaded.
49
104
50
-
`@exportS3Method` tag allows you to generate `S3method()` namespace directives.
51
-
Its primary use is for "delayed" method registration, which allows you to define methods for generics found in suggested packages (R \>= 3.6).
52
-
For example,
105
+
[^2]: i.e. listed the `Imports` or `Depends` fields in your `DESCRIPTION`.
53
106
54
107
```{r}
55
108
#' @exportS3Method pkg::generic
56
109
generic.foo <- function(x, ...) {
57
110
}
58
111
```
59
112
60
-
will generate
113
+
### S4
114
+
115
+
-**Classes**: export the class object if you want others to be able to extend it.
116
+
117
+
-**Generics:** treat it like a function and `@export` if user facing.
118
+
119
+
-**Methods**: you only need to `@export` a method, if the generic lives in another package.
61
120
62
-
S3method(pkg::generic, foo)
121
+
### Datasets
63
122
64
-
Which will cause the method to be registered only when pkg is loaded.
123
+
Note that datasets should never be exported as they use a different mechanism.
124
+
Instead, datasets will either be automatically exported if you set `LazyData: true` in your `DESCRIPTION`, or made available after calling `data()` if not.
65
125
66
126
### Manual exports
67
127
@@ -85,15 +145,15 @@ The `NAMESPACE` also controls which functions from other packages are made avail
85
145
86
146
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()`.
87
147
88
-
```r
148
+
```r
89
149
my_function<-function(x, y) {
90
150
pkg::fun(x) *y
91
151
}
92
152
```
93
153
94
154
If the repetition of the package name becomes annoying you can `@importFrom` and drop the `::`:
0 commit comments