Rmarkdown: Is there a function to create .Rmd documents? - r

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.

Related

Test if active document in Rstudio is RMarkdown

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?

How to include an external file in a Moodle question with R/exams?

In order to include statistical tables when using R-exams, I know that one can just use the option pages inside the function exams2nops(). But when using exams2moodle() how should one proceed?
In Moodle one can upload a file within a question and add a link to the embedded file. Is it possible to do it through R exams?
You can easily include various kinds of supplementary files in R/exams and then export them to Moodle or other learning management systems. Two steps need to be taken:
While processing the .Rmd or .Rnw exercise the supplementary file(s) need to be created in the current working directory (which is a temporary directory by default). This can either be done be creating the file via some R code or by copying an existing file. The convenience function include_supplement() simplifies this.
The supplementary file then needs to be included as a link in the question text. In .Rmd this would be something like [myfile.pdf](myfile.pdf) and in .Rnw exercises \url{myfile.pdf}.
An example for such an inclusion is the lm exercise template shipped along with the package, see: http://www.R-exams.org/templates/lm/. This exercise creates a .csv file on the fly within R and then includes it.
An example for the include_supplement() function is available in the Rlogo exercise template that copies the R logo image from the R system and then includes it in the exercise. See: http://www.R-exams.org/templates/Rlogo/.
Final comment: For a distribution table it would also be possible to include this directly as an HTML table in Moodle. For example, you could generate suitable Markdown or LaTeX code within the R code of the exercise.

How to use libraries across an r notebook?

I wish to use libraries across multiple .Rmd files in an r notebook without having to reload the library each time.
An example: I have loaded the library kableExtra in the index.Rmd file but when I call it in another .Rmd file such as ExSum.Rmd I would get this error:
Error in Kable....: could not find funciton "kable" Calls:...
If I load the kableExtra library again this problem goes away. Is there a workaround?
R Markdown files are intended to be standalone, so you shouldn't do this. There are two workarounds that come close:
If you process your .Rmd files within the R console by running code like rmarkdown::render("file.Rmd") then any packages attached in the session will be available to the code in the .Rmd file.
You can put all the setup code (e.g. library(kableExtra)) into a file (called setup.R, for example), and source it into each document in the first code chunk using source('setup.R'). Every file will run the same setup, but you only need to type it once.
The second is the better approach.

How to set default template for new ".R" files in rstudio

Background:
I like 'r'. I use Rstudio for it - it is a nice IDE. I use the revolution Analytics version of 'r', "revolution R Open".
I find that I type the same stuff in the annotation and structured programming regularly, and I want to save myself the re-typing.
Question:
How do I change the default file template so that the one I want, with some text already populated, comes up when I create a new blank R-script in Rstudio.
Clarifications:
I am not looking for this to be a manual process where I open one file, renaming it to a proper directory, and then work on it. I am looking to change the default so that this happens automatically.
Previous approach:
google
rstudio search (example)
search on stack-overflow
poking around rstudio menus/preferences
Thanks.
Quite late and not really a template but I think the solution is close: go to
Tools => Global Options => Code => Tab Editing => Snippets "Edit Snippets".
Example:
snippet header2
# Author:
# Date: `r paste(date())`
# --------------
# Author:
# Date:
# Modification:
# --------------
If you then type header {snippet} in a new script you get the text above with the date inserted automatically.
It was made possible to define user and system-wide templates for several file types with rstudio v1.3
A few gotchas:
it is necessary to create a 'templates' folder inside the .config directory
specific filetypes must be named according to this scheme
so, in this case, create a file called ~/.config/rstudio/templates/default.R or /etc/rstudio/templates/default.R for a user or system-wide .R file template, respectively.
I think this a 'less manual' solution than the accepted answer
As you are asking specifically for a Windows solution, you create a
templates folder (AppData/Roaming/RStudio/templates) and edit the default.R file.
# Create a templates folder
fs::dir_create(path = "~/AppData/Roaming/RStudio/templates")
# Create the file
fs::file_create("~/AppData/Roaming/RStudio/templates/default.R")
# Open the file in RStudio to edit it
usethis::edit_file("~/AppData/Roaming/RStudio/templates/default.R")
Now you can save the populated file and have created your new default R Script. Another possibility to not only keep this default template on your local machine would be to write a package for the sole purpose of containing a set of standardized default templates.
I've written a short post about it here.

How to access R markdown stored in a package

I want to create a package that produces reports using knitr that uses predefined templates.
At the moment I have a project directory that has this structure
R/createReport.r
R/reportTemplate.rmd
inside createReport.r I want to be able do something like the following;
require(knitr)
render('reportTemplate.rmd', output.file='someplace')
However I have no idea how to get the render function to locate my template file. Any help greatly appreciated!
You could store the template in yourpackage/inst/templates/sometemplate.Rmd and then access it with:
system.file("templates/sometemplate.Rmd", package="yourpackage")

Resources