Created
May 15, 2012 19:59
-
-
Save mages/2704646 to your computer and use it in GitHub Desktop.
Interactive reports in R with knitr and RStudio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment