Code Monkey home page Code Monkey logo

Comments (6)

jennybc avatar jennybc commented on June 18, 2024

Example. This call:

reprex({
 x <- 1:4
 y <- 2:5
 x + y
})

gives rise to this:

 x <- 1:4
 y <- 2:5
x + y
#> [1] 3 5 7 9

Code that started out evenly indented is no longer so. Maybe I could be more respectful of whitespace when turning expression input into strings? But it would be much easier if there was a way to just re-indent.

from reprex.

jennybc avatar jennybc commented on June 18, 2024

Perhaps a job for formatR::tidy_source().

http://yihui.name/formatR/

from reprex.

jennybc avatar jennybc commented on June 18, 2024

Fixed in 96f065a.

from reprex.

jimhester avatar jimhester commented on June 18, 2024

I think just using format() on the quoted expression would work fine actually, no need for the extra dependency. FWIW it would be nice if reprex() also accepted a pre-formatted string directly, like you get from clipr::read_clip().

ex <- quote({
 x <- 1:4
 y <- 2:5
 x + y
})

cat(as.character(ex), sep = "\n")
#> {
#> x <- 1:4
#> y <- 2:5
#> x + y
cat(format(ex), sep = "\n")
#> {
#>     x <- 1:4
#>     y <- 2:5
#>     x + y
#> }

from reprex.

jennybc avatar jennybc commented on June 18, 2024

reopening so I remember to try what you say @jimhester

from reprex.

jennybc avatar jennybc commented on June 18, 2024

OK yes format() is not perfect, but it's better than nothing. formatR is a suggested package (which is already true via the knitr dependency anyway). If formatR is not available, then format() is applied to the expression, once it has been captured as a character vector.

Here's how it does with a well-formatted expression. reprex() call (example from above):

reprex({
  x <- 1:4
  y <- 2:5
  x + y
})

Output:

x <- 1:4
y <- 2:5
x + y   
#> [1] 3 5 7 9

And an expression with more pathological formatting. reprex() call:

reprex({
  x <- 1:10
  for (i in x) {
            print(i)
}
  x
})

Output:

x <- 1:10     
for (i in x) {
print(i)      
}             
#> [1] 1
#> [1] 2
#> [1] 3
#> [1] 4
#> [1] 5
#> [1] 6
#> [1] 7
#> [1] 8
#> [1] 9
#> [1] 10
x             
#>  [1]  1  2  3  4  5  6  7  8  9 10

from reprex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.