Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitr with gridSVG

Tags:

r

knitr

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!

like image 473
Nathaniel Avatar asked May 25 '14 06:05

Nathaniel


People also ask

How to create external graphics with knitr?

External Graphics with knitr 1 Sizing your image. Let’s suppose we want to include a (pre-covid) picture of the Jumping Rivers team. ... 2 Including images in an R markdown File. The crucial thing to note is that we if we size the width & height smaller than the actual image, then the browser ... 3 The Retina Setting. ... 4 Resizing your image. ...

What is knitr?

knitr is a way to write LaTeX, HTML, and Markdown with R code interlaced. knitr seamlessly adds code, charts, and anything else that R can do straight into the document. This tutorial explains the basics of it. A Beginner's Tutorial for knitr - Josh Davis Josh Davis Posts About Contact 12 April 2014 A Beginner’s Tutorial for knitr

When outputting tables in knitr what option should I use?

When outputting tables in knitr, it is important to use the option results = 'asis'. There are several options for formatting tables in R. The knitr package includes a function called kable that makes basic k nitr t ables.

How does knitr work in R?

How to use knitr The basic idea is that your text documents are interrupted by chunks of code that are identified in a special way. These chunks are evaluated by an R process in the order that they appear. R objects that are created by chunks remain in the enviroment for subsequent chunks.


1 Answers

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

like image 160
Yihui Xie Avatar answered Oct 25 '22 13:10

Yihui Xie