ess - how to make file templates (boiler plate texts) - r

I've been using r-autoyas 'as-is' from what I got after installation. How do I customize it or install any other plugins that let me have templates for R documents (i.e. .R, .Rmd) whenever I create a new file?
For example, I want to put some boiler plate text:
Copyright info
Author
Date
File sections
The 'File sections' are different sections marker texts. For example:
## Library Import
## Functions
## Main Code
Maybe these can all be done with yas-snippets, but I don't know how to create custom snippet yet.

Related

How to have R markdown templates share the same set of supporting files?

I have an R package that has 4 markdown templates that can be used to knit HTML documents after installation.
These templates share the same set of supporting files (css, images, scripts, html files, ... - about 6 MB of files).
RStudio's R Markdown Document Templates page: sates that:
If want to include supporting files with your template they should be
placed in the skeleton directory. These files will be automatically
copied alongside new documents.
However, I would like to know if there is there a way for multiple templates to share one copy of these supporting files when creating a new document from a template?
The goal is to reduce the total download size of the package (package vignettes also use the same supporting files, so there are 5 * 6 MB copies all up), and to improve reproducibility (i.e. not having to update multiple copies of the assets when I make a change).
I think the answer might be further down the help page you mentioned, under "Custom Formats". Given that this is happening in a package, you should be able to create a folder, say inst/my_resources/ and place the various required files in there, perhaps in their own subfolders e.g. inst/my_resources/css, inst/my_resources/images etc. Then you can write some functions that call those resources. For the CSS, for example, follow the example of the "Quarterly Report" function in the RMarkdown help more or less directly:
First define a function to use custom CSS and header files:
use_my_css <- function(toc = TRUE) {
# get the locations of resource files located within the package
# remember that package installation moves everything in inst/ up a level
# in the directory hierarchy
css <- system.file("my_resources/css/my_styles.css", package = "mypackage")
header <- system.file("my_resources/html/header.html", package = "mypackage")
# call the base html_document function
rmarkdown::html_document(toc = toc,
fig_width = 6.5,
fig_height = 4,
theme = NULL,
css = css,
includes = includes(before_body = header))
}
Then use that function in place of the default in your template header:
---
title: "My Specific Format"
output: mypackage::use_my_css
---
The same principle should apply to the other shared resources, such as images, scripts, and so on. Within the Rmd template files you ought to be able to have code chunks that run functions leveraging system.file("<relative path>", package = "mypackage") to insert the relevant shared resources.

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.

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

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.

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.

slidify lectureSite: workflow and customizations

I am getting started with http://slidify.github.io/lectureSite/. I like the idea and the template very much. However, being new to this, I am struggling with customization.
Let me first explain what this 'lectureSite' architecture offers. The main code is based on rmarkdown and slidify and the customizations of the layout are mainly controlled in css files. The 'lectureSite' consists of an html webpage that serves as a contents page and starting point to access different slides ('lectures'), much of it created automagically. It looks as great as it sounds!
Each lecture is stored in a separate directory with its own 'assets' directory. So in a course made of 10 lectures, one has 10 directories, each with its own css and js. However, to achieve a common style for the whole course, one would like most of the customizations to be shared among all the lectures. My main problem is how to deal with this.
Question: How should I deal with the css customizations that I would like to share among all the lectures?
I have been successful in customizing each lecture by inserting the css code into each rmarkdown file between <style> and </style> tags, but now I would like to take the customizations to a place where they may be shared. I expected that by including a slidify.css file inside assets/css the styles would be picked up, but they are not: could I be doing something wrong or was my expectation incorrect to begin with? And besides, there are so many of those assets/css directories that it would be tedious to have to copy the css into each every time it is modified. Is there a mechanism to set a single css file that would override the css inside assets/css?
I also tried to make a 'declaration' at the top of the rmarkdown file (something I saw there: http://rmarkdown.rstudio.com/html_document_format.html) with:
css : slidify.css
but that gave the following error message:
pandoc: Could not fetch slidify.css
slidify.css: openBinaryFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 67
Execution halted
An alternative would be to source() a text file with the customization between <style> and </style> tags. Are there any disadvantages to this approach? And btw what is the code for sourcing an external file from rmarkdown?
Consider the following structure:
assets -> put custom img/js/css/layout assets
lectures -> folder containing lectures
Lecture 01
Lecture 02
Lecture 03
libraries -> frameworks, highlighters and widgets
index.Rmd -> Rmd source for home page
site.yml -> Site related configuration
and suppose you are inside one of the Lectures index.Rmd files. I found the following seems to 'by-pass' the local assets directory to source the 'root' directory instead:
url : {lib: "../../libraries", assets: "../../assets"}
If you place your css files into "../../assets/css/custom.css", that is the 'master' assets directory at the top of the structure (at the root, to say it differently), then it will be sourced by slidify. I deleted all the other assets directories and didn't find it did any harm (all they contained to begin with was one single file ribbons.css). Not extensively tested, but it worked on Firefox and Chrome.
In this way, I can have a single customization css for all the chapters.
However, this structure appears to work only for the html5slides framework and not for io2012. For io2012 I explain here what I did (it worked for me, but I have no idea if it's the right way):
https://github.com/ramnathv/slidify/issues/409
Somewhat more involved. Hopefully future versions of slidify will make it easier. One problem with dumping style files into the root assets directory is that, with the html5slides framework, all files are sourced. So my idea of having framework-specific css files in the assets directory doesn't work, because the styles are all sourced and some overwrite one another. So keeping the custom css files in framework-specific directories is probably a better approach anyhow.
I also found the following way of sharing R customizations and knit opts (or whatever they're called)
```{r 'preamble', message = FALSE, warning = FALSE, error = FALSE, echo = FALSE, tidy = FALSE, comment = NA, cache = FALSE} # probably several redundant ones in there
require(knitr)
opts_chunk$set(echo = FALSE, cache = FALSE) # example of knit options
source('../../shared/shared.R') # here I share common R code
```

Resources