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.
Related
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?
I am rendering two documents, that cross-reference items in each other. For that I am using the LaTeX package zref.
To make zref work, it needs the *.aux file of the documents which are created when calling pdflatex.
Unfortunately, using RStudio and its basic approach to render the document (the knit button, Cmd+Shift+K or rmarkdown::render()) these files will be deleted after the compilation was successful.
Unchecking the global option Tools -> Global Options -> Sweave -> Clean auxiliary output after compile does not help.
I know of two options to go around this:
Manually compile the tex file after the pdf was rendered.
Write a makefile that does that.
But is there another option I am not aware of?
rmarkdown::render() eventually calls tinytex::latexmk() to compile the intermediate .tex to .pdf. To preserve auxiliary files, you need tinytex::latexmk(..., clean = FALSE). One way to set clean = FALSE is through the global option options(tinytex.clean = FALSE). You can set this in either your .Rprofile or a code chunk of your Rmd document.
The RStudio option you mentioned is only for Sweave documents (.Rnw).
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.
I wonder whether I can use knitr markdown to just create a report on the fly with objects stemming from my current workspace. Reproducibility is not the issue here. I also read this very fine thread here.
But still I get an error message complaining that the particular object could not be found.
1) Suppose I open a fresh markdown document and save it.
2) write a chunk that refers to some lm object in my workspace. call summary(mylmobject)
3) knitr it.
Unfortunately the report is generated but the regression output cannot be shown because the object could not be found. Note, it works in general if i just save the object to .Rdata and then load it directly from the markdown file.
Is there a way to use objects in R markdown that are in the current workspace?
This would be really nice to show non R people some output while still working.
RStudio opens a new R session to knit() your R Markdown file, so the objects in your current working space will not be available to that session (they are two separate sessions). Two solutions:
file a feature request to RStudio, asking them to support knitting in the current R session instead of forcibly starting a new session;
knit manually by yourself: library(knitr); knit('your_file.Rmd') (or knit2html() if you want HTML output in one step, or rmarkdown::render() if you are using R Markdown v2)
Might be easier to save you data from your other session using:
save.image("C:/Users/Desktop/example_candelete.RData")
and then load it into your MD file:
load("C:/Users/Desktop/example_candelete.RData")
The Markdownreports package is exactly designed for parsing a markdown document on the fly.
As Julien Colomb commented, I've found the best thing to do in this situation is to save the large objects and then load them explicitly while I'm tailoring the markdown. This is a must if your data is coming through an ODBC and you don't want to run the entire queries repeatedly as you tinker with fonts and themes.