public
Last active

Interactive reports in R with knitr and RStudio

  • Download Gist
googleVis_with_knitr_and_RStudio.Rmd
RMarkdown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
My first examples with [***knitr***](http://yihui.name/knitr/)
-----------------------------------------
Let's include some simple R code:
```{r}
1+2
```
That worked.
 
Let's include a plot:
```{r fig.width=4, fig.height=4}
df <- data.frame(x=1:10, y=1:10)
plot(y ~ x, data=df, pch=19, col="blue")
```
Nice.
 
Now let's insert an interactive chart with [***googleVis***](http://code.google.com/p/google-motion-charts-with-r):
```{r}
## load the googleVis package
suppressPackageStartupMessages(library(googleVis))
## create the scatter chart
sc <- gvisScatterChart(data=df,
options=list(width=300, height=300,
legend='none',
hAxis="{title:'x'}",
vAxis="{title:'y'}")
 
)
```
The output of *gvisScatterChart* is a complete web page, including \<html\> and \<body\> tags, but here I only need the chart itself which is stored in *sc\$html\$chart*. To include the chart in the output we have to set the *result* parameter for the code chunk to *'asis'*, or otherwise the html code generated by *googleVis* will be displayed as a string.
```{r results='asis'}
## {r results='asis'}
print(sc, 'chart') ## same as cat(sc$html$chart)
```
Hurray, it works! Hover over the dots to get more information about the values. I am sure this could be simplified further in the same way that I can use *ggplot2* and *lattice* plots without the explicit print statement.
 
Next, let's create a geo chart:
```{r results='asis'}
geo <- gvisGeoChart(CityPopularity, locationvar='City',
colorvar='Popularity',
options=list(region='US', height=350,
displayMode='markers',
colorAxis="{colors: ['orange','blue']}") )
print(geo, 'chart')
```
Wonderful!
 
Unfortunately for charts which still rely on Flash, such as *gvisMotionChart, gvisAnnotatedTimeLine* and *gvisGeoMap*, the preview browser of RStudio will not display those charts at the moment. You have to open the output html-file in your system browser. But that is easy, just hit the browser button in the preview window, circled in red below.
 
![RStudio preview window](http://3.bp.blogspot.com/-b7iTQkXybNI/T7KtgqtE0_I/AAAAAAAAARY/Q_ZyViiQBrg/s1600/Untitled.png)
 
Additionally you may have to add your working directory to the trusted location in the [global secturity settings](http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html) of your Flash Player.
 
Here is a little motion chart example:
```{r results='asis'}
M <- gvisMotionChart(Fruits, "Fruit", "Year",
options=list(width=550, height=450))
print(M, 'chart')
```
Conclusion
-----------
 
RStudio and *knitr* might be the way forward to create quick analysis reports.
 
The markdown language should be sufficient for most tasks to draft a report, and the integration with RStudio makes it a real pleasure to work with *knitr*.
 
Unfortunately a spell checker is currently missing for knitr files, but I wouldn't be surprised if this will be added to a future version of RStudio.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.