# Opal Reporting with R

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('opalr', repos=c(getOption('repos'), 'https://cloud.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

![alt text](http://datashield.org/images/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(opalr)
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','mica_demo.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)
```
