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

Convert chunk option to YAML #2151

Merged
merged 10 commits into from
Jul 26, 2022
Merged

Convert chunk option to YAML #2151

merged 10 commits into from
Jul 26, 2022

Conversation

cderv
Copy link
Collaborator

@cderv cderv commented Jul 25, 2022

This close #2082 and follows #2149

Chunk options are parsed and then written to YAML using yaml::as.yaml() with two tweaks to the parsed chunk options

  • Logical will be true or false instead of default yes or no
  • R expression or variables passed in chunk option value will be written to YAML after !expr to make the fact they should be evaluted before parsing. Example:
```{r, R.options = list(width = 80)}
names(knitr::knit_engines$get())
```

will be converted to

```{r}
#| R.options: !expr list(width = 80)
names(knitr::knit_engines$get())
```

Doing

R.options:
    width: 80

is a bit more complicated than the current solution, as it implies going through the all tree of expression to only find the leaf is list that are variable or expression to evaluated with !expr

This solution is simple enough to produce valid document, and it is easy enough for the user to reformat manually some chunk header.

Here is how a conversion looks like on all knitr-examples: yihui/knitr-examples@master...cderv:knitr-examples:yaml-chunk-header
Does it seems right to you ?

It seems there is still an indent question where the chunk header is not indented, whereas the code inside is - do we want to indent the in-chunk option part too.

Anyway, @yihui I let you review. I wanted to have something ready for the conference, so here it is.

@cderv cderv requested a review from yihui July 25, 2022 11:27
R/parser.R Show resolved Hide resolved
@cderv
Copy link
Collaborator Author

cderv commented Jul 25, 2022

Tests are failing because r-forge seems down... and some examples uses it.

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks perfect. Thanks!

R/parser.R Show resolved Hide resolved
R/parser.R Outdated
params3, handlers = list(
# true / false instead of no
logical = function(x) {
result <- ifelse(x, "true", "false")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think simply tolower(x) would work. Correct me if I'm wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes you're right it should work woth the automatic logical to character conversion.

@yihui yihui merged commit 45289c2 into master Jul 26, 2022
@yihui yihui deleted the convert-yaml branch July 26, 2022 04:21
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: function to convert chunk options to YAML-style
2 participants