Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error after devtools::document when #' comments inside function #737

Closed
statnmap opened this issue May 7, 2018 · 1 comment
Closed

Error after devtools::document when #' comments inside function #737

statnmap opened this issue May 7, 2018 · 1 comment

Comments

@statnmap
Copy link

statnmap commented May 7, 2018

I would say this is linked to a bad practice but as I took me some time to find the reason, I thought this could be good to be reported.
I received a R code that I wanted to transform as a package. I remarked that if there are some comments inside a function that are commented with #', there are used as the roxygen skeleton. Hence, when using devtools::document() the following function returns a warning and is not exported. This because @export is followed by multiple lines.

Warning: @export [mean.R#11]: may only span a single line

Example function:

#' Mean of a vector

#' @param x vector
#'
#' @return return mean of a vector without NA
#' @import magrittr
#' @importFrom stats na.omit
#' @examples 
#' my_mean(c(4,5))
#' @export

my_mean <- function(x){
  #' Remove NA
  x <- x %>% na.omit()
  #' Calculate mean
  sum(x)/length(x)
}

To see it differently, we can finish the roxygen skeleton with @examples. Usually, if this is empty, document returns a warning: Warning: @examples [mean.R#9]: requires a value. However, in the following case, comments written using #' are used as the content of examples with no errors or warnings.

#' Mean of a vector

#' @param x vector
#'
#' @return return mean of a vector without NA
#' @import magrittr
#' @importFrom stats na.omit
#' @export
#' @examples 

my_mean <- function(x){
  #' Remove NA
  x <- x %>% na.omit()
  #' Calculate mean
  sum(x)/length(x)
}

Look at the documentation produced.
my_mean

I think we are not supposed to include such kind of comments inside the function, but does roxygen not supposed to stop reading when calling function ? (Maybe it would not work with S3 methods or documented data ?)

Error reported here once is maybe linked:
STAT545-UBC/Discussion#241
and reported here
https://support.rstudio.com/hc/en-us/community/posts/205901678-Too-many-newlines-in-roxygen-comments

@hadley
Copy link
Member

hadley commented Jun 28, 2018

This was added in v5.0.0:

  • The contents of documented functions are now also parsed for roxygen comments.
    This allows, e.g., documenting a parameter's type close to where this type is
    checked, or documenting implementation details close to the source, and
    simplifies future extensions such as the documentation of R6 classes
    (Allow roxygen comments in object to be documented #397, @krlmlr).

@hadley hadley closed this as completed Jun 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants