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

Styler does not respect knitr chunk options in R scripts #361

Closed
samhinshaw opened this Issue Feb 27, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@samhinshaw
Copy link

commented Feb 27, 2018

Styler formats chunk options in R scripts formatted for spinning to markdown from:

#+

to

# +

For an example, run styler on the knitr spin example. As a result, chunk names are turned into regular code comments.

@lorenzwalthert

This comment has been minimized.

Copy link
Collaborator

commented Feb 28, 2018

Hi @samhinshaw, thanks for pointing out. I heard of spinning but I did not know the details. So do you suggest to always keep #+, just as we keep #'? I wonder whether there are use cases where someone wanted a space but forgot it. Because we would no longer be able to detect that if a plus follows. Maybe we can match the whole line with regex? And is it always preceded with at least two line breaks?

Also, probably need to adapt documentation since we include quite a few special cases now (see #305, #345 etc.)

@samhinshaw

This comment has been minimized.

Copy link
Author

commented Feb 28, 2018

I think it would make sense to preserve #+. I cannot think of too many cases in which this would prevent formatting.

Because of spin's flexibility, the chunk option does not need to be preceded or proceeded by anything in particular—it could be code, line breaks, roxygen comments, or 'regular' comments.

#' Now let's do X in this next example:
#+ example2
ggplot(...)

The nice thing about spin is that you do not need any special formatting within an .R script to use it! Unfortunately, this also limits detection options. However, you can add a YAML header a la R Markdown if you wish:

#' ---
#' title: 'Example Document'
#' author: 'Sam Hinshaw'
#' output: github_document
#' ---

So I suppose another way to do this would be opt-in to by only preserving #+ when a roxygen-commented YAML chunk exists.

@lorenzwalthert

This comment has been minimized.

Copy link
Collaborator

commented Feb 28, 2018

Ok. I think we need simple rules. So I think let's preserve #+ everywhere.

@lorenzwalthert

This comment has been minimized.

Copy link
Collaborator

commented Feb 28, 2018

You can use this patch until this is merged into master / a new release appears on CRAN:

remotes::install_github("r-lib/styler#362")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.