How to Run Sweave from a Windows Batch File
At a glance
- 1. Prepare the files
Keep the Sweave document, an R script, and the batch file together in one project folder.
- 2. Run Sweave
Have Rscript process the .Rnw file to create the corresponding .tex file.
- 3. Compile the output
Run MiKTeX’s pdflatex on the generated .tex file, checking for errors after each command.
- 4. Fix command paths
Add R and MiKTeX executables to PATH, or call them by their full quoted paths.
A .bat file does not run Sweave by itself. It runs Windows commands, so the usual workflow is to call R to turn the Sweave source (.Rnw) into a LaTeX file (.tex), then call MiKTeX to compile that file into a PDF.
The example below keeps the files together in one folder and checks whether each command succeeded. This makes it easier to identify whether a problem is in the Sweave step or the LaTeX step.
1. Put the project files together
For example, create a folder containing these three files:
C:\projects\sweave-demo\
report.Rnw
build.R
build.bat
A minimal report.Rnw could be:
\documentclass{article}
\usepackage{Sweave}
\begin{document}
The result of the calculation is:
<<>>=
2 + 3
@
\end{document}
The <<>>= and @ lines mark an R code chunk. Sweave evaluates the chunk and writes its output into report.tex.
Create build.R with this line:
utils::Sweave("report.Rnw")
Because the batch file will change the working directory to the project folder, the filename can be given relative to that folder.
2. Create the batch file
Save the following as build.bat in the same folder:
@echo off
setlocal
cd /d "%~dp0"
Rscript.exe build.R
if errorlevel 1 (
echo Sweave failed.
exit /b 1
)
pdflatex -interaction=nonstopmode -halt-on-error report.tex
if errorlevel 1 (
echo LaTeX compilation failed.
exit /b 1
)
pdflatex -interaction=nonstopmode -halt-on-error report.tex
if errorlevel 1 (
echo Second LaTeX pass failed.
exit /b 1
)
echo PDF created: report.pdf
%~dp0 expands to the folder containing the batch file. cd /d changes both the current folder and, if necessary, the drive. This means you can run build.bat from another folder or from a shortcut without relying on the command prompt’s starting location.
The first command runs Sweave. The next commands run MiKTeX’s pdflatex. Running LaTeX twice is useful because references and other cross-references may need a second pass to resolve. For a document without such references, one pass may be enough.
3. Make sure Windows can find R and MiKTeX
The example assumes Rscript.exe and pdflatex.exe are available through the Windows PATH. Open Command Prompt and check with:
where Rscript
where pdflatex
If either command is not found, add the relevant R and MiKTeX bin directories to PATH, or replace the command in the batch file with the full path in quotes. For example, use a path of the form "C:\Program Files\R\R-x.y.z\bin\Rscript.exe" for Rscript, substituting your installed R version and location. Use the actual MiKTeX installation path for pdflatex.exe as well. Quotation marks are necessary when a path contains spaces.
You can run the batch file by opening Command Prompt, changing to the project folder, and entering build.bat. Running it from Command Prompt leaves error messages visible, unlike double-clicking a batch file that may close when it finishes.
Common problems
Rscript.exeis not recognized: R is not onPATH, or its executable is in a different folder. Try its full path or updatePATH.pdflatexis not recognized: MiKTeX’s executable directory is not onPATH. Verify the MiKTeX installation and its command-line tools.- No
report.texis created: Check the R output for an error in the.Rnwfile or an R code chunk. The batch file stops before LaTeX runs if Sweave fails. - LaTeX cannot find
Sweave.sty: The generated document loads this style file. Make sure MiKTeX can locate theSweave.stysupplied with R; the file may need to be made available on MiKTeX’s TeX search path. This is separate from installing R packages or addingRscript.exetoPATH. - The PDF is missing despite a successful Sweave step: The
.texfile is only an intermediate file. Read thepdflatexoutput for the first LaTeX error, and confirm that the batch file reaches the compilation commands.
The same pattern works for other documents: change the .Rnw, .R, and .tex filenames consistently. If your source or project folder has a different name, quote filenames and paths that contain spaces.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.