Skip to content

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

Closed

Description

@y9c

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\"))))"

Activity

  1. lorenzwalthert commented on Apr 24, 2019

    @lorenzwalthert
    Collaborator

    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.

  2. y9c commented on Apr 24, 2019

    @y9c
    Author

    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')" 
  3. y9c commented on Apr 24, 2019

    @y9c
    Author

    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.

  4. lorenzwalthert commented on Apr 24, 2019

    @lorenzwalthert
    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?

  5. y9c commented on Apr 24, 2019

    @y9c
    Author

    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

  6. lorenzwalthert commented on Apr 24, 2019

    @lorenzwalthert
    Collaborator

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

  7. y9c commented on May 2, 2019

    @y9c
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions