[R] How to call patchDVI::SweavePDF on an .Rnw which is not the master file?

Marius Hofert marius.hofert at math.ethz.ch
Thu Aug 16 16:14:13 CEST 2012


Dear expeRts,

I have a master file master.tex containing the preamble and which inputs (via
\input{chapter01}, \input{chapter02}, ...) chapters. The chapters are .Rnw
files. My goal is to use patchDVI::SweavePDF to compile the chapters (say,
chapter.Rnw) individually (each chapter starts with sourcing Sweave preliminary
settings).

The first problem is the following. If I execute (in the shell)

,----
| Rscript -e "patchDVI::SweavePDF('chapter.Rnw')"
`----

I obtain

,----
| Loading required package: grDevices
| Error: ‘chapter.Rnw’ is not ASCII and does not declare an encoding
| Execution halted
`----

Normally one uses \usepackage[utf8]{inputenc} to solve this problem. However, I
can't put this into chapter.Rnw since it is only allowed in the preamble, so in
master.tex. But there it has no effect. The question is how to solve this. By
looking at 'standard Sweave', I found the call

,----
| R CMD Sweave --encoding=utf-8 chapter.Rnw
`----

which works fine. But I don't know how to pass the --encoding=utf-8 option to
Rscript or how to use R CMD ... to solve the problem.

The second (follow-up) problem should then be simple: How to call that from
Emacs. Ideally, something like the following should work, but one somehow needs
too many ", ', ... to create the string "Rscript ...":

,----
| (add-hook 'Rnw-mode-hook
| 	  (lambda ()
| 	    (add-to-list 'TeX-command-list
| 			 '("SweavePDF" "Rscript -e 'patchDVI::SweavePDF('%s', and-other-options)"   
| 			   TeX-run-command nil t :help "Run SweavePDF") t); TODO: not working yet
| 	    (add-to-list 'TeX-command-list
| 			 '("Sweave" "R CMD Sweave --encoding=utf-8 %s"
| 			   TeX-run-command nil t :help "Run Sweave") t)
| 	    (add-to-list 'TeX-command-list
| 			 '("Stangle" "R CMD Stangle --encoding=utf-8 %s"
| 			   TeX-run-command nil t :help "Run Stangle") t)
| 	    (add-to-list 'TeX-command-list
| 			 '("make" "make" TeX-run-command nil t :help "Run make") t)
| 	    (setq TeX-command-default "SweavePDF")))
`----

If anybody already has such a setup, I'd be happy to know about it.

Cheers,

Marius



More information about the R-help mailing list