0

How do I change an .Rmd file to an .md file, that I can then push to GitHub? I have knit to HTML

Is it possible to do this by something as simple as changing the file extension to ".md" and doing a save as? Any insight into what the differences are amongst these types of files would be so helpful!

Also, how different should an .Rmd and .md file look when opened?

Thank you!

PugFanatic
  • 31
  • 2
  • 9
  • If you don't have any code chunks you want processed then you can just *save as*. However, if you have plots or workings in your document, you will need to grab the intermediate `.md` and any associated assets (such as images... etc.) – Kevin Arseneau Sep 18 '17 at 02:22
  • I see, I do have code chunks and plots, as well as one image. Where do I find the intermediate `.md' files? – PugFanatic Sep 18 '17 at 02:23
  • 1
    how about `knitr::knit("myfile.Rmd")` ? – Ben Bolker Sep 18 '17 at 02:52
  • @BenBolker, yes, I agree. For individual runs that would be a good solution. As the OP mentioned pushing to a repo I had thought keeping each run for versioning would be beneficial. – Kevin Arseneau Sep 18 '17 at 03:29
  • @Ben Bolker I will try adding that! Will that be globally applied to the entire script? – PugFanatic Sep 18 '17 at 13:48
  • yes, it runs the whole Rmd file. – Ben Bolker Sep 18 '17 at 14:04

2 Answers2

1

You can add keep_md to your YAML header.

---
output: 
  html_document:
    keep_md: true
---

Just be aware you will need to also push any related assets to Github as well as the .md file

Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
  • Hmm, When I tried that, I got an error that looks like this: Error in yaml::yaml.load(enc2utf8(string), ...) : Scanner error: mapping values are not allowed in this context at line 6, column 12 Calls: ... yaml_load_utf8 -> mark_utf8 -> -> .Call Execution halted – PugFanatic Sep 18 '17 at 02:43
  • @PugFanatic, I can't reproduce your error, to improve the quality of your question you need to include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Kevin Arseneau Sep 18 '17 at 02:50
  • The code is a bit lengthy but I wonder if this will help. When I first created the .Rmd file, I specifically selected an R Markdown to HTML file type, as opposed to beginning with a plain text file. When I try to enter your suggestion `keep_md: true` into the YAML header of the R Markdown file, I get the error I copied and pasted above. None of the code runs and it seems to be completely halted, and my only ability at that point is to "knit to keep_md" (as opposed to knit to HTML , etc. that was previously there), which in turn produces the error again. Does this help at all? – PugFanatic Sep 18 '17 at 03:02
  • @PugFanatic, I can't see what you are doing, please produce a minimal reproducible example as requested in the previous comment link. – Kevin Arseneau Sep 18 '17 at 03:05
  • 2
    Errors are probably due to a badly-formatted YAML header. It must be exactly as in the answer, _i.e._: (1) between the dashed lines; (2) the word `output` not indented; (2) the line `html_document` indented 2 spaces; (3) the line `keep_md` indented 4 spaces. Make sure `title`, `author`, `date` sections are similarly formatted: first line of each not indented. – neilfws Sep 18 '17 at 03:38
  • @neilfws When you brought up the YAML header formatting I suspected this could've been the issue, but after reviewing, my header is formatted according to the specifications that you mentioned – PugFanatic Sep 18 '17 at 14:16
  • Google the error: it is always related to incorrect YAML format. You need to show us what you have, as @kevin.arseneau requested, instead of just describing it. – neilfws Sep 18 '17 at 23:00
1

You can use

knitr::knit("my_file.Rmd")

which should output my_file.md (a Markdown file with included code chunks) and generate figures (in a figs/ directory, I think, not sure).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453