rmarkdown: render to .pdf using existing .md files created for .html - r

Is that possible at all? At the moment I have to render my .Rmd files twice, once for an html and once for a pdf report. Each of them take about 50 mins. So if I can use the html .md files created after rendering, that would save me 50 mins.

You can keep the markdown output of knitr with keep_md: yes
---
output:
html_document:
keep_md: yes
---
Then, using pandoc, you can produce your pdf from this file; in a terminal (not a R console):
pandoc mydoc.md -o mydoc.pdf
You'll have to install pandoc if you haven't already, to use it without knitr.
You might loose a few things, though, because knitr adapts its md intermediary file to the final output.

It is possible to render multiple outputs, but not with the "knit" Button in RStudio. Write your desired output in the YAML header and then use output_format = "all" as argument in
rmarkdown::render(<your-rmd-file.rmd>, output_format ="all")
So the YAML header looks like:
title: "multiple outputs"
output:
pdf_document: default
html_document: default
Or any option you want to set for the different output formats.

If your .md document is already created, you can simply use:
library("rmarkdown")
render("mydoc.md", output_format = "pdf_document")
If you always need to generate both html and pdf, then render both at the same time using J_F's solution.

Alternatively, you can tell the compiler to keep the intermediate .md file. Then, you can easily compile that into other formats.
Go to:
1. RStudio > Open your .Rmd file
2. Click on the Gear (settings) drop down and choose Output Options
3. Choose Advanced
4. Check Keep markdown source file
5. Click OK
6. Knit to HTML
7. Open .md file in editor
8. In Gear (settings) drop down, select PDF as Output Format, AND select (No Preview)
9. In the Preview dropdown, select Preview PDF and a .pdf file should be created
As an alternative to steps 2-5, edit your file so that the header includes:
---
title: "blah blah"
output:
html_document:
keep_md: yes
---

Related

Styling of Ms Word not working in R Markdown

Have tried styling MS Word document from the markdown but I don't seem to get it write. What could I not be doing right? Below is the code
---
title: "Test Document"
author: "Moses Otieno"
date: "05/04/2021"
output:
word_document:
reference_docx: referent-doc.docx
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
## Test
This is the test
Referent Document https://www.dropbox.com/scl/fi/dyww5eiga334j55t58vcp/referent-doc.docx?dl=0&rlkey=o1ejilu3dfnncar65irh5er7v
Resulting document https://www.dropbox.com/scl/fi/mieqkcvy22eeighdjaby3/Test.docx?dl=0&rlkey=8wyrci2cg0ijyxbfdw5ks1znh
In a nutshell, make a template using an already knitted document containing your content and different types of headers etc.
I had a similar problem that was fixed by knitting my document and saving the resulting word(.docx) as "template". I now had a good working file with lots of content examples.
Next, I went into this file and manually changed the styles using "styles" in the Home tab. Open up this feature in your new "template" document by using the little down arrow on the bottom right-hand corner. Click on the various paragraphs and headers in your document to see what they refer to (the style will jump about on the dropdown menu) and make your changes. Save again. Now when you want to knit a new document, it will hopefully apply your changes.
Be aware that you might get caught out by the "Body Text" and "First Paragraph", so you might need to change both of these separately.
output:
word_document:
reference_docx: template.docx
In the YAML part of your project, you have to change your referent.docx into "referent-doc.docx", surrounded by quotation marks (" "):
output:
word_document:
reference_docx: "referent-doc.docx"
# quoted with quotation marks, and here located in the same folder
P.S. Be sure to produce a good reference_doc: You need to start from a .docx file which is produced by markdown (e.g., using the knit button), in order to get a few titles, texts, figs and tables into a .docx. Then edit the styles; margin, etc. from Microsoft Word, and save it to a .docx file.

Default knit directory via YAML header in RStudio?

Is there a YAML option, which sets knit directory for an Rmd file? I.e. does the same as illustrated in Fig.1.
The problem is that every time I copy files or share a project with other people (which usually have different default RStudio options than I do), the knit directory should be reset manually in each file.
I'm aware of the global option (Fig.2), but I need a more reproducible solution which works for individual Rmd files in both both RStudio notebook and knitting modes.
And, for instance, I know that there is an option such as:
editor_options:
chunk_output_type: console
Any ideas?
Fig. 1 Setting knit directory via menu commands.
Fig. 2 Global option to set default knit directory in RStudio.
You could try to specify the knit field in the front-matter of our .Rmd file. Example .Rmd that would knit into "some_dir" created in your home directory:
---
title: "Untitled"
output: html_document
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding, output_dir = "~/some_dir/")
})
---
# Some content
Some content

Knit one markdown file to two output files

Is there a way to knit a single .md file to a .html or .docx in the working directory in R and simultaneously post a copy of the .html to another folder, possibly on another drive?
Alternatively, can the 'publish' button in RStudio be used to send a .md file to a location other than RPubs?
Actually, the knit button can indeed -
Render multiple outputs.
Use a custom output directory.
Example with output directory called output:
title: "multiple outputs"
output:
word_document: default
html_document: default
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding,
output_dir = "output", output_format = "all") })
Yes it's possible to render multiple outputs, but not with the "knit" Button in RStudio. Write your desired output in the YAML header and then use output_format = "all" as argument in
rmarkdown::render(<your-rmd-file.rmd>, output_format ="all")
So the YAML header looks like:
title: "multiple outputs"
output:
word_document: default
html_document: default
Or any option you want to set for the different output formats.
I don't know if it is possible to set different output directories, but I don't think so.

Remove bookmarks in PDF created by RMarkdown

When I knit a PDF using RMarkdown, it automatically creates bookmarks in the PDF file from any headers I use.
For example a "bookmark" in the PDF is created when I do:
this is a header 2
Is there any way to prevent this from happening?
Thanks.
Looks like you have to modify the latex template.
https://github.com/rstudio/rmarkdown/blob/master/inst/rmd/latex/default.tex#L105
Set bookmarks=false at that line above. Then save as mytemplate.tex file to your working directory, and use this option in your front matter:
output:
pdf_document:
template: mytemplate.tex

Compile R Script to markdown

Using RStudio's "Compile Notebook to html" feature, I noticed that a temporary .md file was created in the process, but deleted automatically. At one point I was lucky enough to see its content, and it is exactly what I need: the code chunks alternate with output chunks, all perfectly formatted.
So my question is: how do I generate such an .md file directly form an R script?
You could also run knitr::spin() directly from the R console.
If you run "Knit to HTML", a markdown file is temporarily created and deleted. To keep the markdown file, add the following to the YAML code at the top of the RMD file.
---
output:
html_document:
keep_md: true
---
Here is a helpful reference: https://bookdown.org/yihui/rmarkdown/html-document.html#keeping-markdown

Resources