Skip to content

If a commented line inside R code contains 2 straight single quotes, document() throws erroneous warning #428

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

Closed
bestdan opened this issue Nov 3, 2015 · 13 comments

Comments

@bestdan
Copy link

bestdan commented Nov 3, 2015

#' @title Test Bug
#' @author Daniel Egan
#' @param x  
#' @return Nothing
#' @export
#' @examples
#' testFun(x)


testFun <- function(x){

  #' Warning1'
  return(TRUE)
}

Produces

> Warning messages:
> 1: @examples [TestFun.R#8]: mismatched braces or quotes 
@bestdan
Copy link
Author

bestdan commented Nov 3, 2015

I should note, the toughest part about this was that the warning is ascribed to the roxygen block ABOVE the actual R code which is causing it. There are no mis-matched braces or quotes in the examples block, which is SUPER confusing.

If the warning correctly identified the line where the error was (in the R code, not the roxygen block), this would have been much easier to figure out.

@hadley
Copy link
Member

hadley commented Nov 4, 2015

As I mentioned in #426, this is because roxygen now processes roxygen comments inside functions. I think it's hard to get a better error message here, because from roxygen's perspective, that line is part of the examples which start at line 8.

cc @krlmlr

@hadley hadley closed this as completed Nov 4, 2015
@krlmlr
Copy link
Member

krlmlr commented Nov 4, 2015

(I'm responsible for this.) It shouldn't be too hard to add a compatibility switch, if this causes too much trouble. However, changing the comment from #' to a plain # should resolve this issue, and I think that plain comments are not checked.

@bestdan
Copy link
Author

bestdan commented Nov 4, 2015

Ok, but note that if the sentence includes ANY subsequent ', such as in a contraction, it will blow up.

#' This ain't gonna work

@hadley
Copy link
Member

hadley commented Nov 4, 2015

Yes, because that's not valid R code

@bestdan
Copy link
Author

bestdan commented Nov 4, 2015

Isn't it technically not valid roxygen2 code?

R appears to have no problem with it.

@hadley
Copy link
Member

hadley commented Nov 4, 2015

You're generating

#' @examples
#' testFun(x)
#' Warning1'

Which makes:

testFun(x)
warning1'

which is not valid

@bestdan
Copy link
Author

bestdan commented Nov 4, 2015

Completely agree. But the fact that Warning1 shows up is due to roxygen2 parsing of the files and including it.

I see what you're saying, and perhaps it's the right thing in the long run. I personally don't see a use case where I write roxygen documentation inside the function (could you share an example use case?).

It ate up most of my day yesterday, fixing files with #' in the function body. Since a big point of this is to make it easier for people to document files, more helpful error messages that correctly identify and communicate the error would help people who get hit with this a lot.

@hadley
Copy link
Member

hadley commented Nov 4, 2015

As I said in the other thread, it's a step along the road to support for R6 and RC.

We didn't expect people to use roxygen comments instead of normal comments inside functions and unfortunately it's hard to get a more informative error message because of the way parsing works.

@krlmlr
Copy link
Member

krlmlr commented Nov 4, 2015

Here's a simple sed script that should convert most inline roxygen2 comments (those with indentation using space) to plain comments:

sed -r -i"" "s/( +#)'/\1/" R/*

Run it from your package's directory. I've tested it on Ubuntu, I hope I got the -i switch right so that it also works on OS X.

@bestdan
Copy link
Author

bestdan commented Nov 4, 2015

@krlmlr Thank you, that's very helpful. I appreciate the effort.

I've actually fixed everything already, but I'm sure others will benefit from that. Perhaps a stack-overflow question that you ask and answer?

@krlmlr
Copy link
Member

krlmlr commented Nov 4, 2015

You ask, I answer ;-)

@bestdan
Copy link
Author

bestdan commented Nov 4, 2015

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

3 participants