Permalink
Branch: master
Find file Copy path
9ac385b Mar 6, 2017
1 contributor

Users who have contributed to this file

81 lines (54 sloc) 1.95 KB
---
title: "Opal Reporting with R"
output: html_document
---
This is an example of how to use **knitr** for producing reports on data stored in [Opal](http://wiki.obiba.org/display/OPALDOC).
### Procedure
The requirement on client side is to have **opal** package installed:
```{r eval=FALSE}
install.packages('opal', repos=c(getOption('repos'), 'http://cran.obiba.org'), dependencies=TRUE)
```
The requirements on server side is:
* to have permissions to access to Opal data
* to have permission to send R commands to Opal server
* to have a running R server
![](https://wiki.obiba.org/download/attachments/37749068/opal-implementation.png)
The procedure is then the following:
* connect to Opal server
* assign data from Opal to the associated R server
* send to Opal R commands to be executed in R server
* process in R client the data received from these commands
### Example
Load the required libraries on client side and login in Opal (credentials and output are hidden)...
```{r echo=FALSE,message=FALSE}
library(opal)
o <- opal.login('administrator', 'password', 'https://opal-demo.obiba.org')
```
Assign some variables into a data.frame with associated *D* symbol in R on Opal server side:
```{r}
opal.assign(o,'D','FNAC.FNAC',variables=c('SVUOSI','SUKUP','PITUUS','PAINO'))
```
Preview the assigned data.frame:
```{r}
opal.execute(o,'head(D)')
```
Summary of the assigned data.frame:
```{r}
opal.execute(o,'summary(D)')
```
Histogram figure of the *PITUUS* variable:
```{r}
plot(opal.execute(o,'hist(D$PITUUS)'))
```
Loading *ggplot2* library in R on Opal server side... This will fail if *ggplot2* is not installed in R server environment.
```{r echo=FALSE,results='hide'}
opal.execute(o, 'library(ggplot2)')
```
Plot *PITUUS* vs. *PAINO* with "lm" smoothing:
```{r message=FALSE}
opal.execute(o,'qplot(PITUUS,PAINO, data=D) + geom_smooth(method="lm")')
```
Cleaning the resources on Opal server side...
```{r results=FALSE,echo=FALSE}
opal.logout(o)
```