Skip to content

style R Markdown (code chunks) #13

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

Closed
lorenzwalthert opened this issue May 14, 2017 · 9 comments
Closed

style R Markdown (code chunks) #13

lorenzwalthert opened this issue May 14, 2017 · 9 comments

Comments

@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented May 14, 2017

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...

@krlmlr
Copy link
Member

krlmlr commented May 14, 2017

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.

@lorenzwalthert
Copy link
Collaborator Author

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.

@krlmlr
Copy link
Member

krlmlr commented May 14, 2017

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).

@jimhester
Copy link
Member

@lorenzwalthert
Copy link
Collaborator Author

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.

@jimhester
Copy link
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.

@lorenzwalthert
Copy link
Collaborator Author

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.

@tvatter
Copy link

tvatter commented Jun 6, 2019

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)

@tvatter
Copy link

tvatter commented Jun 6, 2019

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

lorenzwalthert added a commit that referenced this issue Sep 22, 2019
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

4 participants