What's the best way to generate multiple HTML files from RMarkdown based on one dataset?

I have a RMarkdown report that is very helpful and has grown several pages with all the numbers and tables in the HTML file.

It uses the same dataset for all shapes and tables.

What I would like to do is save this big html file and then several new subdirectories, each with its own html files and subdirectories within them, each with their own html files.

In this case, the full report contains the department data, then each subdirectory will contain the html output related to each group in the department, and each one will contain a subdirectory with the html output for each person in each group. Thus, if someone is only interested in the metrics of one group or one person, they look at the most appropriate conclusion.

Parent dir: The same large html file with figures and tables generated with data for entire dept.
|
 __Subdir for each group: Output based on same data but only the group metrics
    |
     __Subdir for each person: Output based on same data but only individual metrics

      

What's the best way to arrange this? 1. Is there a code snippet option in RMardkown where I can tell chunk a goes in this output html file, chuck b goes in another?
2. Do I need multiple RMarkdown files, one for each html output, some kind of caching in between so I don't need to recycle all the data? (this would seem silly because I need a lot of html files)
3. Should I opt out of RMarkdown to complete this task?

+3


source to share


1 answer


I do what you suggest with knitr and it works really well.

Don't tell anyone, but I'm using a 'for' loop to cycle through a bunch of tips, each getting the same report, but with their data. Then I post the report to the directory structure, zip it up and mail it.

I have an Rmd file that expects two datasets, setA (being the subject) and setB (being its peers)

The stream looks something like this:

set <- assemble_data() # loads whole set
for (report in report_list) {
    setA <- filter(set, subject == report)
    setB <- filter(set, subject != report)
    output_html <- str_c('path/',report,'.html')
    knit_interim <- str_c('path/',report,'md')
    knit_pattern <- 'name of RMd' # I generate more than one report for each place
    knit(knit_pattern) 
    markdowntoHTML(file = knit_interim, output=output_html, stylesheet=stylesheet, encoding='windows-1252')
}

      

This way I can create a report in a few minutes. My case may be simpler than yours, because the structure of the report is the same - it is a set of data that changes.



Note that this is not pasting code (it is a little more complicated than this), so beware of typos etc.

The point (as I understand it) is to write an Rmd that expects a dataset of a specific name, and the R code provides a local scope for it. At first I struggled with this, but it was all pretty simple in his performance.

[update: "How to transfer data to RMd files?"

You obviously don't need to. In my code above, RMd is written pending data in setA and setB.

This makes the workflow very simple - you write a template using a dataset (manually filter one), and then when you're ready, you can just start a loop. As I said, I tried to understand at first, but I just jumped in and everything turned out very nicely.

+3


source







All Articles