简体   繁体   中英

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.

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.

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. (Using roxygen2 or similar is a "better" alternative for documenting functions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM