Default knit directory via YAML header in RStudio? - r

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

Related

customizing r quarto pdf output file name

How do I customize the filename of r quarto pdf document when I render? Before when I was using Rmarkdown I was using the following code in the YAML:
---
title: "Some title"
author: "First Last"
date: "`r format(Sys.Date(), '%d %B, %Y')`"
output: pdf_document
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = file.path(dirname(inputFile), paste0(Sys.Date(),"_Report","_FirstLast",".pdf"))) })
---
When I hit the "Knit" button the filename of the pdf document would be 2022-08-08_Report_FirstLast.pdf
Is there a way to do this with quarto pdf? I think the quarto_render function needs to be used but don't know how.
Use the output_file argument of quarto_render function.
file_name = paste0(Sys.Date(),"_Report","_FirstLast", ".pdf")
quarto_render("your_qmd_file.qmd", output_file = file_name, output_format = "pdf")
Now about the approach you are trying,
Firstly, there's no such knit yaml key
in quarto AFAIK
Secondly, although r-code can be used in code-chunk option prefixed be !expr , it's not possible to use inline R code in document yaml section right at this moment of answering this question (03 Sep, 2022). See this discussion on Github.
Though there are some suggested workaround for using r-code in yaml in this discussion on Github, but using quarto_render to control output filename seems the easiest option in your case.
And additionally, if your output file name is simple (that is, without any r-code syntax), you can use output-file yaml option.
---
title: "Testing Output file name"
format:
pdf:
output-file: "output_file_name"
output-ext: "pdf"
---
## Quarto
Quarto enables you to weave together content
and executable code into a finished document.
To learn more about Quarto
see <https://quarto.org>.
## Running Code
When you click the **Render** button
a document will be generated that
includes both content and the output of
embedded code.
This will create the output file named output_file_name.pdf in the directory where the source file is.
If you're comfortable using Quarto CLI in bash shell (which may be more convenient for automatic report generation), you could date-stamp your outputs like this.
now=`date +"%Y-%m-%d"`
quarto render my_doc.qmd --output "./out_$now.pdf" --to pdf

How to export R Notebook to LaTex (.tex)? [duplicate]

In R, when compiling a markdown document using rmarkdown::render(...), how can I retain the intermediate .tex file that is produced from knitting.
I have tried setting the clean=FALSE argument, but this retains the figures, not the final tex file or auxiliary files, which I need to inspect for debugging purposes.
You can specify it in your YML header with:
output:
pdf_document:
keep_tex: true
---
More options on the rmarkdown site.

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

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
---

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.

Save a rmarkdown ioslides presentation as standalone website

I would like to save the presentation generated by knitr as a html file. Which I am able to share with people as standalone html file.
If I use Run Document button in R studio (server) this generates a file with the .Rmd extension.
---
title: "Standalone"
author: "MarketRedesign"
date: "10-7-2014"
output: ioslides_presentation
---
## Slide with R Code and Output
```{r}
summary(cars)
```
When you press the knit HTML button, a HTML file IS always created by RStudio in the same directory as the R Markdown file lives in. The HTML file will also have the same name as the R Markdown file that was used to create it.

Resources