formatR icon indicating copy to clipboard operation
formatR copied to clipboard

Empty lines in function calls results in an error

Open Iain-S opened this issue 2 years ago • 3 comments

R version 4.1.2 (2021-11-01) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Big Sur 10.16 Locale: en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8 Package version: formatR_1.11.1

Minimal Reproducible Example

At least some of the time (perhaps all of the time) having two empty lines between function call parens causes formatR to error. However, it appears to be valid R syntax (verified by running it in R).

mre.R

print(


)
> formatR::tidy_file("mre.R", indent=2, arrow=TRUE, args.newline=TRUE)
tidying mre.R
Error in base::parse(text = code, keep.source = FALSE) :
  <text>:3:1: unexpected symbol
2: invisible(".BeGiN_TiDy_IdEnTiFiEr_HaHaHa.HaHaHa_EnD_TiDy_IdEnTiFiEr")
3: invisible

Conversely, having only one empty line in the function call does not throw an error but does result in incorrect behaviour

mre2.R (before)

print(

)
> formatR::tidy_file("mre2.R")
tidying mre2.R

mre2.R (after)

print() print()

By filing an issue to this repo, I promise that

  • [x] I have fully read the issue guide at https://yihui.org/issue/.
  • [x] I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('formatR'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/formatR').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

Iain-S avatar Mar 22 '22 21:03 Iain-S

I'm afraid that this is a known limitation: https://yihui.org/formatr/#6-further-notes Not all blank lines can be correctly formatted. Sorry.

yihui avatar Mar 23 '22 00:03 yihui

OK, thanks. I can see that the files are correctly formatted if I set blank=FALSE and comment=FALSE.

Do you think that the whitespace formatting could be improved or is it a limitation of R, as a language?

Iain-S avatar Mar 23 '22 15:03 Iain-S

It's not a limitation of R, but me. I feel it would be hard to support multiple empty lines as in your first example. For the second example, it looks like a bug. I don't know why two instances of print() were generated. @iqis Do you have time to investigate?

yihui avatar Mar 23 '22 15:03 yihui