# How to write an arara rule for knitr

I've been trying to write a rule for invoking knitr via arara, but I don't seem to know what I'm doing, apparently.

The arara manual explains how to define custom rules. To do so, it is necessary to include the path to be searched for the custom rules inside of the config file. So, my araraconfig.yaml file includes:

!config
paths:
- /path/to/custom/arara/rules


Now, since knitr reads .Rnw files but, by default, arara is set up to handle only .tex, .dtx, and .ltx files, it is necessary to add support for the .Rnw file, too. As such, my araraconfig.yaml file also includes:

filetypes:
- extension: tex
- extension: dtx
- extension: ltx
- extension: Rnw
pattern: ^(\\s)*%\\s+


Finally, I've written knitr.yaml (which is in /path/to/custom/arara/rules), which is:

!config
identifier: knitr
name: knitr
command: <arara> Rscript -e "library(knitr); knit('"@{getBasename(file)}".Rnw')"


since knitr can be invoked from the command line via Rscript -e "library(knitr); knit('myfile.Rnw')".

Now, say I have the following MWE, myfile.Rnw:

% arara: knitr
% arara: pdflatex
% arara: pdflatex

\documentclass{article}

\begin{document}

<<cars, echo=FALSE, message=FALSE, fig.cap="Car graph", fig.lp="fig:">>=
library(ggplot2)
CarPlot <- ggplot() +
stat_summary(data = mtcars,
aes(x = factor(gear),
y = mpg
),
fun.y = "mean",
geom = "bar"
)
CarPlot
@

\end{document}


If I invoke arara myfile from the command line, I get:

I didn't find any directives in 'myfile.Rnw', and so didn't do anything. Is that what you really wanted?

Alternatively, if I invoke arara myfile.Rnw, I get:

I'm sorry, but the file 'myfile.Rnw [.tex, .dtx, .ltx, .Rnw]' does not exist. Note that when you provide only the basename (i.e, the filename without the extension) or with an unknown extension, arara will try to look for files ending with the predefined extensions [.tex, .dtx, .ltx, .Rnw] in that order. You can override the order, the search pattern or even add support for new extensions through the configuration file. Please refer to the arara manual to learn more about this feature.

This latter message is also the message that I get when arara is invoked from within TeXShop. Since I would ultimately like to develop a workflow with knitr and arara from within TeXShop, I would like a rule for knitr that works when arara myfile.Rnw is invoked.

I'm not entirely sure why specifying the extension makes a difference here, and I'm also not sure why, when the extension isn't specified, no directives are found in the file since there are, in fact, directives contained in myfile.Rnw.

Thus, I suppose my question is ultimately how do I write an arara rule for knitr that will work when arara myfile.Rnw is invoked (i.e., when the command is invoked with the file extension).

(Though any insight about the different behavior depending on whether the file extension is specified or not as well as any insight about the inability of arara to find directives in the former case would also be greatly appreciated.)

Note: I assume that % arara: pdflatex will break once the knitr issue is figured out since the arara rule for pdflatex is defined as <arara> pdflatex @{action} @{draft} @{shell} @{synctex} @{options} "@{file}", and I think @{file} returns the file extension as well as the file name, so I assume I will need to write a new rule for invoking pdflatex using "@{getBasename(file)}". However, one step at a time ...

## Update:

Paulo started helping me with a bit of troubleshooting in the chat room. (Paulo's help starts about here.) With his help, there has been a bit of progress.

First, it seems that there is an error in the arara manual. ^(\\s)*%\\s+ in the araraconfig.yaml file should really be ^(\s)*%\s+.

Second, it seems that the rule should have a set of arguments, even if that set is empty. So the knitr.yaml file should be:

!config
identifier: knitr
name: knitr
command: <arara> Rscript -e "library(knitr); knit('"@{getBasename(file)}".Rnw')"
arguments: [] # note this addition


With these changes, calling arara myfile will work (sort of).

(Note that calling arara myfile.Rnw results in the same error noted above.)

arara runs and says:

Running knitr... SUCCESS


However, there is no .tex file that gets written to the directory, which is what should be the result of processing a .Rnw file with knitr. Specifically, if one calls Rscript -e "library(knitr); knit('myfile.Rnw')" from the command line, the result is a .tex file. But the result from this 'successful' arara run is nothing ...

I'm not sure where to go from here. Here's the log file from running arara --log myfile.

07 May 2014 19:48:19.360 INFO  Arara - Welcome to arara!
07 May 2014 19:48:19.372 INFO  Arara - Processing file 'myfile.Rnw', please wait.
07 May 2014 19:48:19.373 INFO  DirectiveExtractor - Reading directives from myfile.Rnw.
07 May 2014 19:48:19.374 TRACE DirectiveExtractor - Directive found in line 1 with knitr.
07 May 2014 19:48:19.378 INFO  DirectiveParser - Parsing directives.
07 May 2014 19:48:19.427 INFO  CommandTrigger - Ready to run commands.
07 May 2014 19:48:19.427 INFO  CommandTrigger - Running 'knitr'.
07 May 2014 19:48:19.427 TRACE CommandTrigger - Command: Rscript -e "library(knitr); knit('"myfile".Rnw')"
07 May 2014 19:48:19.628 TRACE CommandTrigger - Output logging:
07 May 2014 19:48:19.628 TRACE CommandTrigger - [1] "library(knitr); knit('myfile.Rnw')"

07 May 2014 19:48:19.629 INFO  CommandTrigger - 'knitr' was successfully executed.
07 May 2014 19:48:19.629 INFO  CommandTrigger - All commands were successfully executed.
07 May 2014 19:48:19.629 INFO  Arara - Done.


It seems that there might still be hope.

And, if possible, I would still really like a setup that works when arara myfile.Rnw is called, not just arara myfile.

• Please consider writing what you discovered up as an answer. When I saw no answer, I almost assumed no solution but even in your case, you have part of a solution (and I did not encounter the same oddities implementing your solution here).
– cfr
May 30 '14 at 0:19
• @cfr It really works for you? I still get no .tex file output, and I've tried putzing around with a few more things since, but still no dice. What OS do you use again? You're a Linux user, right? Maybe it would be better for you to write up a (partial) answer, since it works on your OS? May 30 '14 at 20:56
• It might be something specific to knitr. What I meant was that the basic problem of setting arara up to work with other file extensions (whether or not the extension is specified in the filename passed to arara) was solved nicely by your question. And this was the only useful thing I found when searching for solutions to the 'didn't find any directives' problem. Yes, I'm on Linux. But I was also specifically trying to enable .mp as an extension to get it to recognise the directive telling it to run mpost. I didn't get the non-existence issue at all, though.
– cfr
May 30 '14 at 21:07
• You are probably right. Perhaps a general question would be better. If the problem then turns out to be specific to knitr, you could edit this question to sharpen it. What do you think?
– cfr
May 30 '14 at 21:14
• OK. tex.stackexchange.com/q/181340/39222 is the link to the new question. Feel free to edit either question or answer. I hope this helps figure out the problem you're experiencing. (It seems unfair to come up with a solution which only works properly for somebody else!)
– cfr
May 30 '14 at 22:08

In version 6.1.0 of arara as of now one can simply
See page 12 of the arara manual.
and both the answers : general one and one especially for knitr