Pass arguments to rmarkdown::render in knit button

Is it possible to pass arguments to rmarkdown::render() through the Knit button in RStudio? My naive attempt was to define arguments just like I normally would for an output style. For example:

    output_dir: report

However, this does not work. When I press the Knit button, I see the following at the top of the Rmarkdown pane in RStudio, so the output_dir argument specified in the YAML is not being passed on.

==> rmarkdown::render('/Users/jakethompson/Desktop/test-rmd/test-doc.Rmd',  encoding = 'UTF-8');

Basically, I would like to be able to change the output directory and/or filename in the YAML. I could write a custom function and put it in a package as suggested here, but that would still require me to pass arguments to that function (i.e., the output directory and filename) in the YAML. Alternatively, I could write a custom function directly in the YAML, as below, but that would then require me to copy and paste the function code into every Rmd, which also seems like a less than ideal solution.

knit: (function(input, ...) {
      output_file = 'my-file-name',
      output_dir = 'my-directory',
      envir = globalenv()

Here is a working reprex and my current session info if that is helpful:

title: "Test Document"
    output_dir: report
output: html_document

```{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 <>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur 10.16
#> Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
#> Package version:
#>   base64enc_0.1.3      compiler_4.0.3       digest_0.6.27       
#>   evaluate_0.14        glue_1.4.2           graphics_4.0.3      
#>   grDevices_4.0.3      highr_0.8            htmltools_0.5.0.9003
#>   jsonlite_1.7.2       knitr_1.30           magrittr_2.0.1      
#>   markdown_1.1         methods_4.0.3        mime_0.9            
#>   rlang_0.4.9          rmarkdown_2.6.4      stats_4.0.3         
#>   stringi_1.5.3        stringr_1.4.0        tinytex_0.28        
#>   tools_4.0.3          utils_4.0.3          xfun_0.19           
#>   yaml_2.2.1

Created on 2020-12-29 by the reprex package (v0.3.0)

This is not the sort of thing you can do in an interactive knit, but there are a few lateral approaches you might consider.

There is an option you can set called rstudio.markdownToHTML which gives you complete control over what happens when the Knit button is clicked. Note however that since RStudio mostly relies on the rmarkdown package's default behavior, most of the R Markdown tooling in the IDE stands down when that option is engaged since it has no idea what you're doing. More here:

You might also consider writing an add-in which starts a background job that runs whatever render command you like.

1 Like

I would second this as a feature request, for the knit button.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.