Thought i'd share the wrapper function i use to knit files using the excellent knitr package in R. This allows easy creation of an html page that integrates commentary, R code, and the resulting plots (which are base64 encoded, so the html file is fully portable).
Thought i'd share the wrapper function i use to knit files using the excellent knitr package in R. This allows easy creation of an html page that integrates commentary, R code, and the resulting plots (which are base64 encoded, so the html file is fully portable). The code is pretty readable and commented, so i won't explain each part. Enjoy!
- # biafra ahanonu
- # updated: 2013.07.13
- # knitr wrapper function
- # ________________________________________________________
- # load libraries
- # ________________________________________________________
- # loop over and process files in the list
- }
- # substitute multiple patterns in a file, create temporary file to process
- }
- }
- #knit files in newDir together, defaults to current directory
- # move to folder with file
- # cd to new directory
- # knit that file!
- # so you can indent R blocks, also suppress warnings/error messages
- # get html/md filenames
- mdFile = flipExt(newRmdFile,'md')
- htmlFile = flipExt(newRmdFile,'html')
- # knit to markdown
- knit(newRmdFile)
- # extra html to add for style (prevent code/images from overflowing horizontal page boundaries)
- extraHtml = '<style>body{width:80%;margin-left:auto;margin-right:auto;}img,pre,code{white-space:pre-wrap;max-width:100%;margin-left:auto;margin-right:auto;}pre{border: 0px solid #ccc;}code.r{border: 1px solid #ccc;}div#toc{-moz-column-count: 4;-webkit-column-count: 4;-moz-column-gap: 1em;-webkit-column-gap: 1em;-moz-column-rule: 1px solid black;-webkit-column-rule: 1px solid black;}li{display:block;}</style>'
- # append extra html to markdown file
- # convert to html
- # remove md and temp Rmd file
- }
- # return to old directory
- }
- # run the script