10

I built my first R package months ago and I now realise some of my older functions are looking a bit dated. I'm already writing better functions to replace them.

I've seen how other R packages warn of deprecated functions, and redirect users to the newer functions. I want to do the same.

How do I mark a function as deprecated in R? Do I just set a warning?

4
  • 3
    see ?.Deprecated
    – alistaire
    Jun 19, 2017 at 3:56
  • 2
    That did it. Thanks @alistaire. Should I just delete this question?
    – lebelinoz
    Jun 19, 2017 at 3:59
  • 1
    Instead of deleting, maybe provide a small example as an answer?
    – zx8754
    Jun 19, 2017 at 6:26
  • 1
    @zx8754 Done! Thanks
    – lebelinoz
    Jun 19, 2017 at 6:34

1 Answer 1

23

The answer is to call the .Deprecated function from base R:

f_old = function(x) {
  .Deprecated("f_new")
  return(x * x)
}

f_new = function(x) {
  return(x^2)
}

This will give the appropriate warning:

> f_old(4)
[1] 16
Warning message:
'f_old' is deprecated.
Use 'f_new' instead.
See help("Deprecated") 
2
  • 6
    In this example one would probably call f_new in f_old.
    – Roland
    Jun 19, 2017 at 6:54
  • 3
    @Roland sometimes yes, sometimes no. If the old function is deprecated because it does things differently (worse) than the new one, then you have to decide whether the damage is worse from changing people's results out from under them (potentially breaking their code or tests), or from giving them results you think are worse than the new results. IMO usually the point of deprecating is to give people their own migration period away from the old code, while keeping the old code in place unchanged. Sep 21, 2021 at 14:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.