Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sweave, include sourced R-Function files

Tags:

r

latex

sweave

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?

like image 499
user1407220 Avatar asked Jan 17 '23 14:01

user1407220


1 Answers

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.

like image 177
Yihui Xie Avatar answered Jan 19 '23 03:01

Yihui Xie