[Tex/LaTex] How to write an arara rule for knitr

araraknitr

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}

A graph about cars (Figure~\ref{fig:cars}).

<<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.381 INFO  TaskDeployer - Deploying tasks into commands.
07 May 2014 19:48:19.381 TRACE TaskDeployer - Task 'knitr' found in '/Users/adamliter/Dropbox/Local-texmf/arara/rules'.
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.

Best Answer

In version 6.1.0 of arara as of now one can simply

% arara: knitr: { quiet: yes }

See page 12 of the arara manual.

But also do see
Add custom extensions and directives to arara question

and both the answers : general one and one especially for knitr

Related Question