What is the knitr equivalent of `R CMD Sweave myfile.rnw`?

RSweaveKnitr

R Problem Overview


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

R Solutions


Solution 1 - R

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"

Solution 2 - R

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...

Solution 3 - R

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).

Solution 4 - R

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\")"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeromy AnglimView Question on Stackoverflow
Solution 1 - RMaiasauraView Answer on Stackoverflow
Solution 2 - RSpacedmanView Answer on Stackoverflow
Solution 3 - RJacob AmosView Answer on Stackoverflow
Solution 4 - Rpauljohn32View Answer on Stackoverflow