I really enjoy using R. Since I plan to write about statistics, and especially statistical computing, I imagine many of my posts will contain R code. R comes with an interesting function called Sweave. Sweave allows you to incorporate R commands within a document you are writing wrapped in a special syntax, see some demos. So you might be writing some or HTML and interweaving R code. After you run the Sweave function using the source (e.g., HTML) document as the input, a new file is created that replaces the R code with the results of the commands. This might sound simple, but it leads to a very powerful model for report generation and reproducible research. My goal was to somehow get publishable HTML from a source org-mode file after running Sweave on it. For example, say I want to generate 100 samples from a normal distribution with mean 10, and summarize the results.
> x <- rnorm(100, mean = 10, sd = 5)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-3.077 6.844 10.100 9.819 13.690 22.300
I do not want to have to actually type any of the summary output, of course, but I also don’t have to even copy and paste it. What I can type is the R code to produce the output, and wrap it in Sweave syntax, like this:
x <- rnorm(100, mean = 10, sd = 5)
When I prepare this post for publishing, Sweave will run the R code and insert the output in place of the Sweave commands. I have wrapped the Sweave syntax in org-mode’s special BEGIN SRC construct, so that when I export to HTML, org-mode will properly syntax highlight the output using the htmlize package, as inspired by this post on using the htmlize package with Erlang. The only elisp I had to write was the following.
;; export an org file into WordPress-ready HTML
(defun run-ascii-sweave-on-buffer ()
(ess-command (concat "Sweave(\"" (buffer-file-name)
(defun sweave-and-htmlize-blog-entry ()
"Run Sweave on current file and produce HTML
ready for pasting to WordPress. Copies text to kill-ring for
(let* ((name (buffer-file-name))
(txt-filename (concat name ".txt"))
(txt-buf (find-file txt-filename)))
(message "Preparing HTML for '%s' ..." name)
(revert-buffer t t t)
(while (re-search-forward "BEGIN_SRC R" nil t)
(replace-match "BEGIN_SRC R-transcript" t nil))
(while (re-search-forward "^----" nil t)
(replace-match "" nil nil))
(point-min) (point-max) t "*HTML*"))
(while (re-search-forward "<pre " nil t)
"<pre style=\"background-color:#FFFFE5; font-size:8pt\" "
(kill-ring-save (point-min) (point-max))
(message "Finished Converting to WordPress HTML" ))))
(define-key global-map (kbd "<f5>") 'sweave-and-htmlize-blog-entry)
All the first function is doing is running an input file (an org-mode file) through Sweave with the RweaveAsciidoc driver found in the R ascii package. It basically just puts in the R output as plain text, as opposed to code or HTML.
The rest of the elisp function manipulates the output in some trivial ways. The explanation for the replacement of the four dashes is that the RweaveAsciidoc Sweave driver produces the dashed string both before and after the R output as a visual offset, since asciidoc uses that as a markup indicator. I did not need that, since my output will not be fed through asciidoc, but rather the org-mode HTML exporter, so I simply replace the dashes found at the beginning of a line with the empty string. The last trick I had to do was to replace the R syntax highlighting with R-transcript syntax highlighting, since the results of Sweave are essentially a transcript of the R commands entered, not the actual R code. Finally, the resulting HTML produced by the org exporter has its pre tags modified with custom colors and font size.