-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alternative width and indentation behavior for usage() #65
Comments
You may submit two pull requests addressing these two issues separately. I think I can accept egnha@9661846 but I'm not sure about egnha@0e34eab. I came up with the similar idea a couple of years ago: https://github.com/yihui/Rd2roxygen/blob/b9fcedb24/R/build.R#L152-L162 but obviously it is not efficient.That said, I don't have other ways to implement the idea of the upper bound of the width. I wrote a blog post in Chinese two months ago about this issue: https://yihui.name/cn/2017/02/formatr/ Not sure if you are able to read it, but the possible ways are:
The first way is probably the most difficult one. The latter two may be a little easier, but still not trivial. |
I should have figured that you'd already tried the while-loop strategy. ;) I agree, it's too pokey. In the meantime, I've come up with a different way to get We even get a small performance boost over the original microbenchmark::microbenchmark(
usage(barplot.default, 60L, indent.by.FUN = TRUE, output = FALSE),
usage2(barplot.default, 60L, indent.by.FUN = TRUE, output = FALSE),
times = 1e3L
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> usage(barplot.default, 60L, indent.by.FUN = TRUE, output = FALSE) 3.846394 4.006600 4.397606 4.087900 4.346930 46.761532 1000
#> usage2(barplot.default, 60L, indent.by.FUN = TRUE, output = FALSE) 1.580056 1.642587 1.803428 1.676515 1.768616 4.916034 1000 (In the case where the function is supplied as a string, further speed-up could be had by replacing Typical output of usage2(barplot.default, 60L, indent.by.FUN = TRUE)
#> ## Default S3 method:
#> barplot(height, width = 1, space = NULL, names.arg = NULL,
#> legend.text = NULL, beside = FALSE, horiz = FALSE,
#> density = NULL, angle = 45, col = NULL,
#> border = par("fg"), main = NULL, sub = NULL,
#> xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL,
#> xpd = TRUE, log = "", axes = TRUE, axisnames = TRUE,
#> cex.axis = par("cex.axis"),
#> cex.names = par("cex.axis"), inside = TRUE,
#> plot = TRUE, axis.lty = 0, offset = 0, add = FALSE,
#> args.legend = NULL, ...) |
I love it! Please submit a PR replacing my |
I'd be glad to submit a PR. :) Aside from writing some tests and ensuring that corner cases are properly handled, there is the question of what Example of what currently happens: # usage2() calls tidy_usage() to fit the lines (formerly done by tidy_source())
usg = "foo (bar, really_long_argument_name = long_argument_default_value)"
tidy_usg = tidy_usage("foo", usg, width = 50, indent = 3)
cat(tidy_usg)
#> foo(bar,
#> really_long_argument_name = long_argument_default_value)
nchar(capture.output(cat(tidy_usg)))
#> [1] 8 60 With
I think the first option is the more practical one, because Which of these options (or an alternative) do you think is best? |
I'd add an argument, say, |
That is a good compromise. I'll do that. |
Implementation of usage() that respects width and indent (#65)
Consider the following example of
usage()
from the help page (formatR 1.4.2):It produces
There are two issues here:
barplot(
, cf. how to split the parameters into different lines and keep them aligned? #57, Indenting consistent with Rstudio #53.width
parameter does not set an upper bound on the length of output lines, as the help page implies, where it is stated thatwidth
is the "the width of output." In the example, the first line is more than 60 characters (78).@yihui I would like to propose two minor modifications to
usage()
(and onlyusage()
) to resolve these points, assuming you consider them to be actual "issues":To handle the indentation issue, one can add a logical toggle to
usage()
, sayindent.by.FUN
(with default valueFALSE
, to maintain the current behavior), to turn on indentation by the width of the function name, see 9661846.To handle the width issue, one has to work around the fact that the
width.cutoff
parameter ofdeparse()
is actually a lower bound for line lengths. So, to ensure that line lengths don't exceedwidth
, one can first trywidth.cutoff = width
, and if that leads to lines that are too long, then decrementwidth
and repeat until all lines are no more thanwidth
in length. See 0e34eab for an implementation of this rudimentary strategy.The outcome of these two modifications is that
usage()
now conforms to the naive interpretation ofwidth
:width
in length (while remaining relatively "full"):(To be precise, this new interpretation of
width
doesn't guarantee that line lengths won't exceed it, always, only that this will be so if it's possible withdeparse(expr, width.cutoff)
, for some (valid) value ofwidth.cutoff
less than or equal towidth
.)The text was updated successfully, but these errors were encountered: