I want to add a small function to my RStudio AddIn which extracts all chunks from an Rmarkdown document, does some transformation and displays it as new editor window in RStudio. Basically, that will be a thin wrapper around knitr::purl. The function will eventually be called via the AddIns interface in RStudio and should consider the currently opened editor window as input.
What I was wondering though, is how to include a check whether the current editor window shows indeed an RMarkdown document.
I could use the file extension like this:
if (grepl("\\.Rmd$", rstudioapi::getSourceEditorContext()$path)) {
## Rmarkdown -> do knitr::purl
} else {
## No Rmarkdown -> give a warning
}
But this feels hackish and a new Rmarkdown document, which is not saved yet (and opened via File -> New File -> RMarkdown...) will fail this check.
As RStudio itself does somehow recognize whether a file is meant to be an Rmarkdown document (creating a file as highlighted will add the Knit button for example, even if it not saved yet), I was wondering how I would ultimately find out the "file" type of an editor window?
I skimmed through the Admin Guide Appendix hoping to find a function like isRMarkdown to be called via rstudioApi::executeCommand but to no avail.
What would be a reliable way to check the file type associated to the current editor window in RStudio?
Related
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:
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:
prevent the creation of HTML files altogether,
still see the results in an HTML format in RStudio's preview window, and
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?
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.
When using Rstudio, one can use File -> New File -> R markdown... to create a new R markdown file from a template, with YAML headers and some examples already buffered. Is there a function to replicate this without an IDE (in this case, saving the template Rstudio would buffer in a new file)?
I'd rather start my .Rmd file from template rather than from scratch, but not using an IDE.
rmarkdown::draft seems to do what you want. It creates a new file, based on a template. You can provide a link to a custom template. See the help page for details.
RStudio renders mynotebook.nb.html file every time R Markdown document mynotebook.Rmd is saved. This process does not involve running code in chunks, so it is much faster than knitting a notebook into mynotebook.html. However, for large .Rmd documents, saving nb.html files can take a long time, and, unfortunately, it is required to wait for it to finish before one can start using the notebook again and run code in chunks.
Is there a way to configure RStudio to not to create nb.html files on save of an R Markdown document?
I've found out you can delete relevant output entry from the top section of your file. In my case it looks like:
---
title: "Document"
output:
html_notebook: default
---
Which causes creation of .nb.html on every save. If you remove the output tag, file is no longer created automatically. You can still knit to any output file from the Knit menu on the top (or press Ctrl+Shift+K for default. This will run all the chunks again, which may take a while.
You may want to consult this guide book for more information on how YAML tags work. I'm just beginning with them!
Another reason (and solution) might be, that you clicked incidentally on the "Knit on Save" button located just under the Save-to-Disk-Symbol in RStudio. This was in my case the problem. Usually you should be able to save a rmd-file without triggering the knitting process.
So - given this scenario - just uncheck the "Knit on Save" button.
I recently updated my RStudio and while writing an R Markdown document in the Source Window, whenever I run a code in a chunk of the RMD, the output is shown in the source window itself in the following manner. It gets too messy when there are huge plots. Would like to disable this feature if possible and revert back to good old style of displaying output in the Console/Plot viewer window only.
Output executed and visible below the Chunk in the Source Window
Thanks
Try this in Rstudio.
Tools > Global Options > R Markdown > Uncheck: Show output inline for all R Markdown Documents.
That should disable inline code chunk output when you're editing R Markdown documents.
Does that get you what you're asking?
Looks like OP had his question answered, but if toggling "Show output line for all R Markdown Documents" has no effect, you may be dealing with the bug detailed here: Chunk output in console not always available. You'll have to uncheck that box, restart, and then select the option to "Chunk Output in Console" from the output menu before re-enabling the former setting.
Is there a elegant way to automatically trigger rendering of a Rmd file, if it has changed ?
Better still, is it possible to preview markdown dynamically in Rstudio ?
Yes, you can use the servr package. Just type servr::rmdv2() or with the argument daemon = TRUE in RStudio, and your HTML output will be automatically refreshed in the RStudio viewer as you save the source document.