17

I am creating an R package in RStudio. Say I have two functions fnbig() and fnsmall() in my package named foo. fnbig() is a function that must be accessible to the user using the package. fnsmall() is an internal function that must not accessible to the user but should be accessible inside of fnbig().

# package code
fnsmall <- function()
{
bla bla..
}

#' @export
fnbig <- function()
{
bla bla..
x <- fnsmall()
bla..
}

I have tried exporting fnsmall(). All works but it litters the NAMESPACE. I tried not exporting fnsmall(), but then it doesn't work inside fnbig() when using x <- fnsmall() or x <- foo::fnsmall(). Then I tried to use x <- foo:::fnsmall(), and it works. But I read that using :::is not recommended.

What is the best way to go about doing this? How do I call an internal function from an exported function?

mindlessgreen
  • 11,059
  • 16
  • 68
  • 113
  • The code you have posted should work as is. Could you share some more detail on how to replicate the problems you're seeing? – krlmlr Jan 19 '16 at 20:17
  • I am having a similar problem. Do your error messages indicate that `fnbig` is not exported, or do they complain about `fnsmall` not being exported? – eric_kernfeld Aug 09 '17 at 22:14

1 Answers1

15

But I read that using :::is not recommended.

I think you mention this based on the following statement in the manual for writing packages by R.

Using foo:::f instead of foo::f allows access to unexported objects. This is generally not recommended, as the semantics of unexported objects may be changed by the package author in routine maintenance.

The reason for this to be not recommended is that unexported functions have no documentation and as such have no guarantee from the side of the package author that they will keep doing what they do now.

However, since you are referring to your own unexported functions, you have full control of what is happening in those functions, so this objection is not as relevant.

Referring to it as foo:::fnsmall is therefore a good option.

JAD
  • 2,035
  • 4
  • 21
  • 35
  • 1
    Thanks for elaborating on this. I have exactly the same problem, however R CMD now gives a warning for referencing your own internal functions with `:::` is there any way around this? – Björn Mar 18 '22 at 20:57
  • 2
    If the internal function (`foo:::fnsmall`) is referenced in your own package (`foo`), then you don't need to reference it internally. Instead, you can call the function as if it's any other function in your package: `fnsmall` – Alexander Christensen Sep 17 '22 at 21:21