Nicholas Hamilton
Nicholas Hamilton

Reputation: 10506

Generate Report in R

Im currently writing a package in R, and i'd like to write a function to generate a standardised pdf report, including text and images, plots and the like.

I have used, for example, knitr many times, but I'd like to generate a report using a single (pre-defined) function, to make things very easy for the end user.

At the moment, I am inclined to write a function which generates the knitr output and dumps it to file, however, this would then require the user to execute the necessary pdflatex commands to turn that generated file into the actual report. Also, this will clutter the working directory with auxiliary files etc...

If someone has or knows of the best way to do this, I welcome any advice.

Upvotes: 4

Views: 2197

Answers (3)

Nicholas Hamilton
Nicholas Hamilton

Reputation: 10506

So the solution I came up with, was to put the .Rmd report file templates in the /inst/rmd/ folder directory of the package, and then write a function like this:

generateReport = function(df,output_format=NULL,output_file=NULL,output_dir="./",...){

  #Determine the template
  theFile = system.file("rmd/report.Rmd", package="MYPACKAGENAME")

  #Process the Arguments
  args               = list(...)
  args$input         = theFile
  args$output_dir    = output_dir
  args$output_format = output_format
  args$output_file   = output_file

  #Run the render
  outputFileName  = do.call('render',args=args)
  invisible(outputFileName)
}

The report itself depends on an object called 'df', which is passed to the above function. More checks can be added, and indeed I have done this in my working version, the above simply illustrates the fundamentals.

Upvotes: 1

greengrass62
greengrass62

Reputation: 986

I tend to put my analysis/output in functions, but I still call them from .Rmd files with the YAML at the top and a couple of chunks. Below would be a bare bones .Rmd file that I'd use to navigate around the other commands ... (my manager seems to like clicking 'Knit Word' or 'Knit HTML' ok enough.)

---
title: "My Title"
output:
  word_document:
    fig_caption: yes
    fig_height: 3.5
    fig_width: 6.5
    reference_docx: template.docx
  html_document:
    fig_caption: yes
    fig_height: 3.5
    fig_width: 6.5
    keep_md: yes
    toc: yes
---


```{r initialize, results='hide'}
library(knitr)
ProjRoot <- 'E:/Dropbox/test'
setwd(ProjRoot)
opts_chunk$set(dpi=150)
```


```{r Analysis, results='hide'}
source('E:/Dropbox/test/myFunction.R')
load('E:/Dropbox/test/5parm.rdata')
myFunction
```

My function (myFunction.R) above will tend to rely heavily on knitr::kable or pander::pandoc.table for tables. I have a series of helper functions, for example, the below function, .H2, prints out a 2nd level header

.H2 <- function(text) {
  pander::pandoc.header(text, 2)
}

So, it makes it pretty painless to push out headers in mark down

Upvotes: 1

Ernest A
Ernest A

Reputation: 7839

I wrote shell script that takes a regular R script as input and outputs a PDF document with the R output, including plots.

It works by creating an intermediate R script with all the necessary boilerplate, using a template such as

#'\documentclass[a4paper,10pt]{printout}
#'\title{@@SCRIPTNAME@@}
#'\begin{document}
setHook(packageEvent("lattice", "attach"),
        function(...) {
            lattice.options(default.theme=standard.theme(color=FALSE))
            trellis.par.set(fontsize=list(text=9, points=6))
        })
opts_chunk$set(dev='pdf', fig.width=6, fig.height=4.5,
               fig.path='./fig/@@SCRIPTSHORTNAME@@-')

@@SCRIPT@@

#'\end{document}

which is then spin()'ed to PDF. I think something similar could be done in R.

Upvotes: 0

Related Questions