简体   繁体   中英

Prevent short comments from wrapping in R Markdown/knitr output

Is it possible to get a code chunk tidy ed up, while leaving the comments alone?

Say I wanna put ASCII art in some comments in a function, I could just set tidy = F but then the rest of the code chunk is "messy" as in the following output:

myfun=function(a,b){
  ##     ^_^
  ##    {o,o}
  ##    |)__)
  ##-----m-m-----
  c=sum(a,b)
  return(c) 
}

If I set tidy = T then the short comments all wrap together and I get the following as output:

myfun = function(a, b) {
    ## ^_^ {o,o} |)__) -----m-m-----
    c = sum(a, b)
    return(c)
}

What I would like to see is the following as output:

myfun = function(a, b) {
    ##      ^_^
    ##     {o,o} 
    ##     |)__)
    ## -----m-m-----
    c = sum(a, b)
    return(c)
}

As suggested in the comments, I'll answer my own question.

If one looks at Yihui's documentation for formatR , one might notice that roxygen comments (which look like this: #' ) will not be wrapped in any case.

So using the code chunk

```{r, tidy = T}
myfun=function(a,b){
  #'     ^_^
  #'    {o,o}
  #'    |)__)
  #'-----m-m-----
  c=sum(a,b)
  return(c)
}
```

will give me the desired output:

myfun = function(a, b) {
    #'     ^_^
    #'    {o,o}
    #'    |)__)
    #'-----m-m-----
    c = sum(a, b)
    return(c)
}

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