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

Support stdin and stdout as input file and output file in command line? #494

Closed
y9c opened this issue Apr 20, 2019 · 7 comments
Closed

Support stdin and stdout as input file and output file in command line? #494

y9c opened this issue Apr 20, 2019 · 7 comments

Comments

@y9c
Copy link

y9c commented Apr 20, 2019

will it be possible?

my solution: (require readr package)

cat test.R| R --slave --no-restore --no-save -e "styler::style_text(readr::read_file((file(\"stdin\"))))"
@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented Apr 24, 2019

Thanks for clarifying. I think an alternative would be the following:

  • Put an executable R script with shebang in a place that is part of your PATH or in a new directory and add that directory to your path. I am on Mac, so I put export PATH = $PATH:~/custom_bin in .bash_profile and then put the following script callerd styler.r in the custom_bin directory:
#!/usr/local/bin/Rscript
con <- file("stdin")
out <- styler::style_text(readLines(con))
close(con)
out
  • Make the file executable with chmod +x ~/custom_bin/styler.r
  • Now you can conveniently call it similar to a littler script.
echo "1+1" | styler.r
1 + 1

or

cat r-script.R | styler.r

if your code is in a script.

@y9c
Copy link
Author

y9c commented Apr 24, 2019

How about adding stdin argument into styler.

It is possible to do that in formatR.

cat test.R | R --slave --no-restore --no-save -e "formatR::tidy_source('stdin')" 

@y9c
Copy link
Author

y9c commented Apr 24, 2019

For vim plugin support, the formatter tool should be a single excutable or a one liner code.
Packing styler into script not a good solution, cause you need to generate the script manually when you start a new environment.

@lorenzwalthert
Copy link
Collaborator

For vim plugin support, the formatter tool should be a single excutable or a one liner code.
Packing styler into script not a good solution, cause you need to generate the script manually when you start a new environment.

Ok. So we have support for styler in ale, just added a few days ago, which is a VIM Plug-in: dense-analysis/ale#2401 (comment). Maybe this is what you are looking for?

@y9c
Copy link
Author

y9c commented Apr 24, 2019

I also pushed a commit for styler support in neoformat (another popular vim plugin) a few days ago, but it is not perfect. I need readr for styler to make it work.

https://github.com/sbdchd/neoformat/blob/924dee18e7ca547e105ab8d800069fc5829b019b/autoload/neoformat/formatters/r.vim#L8

@lorenzwalthert
Copy link
Collaborator

But why not using the approach I used above with readLines(file(„stdin“)) instead of the approach with readr?

@y9c
Copy link
Author

y9c commented May 2, 2019

I forget to close file, and got some error in the first attempt.

It works fine now. Thank you @lorenzwalthert


BTW, the performance of styler is a little slow. Even if the file is well formatted.

styler::style_text (for 20 lines of R code)

cat test.R  0.00s user 0.00s system 79% cpu 0.001 total
R --slave --no-restore --no-save -e   0.40s user 0.02s system 100% cpu 0.411 total

black formatter of python (for a large file)

> time black -l 79 test.py
reformatted test.py
All done! ✨ 🍰 ✨
1 file reformatted.
black -l 79 test.py  0.14s user 0.02s system 38% cpu 0.406 total

> time black -l 79 test.py
All done! ✨ 🍰 ✨
1 file left unchanged.
black -l 79 test.py  0.07s user 0.01s system 99% cpu 0.082 total

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

No branches or pull requests

2 participants