[R] Adapt sweave function to produce an automatic pdf.

Marc Schwartz marc_schwartz at me.com
Thu Nov 20 17:00:24 CET 2014

> On Nov 20, 2014, at 4:03 AM, Frederic Ntirenganya <ntfredo at gmail.com> wrote:
> Hi All,
> I want to make a climate method ("sweave_function"). The aim is to be able
> to adapt sweave code that produces an automatic pdf so that it can works
> for my climate object. i.e. instead of compile pdf, I call :
> data_obj$sweave_function() and get the pdf. for instance I have
> boxplot_method() and I want to output it in this way. Thanks for the help!!!
> Ex: This how I started but I don't understand how I can proceed.
> climate$methods(sweave_function = function(climate_data_objs_str,
> climate_data_objs_ind
> ) {
> #---------------------------------------------------------------------------------------------
>  #  This function returns the pdf using sweave function in R
>  #  The required arguments are:
>  #   climate_data_objs_str  : list of the names of climate data objects in
> the climate object
>  #   climate_data_objs_ind  : list of the indices of climate data object
> in the climate object
>  #   note that one of the above arguments is enough.
> #--------------------------------------------------------------------------------------------
>  # get_climate_data_objects returns a list of the climate_data objects
> specified
>  # in the arguements.
>  # If no objects specified then all climate_data objects will be taken by
> default
>  climate_data_objs = get_climate_data_objects(climate_data_objs_str,
> climate_data_objs_ind)
> })

You need to either dynamically generate the entire final .tex file including the preamble and so forth via your function(s)


alternatively use a .Rnw Sweave "master" file template that contains the static content and then in that file, insert the dynamic content using one or more directives along the lines of \input{InsertContentHere.tex}, where InsertContentHere.tex contains raw TeX content to be inserted at that point in the master file when processed by Sweave. 

The content of the *.tex files can be generated via various R functions including ?cat for raw text and others like the xtable() function in the CRAN package of the same name or the latex() function in Hmisc, which can be used to create tables, etc.

Note that the content of InsertContentHere.tex will not itself be processed by Sweave, as if it was a "child" .Rnw file. If you want that type of functionality, you would need to use \SweaveInput{InsertContentHere.Rnw}.

Once you have your final .tex file, you can then run pdflatex on that file via ?system.


Marc Schwartz

More information about the R-help mailing list