Knitr with gridSVG

knitr, r

Solution

Update: with knitr >= v1.40, you can ignore the answer below and simply use the chunk option `dev = 'gridSVG'`.

Because `gridSVG::gridsvg()` is not a standard R graphics device, you cannot use the `dev` chunk option in knitr. The only way at the moment is to manually save the plots, and use a chunk hook to write the plots to your output (see the knitr graphics manual). Here is an example:

---
title: Save plots using gridSVG and knitr
author: Yihui Xie
output:
  html_document: default
---

Set up a chunk hook for manually saved plots.
 

{r setup} library(knitr) knit_hooks$set(custom.plot = hook_plot_custom)

 
A single plot.

{r test-a, fig.keep='none', fig.ext='svg', custom.plot=TRUE} library(ggplot2) qplot(speed, dist, data = cars) gridSVG::grid.export(fig_path('.svg'))


Multiple plots.

{r test-b, fig.keep='none', fig.ext='svg', custom.plot=TRUE, fig.num=2, message=FALSE} library(ggplot2) p = qplot(speed, dist, data = cars) p + geom_smooth() gridSVG::grid.export(fig_path('svg', number = 1)) p + geom_jitter() gridSVG::grid.export(fig_path('svg', number = 2))


See the source document of this post at
http://stackoverflow.com/q/23852753/559676

`

See output at http://rpubs.com/yihui/knitr-gridsvg

Problem

Does anybody use gridSVG in Knitr? I found package "gridSVG" provide a device named "gridsvg" and I've written code as follows. ```` ```{r message=FALSE, echo=FALSE, warning=FALSE, results='hide', dev='gridsvg', fig.ext='svg'} analysis.runner.result.plot(result, score.table, info.table) # which produces a gpplot ``` ```` When I click "Knitr HTML", I got: ``` pandoc.exe: Could not find data file AnalysisReport_files/figure-html/unnamed-chunk-61.svg pandoc document conversion failed ``` It seems that the gridsvg didn't produce svg file. I've searched the internet but failed to find any example that uses gridSVG in knitr. How should I modify the code? (P.S. due to the render quality under windows, I don't want to use the default 'svg' device) Thanks!

Original source