Frage

tidy.source(source = "ugly.R", 
            file = "pretty.R", 
            reindent.spaces = 2, 
            width.cutoff = 72)

The above did not wrap the code at column 72, my output was as follow:

pwr.norm.test(d = (19.4 - 18.4)/2.8, n = 100, sig.level = 0.05, alternative = "two.sided")
pwr.norm.test(d = (18.9 - 18.4)/2.8, n = 200, sig.level = 0.05, alternative = "two.sided")
pwr.norm.test(d = (18.4 - 18.4)/2.8, n = 10000, sig.level = 0.05, alternative = "two.sided")

Did I do it wrongly? Also, there was no space before and after the "/" sign after formatting. Was that supposed to be correct? Thanks.

War es hilfreich?

Lösung

width.cutoff specifies the minimum length at which line-breaking is tried, not the maximum line length. So in this case it won't start trying to line-break until it's already in the middle of the word alternative. tidy.source also doesn't split assignments, so it ends up not finding any place to break until the end of the line.

Also, it does appear that tidy.source doesn't insert spaces before and after slashes - this behaviour appears to be inherited from deparse, which it uses.

Andere Tipps

The problem is with a base R function: deparse.

I forked formatR and did a hack on how this functions is used. You can install by:

library(devtools)
install_github("pablo14/formatR")

It works in most of the cases I tested (a whole book). But please do your own checks, and consider to go back to the original repository after you get the fitted lines.

What I did:

The internal function tidy_block (which calls deparse) N times until width criteria is satisfied. Otherwise, it returns the original value. It is not possible to solve all the cases, but works better than original version.

Related git pull request: https://github.com/yihui/formatR/pull/71

What to do when it doesn't work?

If the lines continue to remain outside the margins, you need to set tidy=FALSE and then add the enter (new line) manually (counting the number of characters per line).

Hope this helps!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top