4
votes

I have an Rjob writen in file a.R which calls functions writen in file b.R, and a snw file c.snw. I call c.snw via "R CMD c.snw".

I am looking for a possibilty to only include a.R via \SweaveInput{a.R} into c.snw, but the code of the functions called from a.R should also be written in the tex file. This implies that Sweave traces all source() commands in the included main R file (here a.R).

Can anyone suggest how to do this?

2

2 Answers

7
votes

I guess life will be easier with the knitr package in this case. You can read a.R as a chunk label-a, and write it in c.Rnw:

<<read-code>>=
read_chunk('a.R', labels = 'label-a')
@
<<label-a>>
@

When you compile c.Rnw with library(knitr); knit('c.Rnw'), a.R will be included into the results (it is equivalent to copy & paste code into the chunk label-a). This is called code externalization in knitr.

\SweaveInput{} is not used to input R source code; you can only input Rnw documents.

2
votes

I'm pretty sure you will get what you want if you use source("b.R", echo = TRUE) in a.R.