Sweave, include sourced R-Function files

血红的双手。 提交于 2019-12-18 17:12:21

问题


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?


回答1:


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:


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



来源:https://stackoverflow.com/questions/10715916/sweave-include-sourced-r-function-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!