Explicit namespaces for non-exported functions within same R package - best practice

Viewed 191

I have an R package (MyPackage) that has some exported (using @export) and some non-exported functions. If I call a non-exported function from elsewhere in the package, what is the most appropriate way to reference it? For example, given the following code:

#' @export
f1 <- function(){
  f2()
  }

f2 <- function(){
  print('hello')
  }

When I run linting on the package I get the warning:

no visible global function definition for 'f2'

I could use MyPackage:f2 but my understanding was that this isn't necessary. I do not expect to get the error 'no visible global function definition' for a function within the same package. What is the best practice in this case?

1 Answers

As myself and others have mentioned, your code does not produce such a warning.

In terms of best practices, don't use MyPackage:::f2. As mentioned here.

A function is not exported to user is not present in the NAMESPACE. If you use roxygen, putting @export tag ONLY for the function you want to export will do the job.

As you did, just using the @export tag for the functions you want to make available, and not for the internal functions, is the way to go. You should just decorate your internal functions with a few roxygen comments, then decide whether you want to create a manual page for this function or not.

If you don't wish to create a manual page for f2, you should use the #' @noRd tag. (source)

#' Internal function printing "hello"
#' @description A function that prints the text "hello".
#' @noRd
f2 <- function(){
  print('hello')
  }

If you wanted to create a manual page for f2, but to exclude it from the index of the manual, you could use @keywords internal, which works even with f1 or basically any function that you wouldn't want too visible in your manual.

#' Internal function printing "hello"
#' @description A function that prints the text "hello".
#' @keywords internal
f2 <- function(){
  print('hello')
  }
Related