Title: | Pre-Knitting Processing and Post HTML-Rendering Processing |
---|---|
Description: | Dynamize headers or R code within 'Rmd' files to prevent proliferation of 'Rmd' files for similar reports. Add in external HTML document within 'rmarkdown' rendered HTML doc. |
Authors: | Chin Soon Lim [aut] |
Maintainer: | Chin Soon Lim <[email protected]> |
License: | GPL (>= 2) | file LICENSE |
Version: | 0.1.1 |
Built: | 2025-03-03 03:17:40 UTC |
Source: | https://github.com/chinsoon12/preknitposthtmlrender |
Evaluates code chunks in between ' these code chunks with evaluated output captured using capture.output function. Typically, we want to insert an external HTML file and we can use ' This function will read in the external html file and replace this ' contents in the html file.
post_html_render_proc(inhtml, outhtml = NULL)
post_html_render_proc(inhtml, outhtml = NULL)
inhtml |
- input file for be processed |
outhtml |
- output file name |
Evaluates code chunks which are marked as between adjacent lines of @@s. Replace these code chunks with evaluated output captured using capture.output function.
pre_knit_proc(infile, outfile = NULL)
pre_knit_proc(infile, outfile = NULL)
infile |
- input file for be processed |
outfile |
- output file name |
1) In pre-knitting processing, function takes in a Rmd file, evaluates code chunks which are marked as between adjacent lines of @@@s, and then replace these code chunks with evaluated output captured using capture.output function. 2) Function then calls knitr::knit followed by rmarkdown::render. 3) After which, in post HTML-rendering processing, function evaluates code chunks in between ' captured using capture.output function. Typically, we want to insert an external HTML file and we can use ' This function will read in the external html file and replace this ' contents in the html file.
preknit_knit_render_postrender(pRmdfile, outhtml = NULL)
preknit_knit_render_postrender(pRmdfile, outhtml = NULL)
pRmdfile |
- input Rmd file |
outhtml |
- output html file |
The output html file will have the codes between @@@ and/or \'%%% \' (where \' is a backtick) code chunks evaluated
oldwd <- getwd() setwd(tempdir()) #pandoc.exe is required to run this code samplermd <- tempfile('test', getwd(), '.Rmd') addhtml <- 'test__test.html' #generate the test Rmd file writeLines(c('---', 'title: "Example Usage"', 'output: html_document', '---', '', 'This document is used for various similar reports.', '', '@@@', "cat(paste('#Dynamic Header1', rnorm(1)))", '@@@', '', '`%%% writeLines(readLines("test__test.html"))`', '', '@@@', 'cat(paste("##Dynamic Header2", rnorm(1)))', '@@@', '', 'Some content is invariant across different reports.'), samplermd) #generate test html file writeLines(c('<ul>', '<li>Item 1</li>', '<li>Item 2</li>', '<li>Item 3</li>', '</ul>'), addhtml) #Pre-knit processing and post HTML render processing preknit_knit_render_postrender(samplermd, "sample__html.html") #output 'sample__html.html' is in tempdir() setwd(oldwd)
oldwd <- getwd() setwd(tempdir()) #pandoc.exe is required to run this code samplermd <- tempfile('test', getwd(), '.Rmd') addhtml <- 'test__test.html' #generate the test Rmd file writeLines(c('---', 'title: "Example Usage"', 'output: html_document', '---', '', 'This document is used for various similar reports.', '', '@@@', "cat(paste('#Dynamic Header1', rnorm(1)))", '@@@', '', '`%%% writeLines(readLines("test__test.html"))`', '', '@@@', 'cat(paste("##Dynamic Header2", rnorm(1)))', '@@@', '', 'Some content is invariant across different reports.'), samplermd) #generate test html file writeLines(c('<ul>', '<li>Item 1</li>', '<li>Item 2</li>', '<li>Item 3</li>', '</ul>'), addhtml) #Pre-knit processing and post HTML render processing preknit_knit_render_postrender(samplermd, "sample__html.html") #output 'sample__html.html' is in tempdir() setwd(oldwd)
Run a line of code and capture output into a character string
run_code(oneline, envir)
run_code(oneline, envir)
oneline |
- code line |
envir |
- environment to evaluate the code in |