Skip to content
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

R 4.1.0 anonymous functions are not treated correctly #98

Closed
3 tasks done
notPlancha opened this issue Dec 20, 2022 · 2 comments
Closed
3 tasks done

R 4.1.0 anonymous functions are not treated correctly #98

notPlancha opened this issue Dec 20, 2022 · 2 comments

Comments

@notPlancha
Copy link

notPlancha commented Dec 20, 2022

I'm writing a Sweave document and when knitting my document, my anonymous functions are turned into bigger anonymous functions; for example:

<<>>=
(\(x) x^2)(4)
@

Turns into

(function(x){x^2})(4)

In the output. My issue is that I'm doing a bunch of these, and it's occupying a lot of space in my output, which I didn't wish it did.
Explicitly,

This

Turns into

This

I know this is because of tidy=TRUE and formatR, but I did not find any parameter that changes this behaviors. Adding the option tidy=FALSE resolves the issue, but I think this is not intended, so I'm writing an issue about it.
Interestingly, not putting the brackets produces a code that should not run, since the function requires brackets to run afaik.


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • 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.
  • 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.

@yihui
Copy link
Owner

yihui commented Dec 20, 2022

The core technical issue is that R's parser turns \() into function() internally:

> deparse((parse(text='(\\(x) x^2)(4)'))[[1]])
[1] "(function(x) x^2)(4)"

but I think I can come up with a hack to preserve \() in formatR. Thanks for the report!

@yihui yihui closed this as completed in 14c369d Dec 22, 2022
@yihui
Copy link
Owner

yihui commented Dec 22, 2022

Should be fixed now. You can install the development version via

remotes::install_github('yihui/formatR')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants