"How to do it in R" wraps the code only right before showing it (kit_code() does this
automatically), so functions that generate code need not worry about width:
Details
Comments: break at spaces or between Chinese characters; continuation lines also start with "# "; punctuation is not put at the start of a line.
Code: break after a comma inside parentheses, and align continuation lines just after that parenthesis (a line break inside parentheses is always valid R). If the parenthesis was opened on an earlier line (e.g.
mutate(on its own line with the arguments on the next), continuation lines use the same indent as this line.If a trailing comment makes the line too wide, it is moved above the line.
After wrapping, the code is parsed again: if the expression differs from the original (or the original did not parse), the code is left unchanged. Places that cannot be broken (e.g. a very long string) are also left as is and scroll sideways in the code box.