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
By and large, I think the description of what you should do is good, but the code (IMO) is overly complicated, so I've proposed some simpler alternatives.
Copy file name to clipboardExpand all lines: maintenance_evolution.Rmd
+14-17Lines changed: 14 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,11 @@ Everyone's free to have their own opinion about how freely parameters/functions/
14
14
15
15
Sometimes parameter names must be changed for clarity, or some other reason.
16
16
17
-
A possible approach is to catch all parameters passed in to the function and check against some list of parameters, and stop or warn with a meaningful message.
17
+
A possible approach is check if deprecated arguments are not missing, and stop a meaningful message.
18
18
19
19
```r
20
20
foo_bar<-function(x, y) {
21
-
calls<- names(sapply(match.call(), deparse))[-1]
22
-
if(any("x"%in%calls)) {
21
+
if (!missing(x)) {
23
22
stop("use 'y' instead of 'x'")
24
23
}
25
24
y^2
@@ -29,12 +28,12 @@ foo_bar(x = 5)
29
28
#> Error in foo_bar(x = 5) : use 'y' instead of 'x'
30
29
```
31
30
32
-
Or instead of stopping with error, you could check for use of `x` parameter and set it to `y` internally.
31
+
If you want to be more helpful, you could emit a warning but automatically take the necessary action:
33
32
34
33
```r
35
34
foo_bar<-function(x, y) {
36
-
calls<- names(sapply(match.call(), deparse))[-1]
37
-
if(any("x"%in%calls)) {
35
+
if (!missing(x)) {
36
+
warning("use 'y' instead of 'x'")
38
37
y<-x
39
38
}
40
39
y^2
@@ -44,7 +43,7 @@ foo_bar(x = 5)
44
43
#> 25
45
44
```
46
45
47
-
Be aware of the parameter `...`. If your function has `...`, and you have already removed a parameter (lets call it `z`), a user may have older code that uses `z`. When they pass in `z`, it's not a parameter in the function definition, and will likely be silently ignored -- not what you want. So, do make sure to always check for removed parameters moving forward since you can't force users to upgrade.
46
+
Be aware of the parameter `...`. If your function has `...`, and you have already removed a parameter (lets call it `z`), a user may have older code that uses `z`. When they pass in `z`, it's not a parameter in the function definition, and will likely be silently ignored -- not what you want. Instead, leave the argument around, throwing an error if it used.
48
47
49
48
## Functions: changing function names
50
49
@@ -72,21 +71,19 @@ bar <- foo
72
71
73
72
With the above solution, the user can use either `foo()` or `bar()` -- either will do the same thing, as they are the same function.
74
73
75
-
It's also useful to have a message but then you'll only want to throw that message when they use the old function name, e.g.,
74
+
It's also useful to have a message but then you'll only want to throw that message when they use the old function, e.g.,
76
75
77
76
```r
78
77
#' foo - add 1 to an input
79
78
#' @export
80
79
foo<-function(x) {
81
-
if (as.character(match.call()[[1]]) =="foo") {
82
-
warning("please use bar() instead of foo()", call.=FALSE)
83
-
}
84
-
x+1
80
+
warning("please use bar() instead of foo()", call.=FALSE)
81
+
bar(x)
85
82
}
86
83
87
84
#' @export
88
85
#' @rdname foo
89
-
bar<-foo
86
+
bar<-function(x) x+1
90
87
```
91
88
92
89
After users have used the package version for a while (with both `foo` and `bar`), in the next version you can remove the old function name (`foo`), and only have `bar`.
@@ -103,11 +100,11 @@ To remove a function from a package (let's say your package name is `helloworld`
103
100
104
101
* Mark the function as deprecated in package version `x` (e.g., `v0.2.0`)
105
102
106
-
In the function itself, use `.Deprecated()`like:
103
+
In the function itself, use `.Deprecated()`to point to the replacement function:
107
104
108
105
```r
109
106
foo<-function() {
110
-
.Deprecated(msg="'foo' will be removed in the next version")
107
+
.Deprecated("bar")
111
108
}
112
109
```
113
110
@@ -139,7 +136,7 @@ In the function itself, use `.Defunct()` like:
139
136
140
137
```r
141
138
foo<-function() {
142
-
.Defunct(msg="'foo' has been removed from this package")
139
+
.Defunct("bar")
143
140
}
144
141
```
145
142
@@ -149,7 +146,7 @@ In addition, it's good to add `...` to all defunct functions so that if users pa
149
146
150
147
```r
151
148
foo<-function(...) {
152
-
.Defunct(msg="'foo' has been removed from this package")
0 commit comments