Print the sourced R file to an appendix using Sweave

后端 未结 4 1773
鱼传尺愫
鱼传尺愫 2021-01-03 12:53

I keep R and Rnw files separate, then load the R data/plots with load(\"file.R\") in the first Sweave chunk. Is there a way that I can print the sourced R file

4条回答
  •  心在旅途
    2021-01-03 13:05

    Separating R and Rnw files sort of defeats the purpose of literate programming. My own approach is to include the code chunks at the appropriate place in the text. If my audience isn't interested in the code, then I might mark it as

    <>=
    x <- 1:10
    @
    

    I might assemble the code in an appendix as

    <>=
    <>
    @
    

    which I admit is a bit of a kludge and error prone (forgotten chunks). One quickly wants to bundle the document with supporting material (data sets, useful helper functions, non-R scripts) into an R package, and these are not difficult to create. Building the package automatically creates the pdf and Stangle'd R file, which is exactly what you want. Package building can be a slow process, but installing the package does not require that the vignettes be rebuilt and so is fast and convenient for whomever you're giving the package to.

    For twiddling with formatting / text, I use a global option \SweaveOpts{eval=FALSE}.

提交回复
热议问题