2

RStudio will reorder the output list in the YAML depending on which option was selected last in the knit dropdown button.

---
title: "Untitled"
output:
  html_document: default
  word_document: default
---

The YAML header above gives the option to compile the RMarkdown to word or html. It will give you the main option at the top, in this case html_document.

When the dropdown is used to select an alternate output, e.g. word_document, the order of output will change like so:

---
title: "Untitled"
output:
  word_document: default
  html_document: default
---

I'm assuming (from the helpful remarks by Jonathan and Kevin on a previous question), that this is due to the way RStudio reads the RMarkdown and gives contextual buttons, however, I'm curious if this re-ordering can be 'turned off' somehow?

A motivation for this is version control. This changes the text document, which registers as a modification and therefore forces a commit, or revert.

DaveRGP
  • 1,430
  • 15
  • 34
  • 1
    It is possible to use the YAML header without changing, but then you can't use the knitr button, you have to use the `rmarkdown::render` command, see here: http://stackoverflow.com/questions/39662365/knit-one-markdown-file-to-two-output-files/39663872#39663872 – J_F Apr 05 '17 at 17:01

2 Answers2

4

That the order of the lines changes is certainly a problem for version control, but according to the YAML specification the keys in mapping are unordered and loading-dumping not guaranteed to give the same value

Some libraries dump mappings by sorted values of the keys, but that is not required, and doesn't make much sense for anything but mappings for which all of the keys are of the same type and can be ordered.

So the YAML you can load from your two examples is exactly the same, assuming the parser adheres to the standard. If the order of the key-value is significant, the correct way would be to make a list of single key-value mappings (dashes can, but don't have to be indented):

title: "Untitled"
output:
- html_document: default
- word_document: default

and optionally put in a tag !!omap to make sure this is an ordered map:

title: "Untitled"
output: !!omap
- html_document: default
- word_document: default

It is of course possible that RStudio does use the ordering of the lines in the mapping (I do so myself in my ruamel.yaml Python package to allow round-tripping without key re-ordering). But the more correct way would be to use !!omap, or in some other way indicate the selected key, or the order of keys.

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • Thanks for the info. It appears RStudio does not allow the dashes or `!!omap` to be included in the header. It will run the first option (here html) correctly and remove the `!!omap` line, but then on selecting word (which is now listed as `- word_document` it will then insert a third, totally new option, at the top of the list: `'- word_document': default` and error out `Error in -word_document : invalid argument to unary operator Calls: -> create_output_format -> eval -> eval Execution halted` – DaveRGP Apr 06 '17 at 08:41
  • 1
    @DaveRGP It looks like RStudio uses (abuses?) the omap option of the YAML package for R, that way loading the YAML file creates an ordered map instead of normal maps according to the [reference manual](https://cran.r-project.org/web/packages/yaml/yaml.pdf). Maybe contacting to the RStudio developers and proposing an alternative solution is possible (e.g. via some configuration file option). – Anthon Apr 06 '17 at 08:55
  • RStudio makes other changes in the YAML- it evaluates expressions with !expr and replaces them with the current value. They have acknowledged the bug [report here](https://support.rstudio.com/hc/en-us/community/posts/209422797-YAML-dynamic-header-getting-munged-by-rmarkdown-rstudio) and supposedly fixed it in their daily builds 3 months ago, but the behavior still appears in release 1.0.136 – DonJ May 16 '17 at 15:06
0

This is a very annoying issue.

I mostly use the nb.html, but sometimes I want to knit a PDF.

As soon as I knit a PDF:

  • I have to manually reorder the output, otherwise the nb.html may not be saved anymore.
  • I have to restore any comments I had in the output field, because they are all automatically removed.

My best solution so far has been to keep a clean copy of the YAML header in a chunk right below.

It's ugly, but I have tried more elaborate solution such as r expressions. They fail, as RStudio checks for a literal "html_notebook" field in the YAML header (cf In r studio, how does the IDE treat notebooks with the 'preview' button), so there is no clean workaround

Community
  • 1
  • 1
Cha
  • 21
  • 4