antonio
antonio

Reputation: 11120

knitr inline comments for arguments

When knitting this code snippet:

<<test>>=
f <- function( 
    x, #arg description
    y) x*y
f(3, 4) 
@ 

I get the error:

label: test
Warning in block_exec(params) :
  failed to tidy R code in chunk <test>
reason: Error in base::parse(text = code, srcfile = NULL) : 
  2:5: unexpected SPECIAL
1: f <- function (
2: x , %InLiNe_IdEnTiFiEr%
       ^

The problem is linked to tidy.source(), which is unable to format the given block.

Commenting function arguments in place is a very common and often suggested practice. I think this behaviour might be not by design, since in the past it was possible to use these type of comments.

Upvotes: 1

Views: 264

Answers (2)

Dason
Dason

Reputation: 61933

Note that you get a warning - not an error. It still should produce a document it just won't be tidied. It doesn't know how to tidy that and I don't think that's a great way to format your code so I'm ok with not being able to tidy that code.

If you want to eliminate the warning you can do this

<<test, tidy=FALSE>>=
f <- function( 
    x, #arg description
    y) x*y
f(3, 4) 
@ 

You won't have your code tidied for you but you won't get the warning.

Upvotes: 3

mnel
mnel

Reputation: 115392

Comments cannot be retained within an incomplete expression.

See section 6 of https://github.com/yihui/formatR/wiki.

This has been noted as (closed) issue #15 by the package author. (Usingroxygen2 or similar is a "better" alternative for documenting functions).

Upvotes: 2

Related Questions