The post was written from the view of a Windows user, if problems were came up because you use an other operating system, don't hesitate to contact us to get the solutions for that. As it is widely known, to run the templates we use R. Writing a template I would suggest to use Notepad++, although the rest of the development team swear on Emacs/ESS :)

Just to avoid annoying little mistakes I might note, one should be careful with some settings in the IDE:

Encoding should be UTF-8 without BOM and

End-of-line characters has to be set to Unix.

At the first sight the template (without the Header, which will be elaborated in a later stage of this post) seems like a "normal text filled in with R commands". This impression doesn't exist without reason. The text parts help the user to understand the statistical method, interpret the results or sometimes just broaden the user's knowledge.

The aim of a template and our web application is to present a user-friendly interface and output for a wide scale of people. Thus it can be available for the less R-friendly users and for them who already digged deeply into the programming field as well.

What do you need to being able to write or run a template?

Before starting to elaborate the way of writing a template, let me write briefly which steps have to be done before starting to write one. As a reason, that the templates run in R, basically it is unavoidable that program to be installed. Moreover it is advisable to check which version, because the older version do not support some new features. These needed packages are the rapport and the pander. There are various ways to install them (if we didn't do that before), the simplest one beside heading to CRAN is to do that directly from GitHub:

These are the preparations which are inevitable to be done before we start to begin producing a template, although you might also consider installing Pandoc to produce HTML, pdf, docx, odt and other popular formats.

Specialties

For R users producing a template wouldn't need too much extra skills to gain. On the other hand, there are still some things which one should look at on while writing one:

The syntax

Templates have to be written in brew syntax. The most important to note in connection with that is the two kind of special tags which this kind of syntax uses:

<%= command %><% command %>

These two codes look pretty the same and actually doesn't differ too much, but still in a crucial way.

Both of them are good for running R commands, but the first will run each expression after each other and send the markdown format result to the interface. The code in the belly of the second tag is to be used for unprinted results and mostly for conditional statements or loops. It is strongly advised to use the prior tag for even expressions without a result to use the cache and improved error handling.

To see the use-cases of the tags a little bit clearer, let me show you some basic commands as an example with loading a built in database of the rapport package, the Internet Usage Survey in 2008:

<%=data(ius2008)colnames(ius2008[1])%># That was not more complicated than just producing a string ("gender")

Let's see how to use the tag without the equal sign:

<%if (colnames(ius2008[1])=="gender") { %>Thefirstvariableoftheius2008databasecontainstheresponder's gender.<% } %> # The following sentence will be written on the interface we use: "The first variable of the ius2008 database contains the responder'sgender."

Next to that we can obviously use additional command like in R:

<% if (colnames(ius2008[1])=="gender") { %>Thefirstvariableoftheius2008databasecontainstheresponder's gender.<% } else { %>We do not know what kind of information the first variable of the ius2008 database contains.<%}%> # In this case only the second sentence will be shown.

Header

In order to make the template enable to run via R, and to provide some meta-tags, filling in with some information about itself, that needs to have a beginning part, called Header. The Header part also has a beginning (<!--head) and a finishing (head-->) tag like the others elaborated above.

The Header contains some mandatory and some optional fields. With the help of that, one can show his/her template's most important properties and specify the fields, which are necessary to run that.

As a mandatory part one has to give a Title, an Author and a short Description about the template. Sometimes the Title tells everything about the template for most of the users but it could be useful also to elaborate that, especially in complex templates.

The writer can optionally extend the amount of the information with giving his/her email address in case to exchange the experience with the users. There is also a possibility to give one or more example(s) how to run it in R. An example of the example is:

The aboved listed information are mostly important for the users (and the example also for the developper to easily test the new template with tpl.example function), but we still need to specify some other things to tell R which variables to use inside the template body.

It could happen that running a template would require a specific dataset or a package. In the Header one can set that in a new line, started by Packages/Data required. In the package line, one should write the names of the required packages separated by a comma, but don't forget, the chosen package should be installed on the computer before we want to use that in the template.

The Data required command works like a boolean type, we just set it TRUE if we needed a specific dataset and later specify that while running the template. A short example of them looks like:

Packages:nortest# nortest package is needed to run the templateDatarequired:TRUE# data should be specified in the R code

With this part we slightly touched the field and arrived to the input specifications, but while the dataset and the packages are optional to be set as required, variables are always likely to be specified. To give some spice to it, next to variables there is a possibility to set any parameters from the template as optional.

Each lines contain 4 parts, separated by a pipe character (|). In the first part one should specify the name of the input, what will have to be written after into the R code to specify the variable or the parameter we want to use.

In the second part of the line can be set the type of the parameter and which variables are required to use, thus they have to be specified in the R code while running the template. We can set that with a star (*). In the lines of the variables we don't want to set as required, we should just leave out the star sign.

There are different types of variables and parameters as well. In the case of an optional feature of the template we should set that a default FALSE there, which can be overwritten from the command line by passing TRUE to that field.

To make it a little bit clearer let me show you some examples of the specifications from the t-test.tpl:

x|*numeric|Xvariable| Dependent (responsevariable) # required numeric variablemu|number[1,10] |Meanvalue|Meanvalueforone-samplet-test# optional to be passed to Rpaired|FALSE|Pairedt-test|Carryoutpairedt-testornot# to be used in conditional statements

The last two parts can be useful in the template body to have conditional expressions etc.

Formatting the textAfter we have seen above how to compile the header of the template and also checked out the brew syntax, it could be useful to look at some practical way to format our template, and see which tags of Pandoc's markdown help us to present the template in a nice way.

Title

Formatting the title one could have several levels. All of them lies on the beginning of the line in ATX stlye. For example:

# Top header## Subtitle### Minor title#### And even smaller parts up to 6 # signs

TextThere are several ways to make the text nicer and easier to read. For example we can emphasize part of that or add links which direct us to other homepages. We were using these tools to extend our templates and here I will show how you can also do that with yours.

_If you put the text in the belly of two underscore, it will be italicized_ and [You can connect a part of the text with a homepeage, with writing it between two scared brackets and the link in parenthesis after. Most of the links in the templates will direct you to one article on wikipedia](www.wikipedia.org)

So (finally) how to write a template?After we successfully run over the most important special properties of the template-writing now we can start to produce one! In this example I did not focus on using complicated R commands or making an extravagant outlook, the aim was rather to show how full template looks and to give a summary.