1

I would like to keep the base folder of my R Markdown project tree as uncluttered as possible and still use the Knit button in RStudio to see some results as an HTML output in the RStudio's preview window. However, every time I knit, an HTML file is created to the base folder along my .Rmd files.

My folder structure might look like this:

enter image description here

If I've understood correctly - and do please correct me if I'm wrong - it is not very straightforward to output the HTML files to an upstream folder (here:"results" folder) from an R Markdown file by pressing the Knit button, so I would be willing to:

  1. prevent the creation of HTML files altogether,
  2. still see the results in an HTML format in RStudio's preview window, and
  3. only when I'm really willing to save an HTML file, save (export) such a file and (manually) transfer it to the results folder.

So, if there is no easy way of directing the HTML files to the "results" folder, can I prevent the creation of HTML files altogether and only preview the results in the preview window?

jaggedjava
  • 440
  • 6
  • 14

1 Answers1

2

I don't know of a way to attach this to the knit button, but you could write a function that does this:

  • get the current file open in the edit pane. This answer gives details on that.
  • run rmarkdown::render() with the output_dir argument set the way you want.

You can attach this function to a keyboard shortcut in RStudio using these instructions.

Here's a simple version of the function:

  knit2 <- function(filename = rstudioapi::getSourceEditorContext()$path,
                    output_dir = tempdir()) {
    result <- rmarkdown::render(filename, output_dir = output_dir)
    getOption("viewer")(result)
  }

Simply call knit2(), and the output will be written to tempdir(), which means the preview will appear in the Viewer pane. If you want it to write to "results", call it as

knit2(output_dir = "results")

If that's not a subdirectory of tempdir(), the preview will appear in an external browser.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • I tested this method and it works 100% perfectly. This approach is likely to be far more practical than the one I was looking after (less mouse clicks here and there). The question remains, doesn't everybody want to knit .Rmd files this way or is it just me? I'll be happy to accept this answer if I see no contending answers soon. BTW: the resulting HTML file opened to an external web browser. Is there a way to preview the result in RStudio's preview window? – jaggedjava Sep 17 '22 at 12:45
  • If you put the file into the directoy `tempdir()` then it will be opened in the viewer pane. I'll edit the answer... – user2554330 Sep 17 '22 at 13:38
  • Works perfectly. Do you happen to know how to have it opened in the RStudio viewer window (instead of the pane)? – jaggedjava Sep 17 '22 at 16:31
  • No, I don't know if there's a way to trigger that kind of viewer from R. – user2554330 Sep 17 '22 at 17:48
  • I couldn't find an answer to that either. Thank you very much for the solution of the original question. – jaggedjava Sep 17 '22 at 18:05