29

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.

Adam Liter
  • 12,567
  • 2
    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 00: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? – Adam Liter 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
  • I don't have knitr to test. Do you want me to post the stuff I got working for metapost so that you can see whether or not the issue is specific to knitr or to your OS or whatever? – cfr May 30 '14 at 21:09
  • @cfr maybe it would be better to post a general question for all file extensions other than the three that are supported out of the box, and then answer that one, since this question is specific to knitr? – Adam Liter May 30 '14 at 21:10
  • @cfr Or that could work, too, if you'd like to do that. – Adam Liter May 30 '14 at 21:10
  • 1
    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
  • @cfr Sounds good to me! – Adam Liter May 30 '14 at 21:14
  • 1
    OK. http://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
  • Did you see http://tex.stackexchange.com/a/202487? – cfr Sep 22 '14 at 12:29
  • No I hadn't seen that, @cfr. Thanks for pointing it out! I'll look at it more closely next week when I have more time. – Adam Liter Sep 22 '14 at 19:20
  • Are there any news here? – TeXnician Aug 09 '18 at 17:19
  • @TeXnician Yes. It's now possible to put together a working arara rule for knitr. @PauloCereda and I have been discussing it here. There are still a few things that aren't quite ideal, in my opinion. Anyway, if you follow that link, you can see a sketch of a knitr rule. It should still be modified a bit, probably. I'll aim to post an answer in the next couple of days. I'm a bit busy, but something along the lines of what you'll find at that link should work. – Adam Liter Aug 13 '18 at 13:58

1 Answers1

1

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