Creating markdown reports within a function


New markdown user here with a workflow problem.

I have a whole slew of R functions designed to process a certain class of S4 object. The functions perform various tasks, but essentially they create multiple plots and reports files per call. The reports are created as plain text ASCII files using a combination or sink(), capture.output(), and write.table() calls.
I am interested in converting these pure text reports into markdown-rendered HTML reports, and leveraging the capabilities of kable and kableExtra (note that I am not interested in including the plots in the reports). I have a pretty good idea about how to create HTML files from Rmd files but am not sure how to proceed from within a R function.

Here is an extreme simplification of the problem. Any suggestions (especially links to documentations or tutorials) about how to use R markdown syntax and functions to replace the section of code delimited by sink() calls would be greatly appreciated.

myf <- function(data, settings){

  # some manipulation of data
  n <- length(unique(data[,1]))

  # creation of the reports
  for (i in 1:n){
    sink(sprinf('./junk%d.txt', n))
    cat('Some awesome insight about the meaning of life:\n')
1 Like


The answer to this other post of mine is probably a good start for addressing this issue.