Skip to content

style R Markdown (code chunks) #13

Closed

Description

@lorenzwalthert

It would be nice if styler could not only format plain .R files, but also .Rmd files.

Code Chunks

This could probably by implemented by changing transform_files, i.e. adding a function transform_lines_rmd_chunk. Main steps are:

  • first read read_lines_enc
  • look for regex ^´´´\\{r.*\\} *$ and ^``` *$
  • transforming lines between these expressions while leaving everything else unchanged
  • write back with write_lines_enc

@krlmlr maybe this functionality fits even in the utf8 package?

Plain Text

  • Line breaks if text is longer than 80 characters
  • punctuation (i.e. space after . , ? ! ect.)

The second point might be out of the scope of the package though...

Activity

  1. krlmlr commented on May 14, 2017

    @krlmlr
    Member

    Could we use knitr to parse the .Rmd file? The transform_lines_enc() function in utf8 seems generic enough.

    Not sure about styling plain text, realistically we'd need to parse and write back Markdown.

  2. lorenzwalthert commented on May 14, 2017

    @lorenzwalthert
    CollaboratorAuthor

    As far as I know, there is only the knitr code chunk option tidy that tidies up the output based on formatR, but I don't know how knitr could tidy the source.

  3. krlmlr commented on May 14, 2017

    @krlmlr
    Member

    knitr is not supposed to test it, just to do the extraction work for us (determine what is code, what language is that code in, what is text).

  4. lorenzwalthert commented on May 18, 2017

    @lorenzwalthert
    CollaboratorAuthor

    Thanks @jimhester for that hint. I just tried out the function and it indeed seems to me that we could use it to achieve our goal.
    I know its a minor thing but I just thought about inline R code in Rmd documents. It would be nice if that could also be covered, but I think this is not the case with extract_r_code, right?
    Also, what was the reason against using (internal) knitr functionality in the lintr package to do the extraction, as @krlmlr suggests?

    @krlmlr I found the exported function knitr:pulr(text = character_vec) that returns all code lines (not only R) as a character vector, but I think this does not really help us. I just had a quick look at internal functions of knitr, and probably there are tools that could help us, but I did not really have time yet to explore that further.

  5. jimhester commented on May 18, 2017

    @jimhester
    Member

    Inline R code is typically a single statement long, so likely does not have anything worth linting / styling. Supporting it would complicate the implementation for limited gain.

    We do use internal knitr functionality https://github.com/jimhester/lintr/blob/aa70430282852afa15a61cc69771e71dd32e1aec/R/extract.R#L40-L41, to detect the code boundries. The reason you can't use purl et all directly is they transform the source code lines into a new script, so the line numbers do not match the original version. For the linting case we simply blank out the non-R lines and lint that file, you would have to do something more complicated for styler.

  6. lorenzwalthert commented on Oct 13, 2017

    @lorenzwalthert
    CollaboratorAuthor

    I'd say closed with #233 for now. Styling markdown text is out of scope I think. The only thing one might consider is formatting to 80 characters (for non-code). We could integrate stylermd into styler (i.e. copying relevant code over). If you @jonmcalder are interested, you can have a look, otherwise we leave it for now.

  7. tvatter commented on Jun 6, 2019

    @tvatter

    Question: how is this supposed to work?

    I tried to style the file at https://gist.github.com/tvatter/25dbb789b64e7ba9484c65b5d4c4f828
    using styler::style_file("sample.Rmd") and I got:

    Status    Count   Legend 
    ✔         1       File unchanged. 
    ℹ         0        File changed. 
    ✖         0       Styling threw an error. 
    

    (and obviously the ugly code chunk was left untouched)

  8. tvatter commented on Jun 6, 2019

    @tvatter

    Oh gosh, my bad.... the code is in text... Forget it :)

  9. added a commit that references this issue on Sep 22, 2019
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