yihui / formatR

Format R Code Automatically
https://yihui.org/formatr/
255 stars 52 forks source link

Line breaking behavior needs improvement #37

Closed renkun-ken closed 10 years ago

renkun-ken commented 10 years ago

The formatR package work well in most aspects and I love this package for it makes R programmers life easier.

I find the line breaking behavior looks not working pretty well especially when lines are long, like:

if (profile$settings$backtest$dynamic$quantiles$enable) {
  qts <- quantile(tick$res[max(1, i - profile$settings$backtest$dynamic$quantiles$window + 1):i], as.numeric(profile$strategy$quantiles), na.rm = TRUE, names = FALSE)
  names(qts) <- names(profile$strategy$quantiles)
  for (qts.name in names(qts)) {
    tick[i + 1, qts.name] <- learning.rate * qts[qts.name] + (1 - learning.rate) * tick[i, qts.name]
  }
}

The code is formatted as

if (profile$settings$backtest$dynamic$quantiles$enable) {
    qts <- quantile(tick$res[max(1, i - profile$settings$backtest$dynamic$quantiles$window + 
        1):i], as.numeric(profile$strategy$quantiles), na.rm = TRUE, names = FALSE)
    names(qts) <- names(profile$strategy$quantiles)
    for (qts.name in names(qts)) {
        tick[i + 1, qts.name] <- learning.rate * qts[qts.name] + (1 - learning.rate) 
* 
            tick[i, qts.name]
    }
} 

where in 80-column text-width, the codes does not look well because several lines that involve long variable names are not successfully broken down so that several arithmetic operators occupies new lines in 80-column view, which is quite unappealing.

Is there any possibility to improve the line breaking behavior?

yihui commented 10 years ago

The option width sets the minimal width which R uses to find a break point. See width.cutoff in ?deparse.

This is not an easy problem, and I'm afraid I cannot do anything about it, especially when you have a super long token like profile$settings$backtest$dynamic$quantiles$window. Sorry.