---
title: "Opal Files"
author: "Yannick Marcon"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Opal Files}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

[Opal](https://www.obiba.org/pages/products/opal/) has an internal file system that is accessible through web services. The Opal R package exposes files management related functions:

* upload a file or folder,
* download a file or folder (optionaly encrypted),
* move or rename a file or folder,
* create a folder,
* remove a file or folder,
* unzip a zip file into a folder,
* write a file to the remote R session workspace,
* read a file from the remote R session workspace.

Setup the connection with Opal:

```{r eval=FALSE}
library(opalr)
o <- opal.login("administrator", "password", "https://opal-demo.obiba.org")
```

Download a file:

```{r eval=FALSE}
opal.file_download(o, "/home/administrator/CNSIM.zip")
```

Download a file, protected by a password:

```{r eval=FALSE}
opal.file_download(o, "/home/administrator/CNSIM.zip", "CNSIM-encrypted.zip", key="ABCDEFGHIJKL")
```

Upload the file at another location:

```{r eval=FALSE}
opal.file_upload(o, "CNSIM.zip", "/projects/CNSIM")
```

Create a folder and list folder content:

```{r eval=FALSE}
fooDir <- paste0("/projects/CNSIM/foo-", sample(10000:99999, 1))
opal.file_mkdir(o, fooDir)
opal.file_ls(o, "/projects/CNSIM")
```

Move file to the new folder and list folder content:

```{r eval=FALSE}
opal.file_mv(o, "/projects/CNSIM/CNSIM.zip", fooDir)
opal.file_ls(o, fooDir)
```

Rename the new folder and list folder content:

```{r eval=FALSE}
barDir <- paste0("/projects/CNSIM/bar-", sample(10000:99999, 1))
opal.file_mv(o, fooDir, barDir)
opal.file_ls(o, "/projects/CNSIM")
```
Extract the content of the archive file into a folder: 

```{r eval=FALSE}
out <- opal.file_unzip(o, paste0(barDir, "/CNSIM.zip"), barDir)
opal.file_ls(o, out)
```

Write a file from the Opal file system into the R server session workspace:

```{r eval=FALSE}
opal.file_write(o, paste0(barDir, "/CNSIM.zip"))
opal.execute(o, "list.files()")
```

Read back the file into the Opal file system:

```{r eval=FALSE}
opal.file_read(o, "CNSIM.zip", paste0(barDir, "/ds.zip"))
opal.file_ls(o, barDir)
```


Remove created folder and list folder content:

```{r eval=FALSE}
opal.file_rm(o, barDir)
opal.file_ls(o, "/projects/CNSIM")
```

Good practice is to free server resources by sending a logout request:

```{r eval=FALSE}
# clean server side
opal.logout(o)
# clean client side
unlink("CNSIM-encrypted.zip")
unlink("CNSIM.zip")
```

