58

What is the command-line knitr equivalent of R CMD Sweave myfile.rnw?

Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173

4 Answers4

67

The general solution (works regardless of the R version):

Rscript -e "library(knitr); knit('myfile.Rmd')"

Since R 3.1.0, R CMD Sweave has started to support non-Sweave documents (although the command name sounds a little odd), and the only thing you need to do is to specify a vignette engine in your document, e.g.

%\VignetteEngine{knitr::knitr}

To see the possible vignette engines in knitr, use

library(knitr)
library(tools)
names(vignetteEngine(package = 'knitr'))
# "knitr::rmarkdown" "knitr::knitr" "knitr::docco_classic" "knitr::docco_linear"
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
  • Damn... I was going to post that but I figured I check around a little bit to see if there was a less cheesy alternative... – Dason Jun 08 '12 at 06:06
  • 6
    what's so cheesy? Short of being actually built into R, you can't do much better. My Makefiles have `$(RSCRIPT) -e "library(knitr); knit(\"$*.Rnw\")"` – Ben Bolker Jun 08 '12 at 07:00
  • 2
    Brilliant. In this particular instance, I was still using LaTeX, instead of R Markdown, so it was `Rscript -e "library(knitr); knit('myfile.rnw')"` – Jeromy Anglim Jun 08 '12 at 07:10
  • 3
    Note that after making this change, I needed to add `require(methods)` in order for a lattice plot to display properly; because [apparently Rscript doesn't load the methods package by default](http://ucfagls.wordpress.com/2011/06/08/stratigraphic-diagrams-using-analogue/). – Jeromy Anglim Jun 08 '12 at 07:21
  • @BenBolker I had thought I remembered Yihui mentioning something about R CMD ____ for knitr. In comparison to having something like R CMD the Rscript alternative didn't seem as nice. But thinking back on it the only reason I was thinking of that was because the first time I was learning about knitr I was also trying to get roxygen working from the command line. – Dason Jun 08 '12 at 07:51
  • 10
    see https://stat.ethz.ch/pipermail/r-devel/2011-November/062704.html for a discussion about `R CMD foo`; unfortunately the response from R core was (as almost always) you can use `Rscript -e`, and this bothers me a lot; I have a shell script https://github.com/yihui/knitr/blob/master/inst/bin/knit to which I make a symbolic link under `~/bin/` so normally I just call `knit foo.Rnw` in the terminal window (will not work for Windows, of course...) – Yihui Xie Jun 08 '12 at 13:16
24

I have a knitme.R script:

library(knitr)
render_html()
source("hooks.R") # mods to defaults
inFile = commandArgs(trailingOnly=TRUE)[1]
outFile = commandArgs(trailingOnly=TRUE)[2]
knit(inFile,output=outFile)

so I can then do

Rscript knitme.R $SOURCE $TARGET

Where $SOURCE and $TARGET are as required.

You could also integrate this into Make, so you had a rule that all you had to do was:

make myfile.html

and it would go to myfile.Rhtml and produce the HTML file. Adjust to make PDF from .Rnw

I'm using it with SCons instead of Make, so I have a Sconscript file which is a bit more complex (partly because I've only just started learning to use SCons, so it might be a bit crufty)

env=Environment()
bld = Builder(action = '/usr/bin/Rscript knitme.R $SOURCE $TARGET',
              suffix='.html',
              src_suffix='Rhtml')
env.Append(BUILDERS = {'Knit' : bld})
env.Knit(source='test.Rhtml',target='test.html')

Then all I need to do is:

scons test.html

and I get test.html built from test.Rhtml if test.Rhtml has changed.

This is all part of a Sconstruct file that builds an entire web site and copies it to a server, based on all sorts of other dependencies..

Drifting off-topic now...

Nikos Alexandris
  • 708
  • 2
  • 22
  • 36
Spacedman
  • 92,590
  • 12
  • 140
  • 224
4

To add on to the other answers, if you want to knit/render the file and open the output all in one line you can use:

Rscript -e "rmarkdown::render('file.Rmd')" & open file.pdf

I prefer to do it all in one line because it's simpler to run as a reusable Vim command.

You can also replace open with a specific application if you want to use your system's non-default. I tend to use this if I'm on Windows and want to use Sumatra to overwrite a PDF output that's currently open (so I don't have to remember to close it every time).

Jacob Amos
  • 996
  • 11
  • 18
2
R CMD knit file.Rmd

is the direct equivalent to R CMD Sweave file.Rmd

Lately, there are enhanced functions in rmarkdown and knitr for this kind of dirty work. For slides, I've been using the Rmarkdown YAML header to designate the intended output format and the command line is basic, like

R -e "library(rmarkdown); render(\"file.Rmd\")"
pauljohn32
  • 2,079
  • 21
  • 28