---
title: "IDE Container: RStudio Server, Shiny Server, and More"
author: "Tengfei Yin <<tengfei.yin@sevenbridges.com>>"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 4
    number_sections: true
    css: sevenbridges.css
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{IDE Container: RStudio Server, Shiny Server, and More}
---

# Introduction

The goal of this [sevenbridges/sevenbridges-r](https://hub.docker.com/r/sevenbridges/sevenbridges-r/) Docker image is to provide

- Seven Bridges SDK environment that includes
    - Seven Bridges Platform command line uploader
    - Seven Bridges command line tools
    - R package sevenbridges with essential dependencies
- RStudio Server
- Shiny Server

# Docker container

## Build container locally

The `Dockerfile` is included with the package in `inst/docker` folder.

Here is the current content of `Dockerfile`:
```{r comment = ''}
fl = system.file("docker/sevenbridges/", "Dockerfile", package = "sevenbridges")
cat(readLines(fl), sep = "\n")
```

You can simply build it locally, enter folder which contain that `Dockerfile`, in this case,
it is under `inst/docker/sevenbridges`

```bash
docker build -t sevenbridges/sevenbridges-r .
```

## Pull from Docker Hub

A hook is added to build the Docker image automatically from the [sevenbridges-r GitHub repository](https://github.com/sbg/sevenbridges-r/tree/master/inst/docker/sevenbridges). It is automatically built on [Docker Hub](https://hub.docker.com/r/sevenbridges/sevenbridges-r/). You can directly use this image `sevenbridges/sevenbridges-r`. Everytime a push is made in the GitHub repo, the Docker container is re-built.

# Launch RStudio Server from Docker container

For example, you can ssh into your AWS instance, here I suppose you already have Docker installed, and pull the image

```bash
docker pull sevenbridges/sevenbridges-r
```

To launch sevenbridges RStudio Server image, I recommend you read this [tutorial](https://github.com/rocker-org/rocker/wiki/Using-the-RStudio-image)

Or following the quick instruction here

```bash
docker run -d -p 8787:8787 sevenbridges/sevenbridges-r
docker run -d -p 8787:8787 -e USER=<username> -e PASSWORD=<password> rocker/rstudio
```

You will be able to access the RStudio in the browser by something like

`http://<your_ip_address>:8787`

Sometimes you want to add more users, to add users

```bash
## Enter the container
docker exec -it <container-id> bash
## Interactively input password and everything else
adduser <username>
```

## Launch both RStudio Server and Shiny Server from the same Docker container

Sometimes it is very conventient to launch both RStudio Server and Shiny Server from a singel container and your users can manage to using RStudio Server and publish Shiny apps at the same time in the same container. To do so, just pull the same image and launch them at different port.

```bash
docker run  -d -p 8787:8787 -p 3838:3838 --name rstudio_shiny_server sevenbridges/sevenbridges-r
```

To mount file system you need to use `--privileged` with fuse.

```bash
docker run  --privileged -d -p 8787:8787 -p 3838:3838 --name rstudio_shiny_server sevenbridges/sevenbridges-r
```

check out the ip from docker machine if you are on mac os.

```bash
docker-machine ip default
```

In your browser, `http://<url>:8787/` for RStudio Server, for example, if 192.168.99.100 is what returned, visit `http://192.168.99.100:8787/` for RStudio Server.


For Shiny Server, __per user app__ is hosted `http://<url>:3838/users/<username of rstudio>/<app_dir>`, for example, for user `rstudio` (a default user) and an app called `01_hello`, it will be `http://<url>:3838/users/rstudio/01_hello/`. To develop your Shiny app from RStudio Server, you can log into your RStudio Server with your username, and create a fold at home folder called `~/ShinyApps` and develop Shiny apps under that folder, for example, you can create an app called `02_text` at `~/ShinyApps/02_text/`.

Let's try this, please log into your RStudio at `http://<url>:8787` now, then try to copy some example over to your home folder under `~/ShinyApps/`

```r
dir.create("~/ShinyApps")
file.copy("/usr/local/lib/R/site-library/shiny/examples/01_hello/",
          "~/ShinyApps/", recursive = TRUE)
```

If you logged in with the username `rstudio`, then visit `http://192.168.99.100:3838/rstudio/01_hello` you should be able to see the hello example.

Note: Generic Shiny apps can also be hosted `http://<url>:3838/` or for particular app, `http://<url>:3838/<app_dir>` and inside the Docker container, it is hosted under `/srv/shiny-server/`.
