<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/shiny-application-in-production-with-shinyproxy-docker-and-debian/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rtask.thinkr.fr/blog/shiny-application-in-production-with-shinyproxy-docker-and-debian/"> (en) The R Task Force</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>You created some great Shiny applications, following our advice of <a rel="nofollow" href="https://rtask.thinkr.fr/blog/our-shiny-template-to-design-a-prod-ready-app/" target="_blank">Shiny packaging</a> for example, and you want to put them into production, self-hosting, so that others can enjoy them, without limitations, on the Internet or on an internal server of your company? ShinyProxy is for you!</strong></p>
<p>ShinyProxy v2.0 has recently been released. What a great opportunity to talk about its implementation!<br>
A major interest of ShinyProxy is its ability to create an independent Docker container for each user who connects to your Shiny application. This overrides the limitations on the number of users faced with ShinyServer. Are you interested? Follow the installation procedure….</p>
<p><img src="https://i2.wp.com/rtask.thinkr.fr/wp-content/uploads/shinyproxy-docker-shiny_mini.png?w=150&ssl=1" data-recalc-dims="1"></p>
<h2>ShinyProxy installation</h2>
<p><img width="71" src="https://i2.wp.com/rtask.thinkr.fr/wp-content/uploads/debian.png?resize=71%2C88&ssl=1" data-recalc-dims="1" height="88"></p>
<p>For our installation, we chose to use a <em>Debian Jessie</em> server. We let you make the necessary code adjustments for other Linux distributions. (For example, for Ubuntu, replace ‘debian’ with ‘ubuntu’ in the appropriate codes). In addition, there are two ways to deploy ShinyProxy, either using JAVA or using a Docker container. You will be able to choose either one or the other after this article but in both cases you will have to install Docker on your server!</p>
<h2>ShinyProxy and Java</h2>
<h3>First step: install Java</h3>
<p><img width="104" src="https://i2.wp.com/rtask.thinkr.fr/wp-content/uploads/java.jpeg?resize=104%2C104&ssl=1" data-recalc-dims="1" height="104"></p>
<p>ShinyProxy is a software that works with Java. You need to check if Java is installed and if so, which version. In your terminal, enter the command: `<code>sh Java -version</code></p>
<p>If you receive a message like this:</p>
<pre><code>openjdk version &quot;1.8.0_xxx&quot;</code></pre>
<p>Then Java 8 is already installed on your server. Otherwise, you can install either (1) the free version:</p>
<pre><code>sudo apt install default-jre default-jdk</code></pre>
<p>or (2) the Java8 version of Oracle with webupd8team’s PPA:</p>
<pre><code>echo &quot;deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main&quot; | \
  sudo tee /etc/apt/sources.list.d/webupd8team-java.list
echo &quot;deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main&quot; | \
  sudo tee -a /etc/apt/sources.list.d/webupd8team-java.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
sudo apt-get install oracle-java8-installer</code></pre>
<p>We check if the installation worked well:</p>
<pre><code>java -version</code></pre>
<pre><code>openjdk version &quot;1.8.0_xxx&quot;</code></pre>
<p>Perfect, then we can continue our installation.</p>
<h3>Second step: Install Docker</h3>
<p><img width="159" src="https://i2.wp.com/rtask.thinkr.fr/wp-content/uploads/docker.png?resize=159%2C90&ssl=1" data-recalc-dims="1" height="90"></p>
<p>As explained above, ShinyProxy allows you to launch a Docker container from our Shiny application. So we will install Docker CE on our server.</p>
<p>There is a need for some dependencies. It is also better to install the latest version of Docker CE using Docker servers as a third-party repository.</p>
<p>In your terminal:</p>
<pre><code>sudo apt-get install \
     apt-transport-https \
     ca-certificates \
     curl \
     gnupg2 \
     software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository \
   &quot;deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable&quot;
</code></pre>
<p>sudo apt-get install docker docker-ce</p>
<p>To find out if everything went well, you can run a little test to ask Docker to say hello:</p>
<pre><code>docker run hello-world</code></pre>
<p>If you get a message like this: <img src="https://i1.wp.com/rtask.thinkr.fr/wp-content/uploads/docker_run.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>Then Docker is installed.</p>
<p>One last small modification….<br>
For ShinyProxy to work, you need to modify the Docker file <code>/lib/systemd/systemd/system/docker.service</code> and restart Docker (use your favorite text editor):</p>
<pre><code>sudo nano /lib/systemd/system/docker.service</code></pre>
<p>Modify the line with ‘ExecStart’.</p>
<pre><code>ExecStart=/usr/bin/dockerd -H fd:// -D -H tcp://127.0.0.1:2375</code></pre>
<p>Then restart Docker</p>
<pre><code>sudo systemctl daemon-reload
sudo systemctl restart docker</code></pre>
<h3>Third step: Install ShinyProxy</h3>
<p><img width="74" src="https://i1.wp.com/rtask.thinkr.fr/wp-content/uploads/shinyproxy.png?resize=74%2C74&ssl=1" data-recalc-dims="1" height="74"></p>
<p>It’s time to go back to what really interests us, the installation of ShinyProxy to <strong>deploy our Shiny applications</strong>!</p>
<p>First, you need to clone the GitHub repository of the ShinyProxy software (you can also download the .zip from github):</p>
<pre><code>git clone https://github.com/openanalytics/ShinyProxy.git</code></pre>
<p>We should find a “ShinyProxy” folder. We get into the folder and install ShinyProxy. You may first need to install “maven”:</p>
<pre><code>cd ShinyProxy/
sudo apt install maven
mvn -</code></pre>
<p>Then, we can test if the examples can be run. To do this, you need the ShinyProxy template and the Docker images of the Shiny applications proposed in the demo.</p>
<pre><code>mkdir app-shiny
cd app-shiny/
git clone https://github.com/openanalytics/ShinyProxy-template.git
cd ShinyProxy-template/
docker build -t openanalytics/ShinyProxy-template .</code></pre>
<p>Then, you launch ShinyProxy: <em>You have to go up in the tree structure to find the folder “ShinyProxy/target”.</em><br>
<em>Change this command with the version of ShinyProxy you have. </em></p>
<pre><code>cd ShinyProxy/target/
java -jar ShinyProxy-2.0.2.jar</code></pre>
<p>Go to <code>https://localhost:8080</code>. You should see this page: <img src="https://i0.wp.com/rtask.thinkr.fr/wp-content/uploads/page_shinyproxy.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>To be able to customize your ShinyProxy (deploy your applications, change the ShinyProxy home page), you have to modify the <code>application.yml</code> file and the different <code>.html</code> contained in the different sub-folders of <code>shinproxy/</code>. For example, we can modify the configuration file <code>application.yml</code> (this file allows us to define the applications to be deployed and other ShinyProxy configurations):</p>
<pre><code>cd ../src/main/ressources/
nano application.yml</code></pre>
<p>Let’s remove a demo application for example. For that you just have to delete the following lines:</p>
<pre><code>  - id: 06_tabsets
    container-cmd: [&quot;R&quot;, &quot;-e&quot;, &quot;ShinyProxy::run_06_tabsets()&quot;]
    container-image: openanalytics/ShinyProxy-demo
    container-network: sp-example-net</code></pre>
<pre><code>cd ../../../
mvn -U clean install</code></pre>
<p>Then, restart with the <code>.jar</code>:</p>
<pre><code>cd target/
Java -jar ShinyProxy-2.0.2.jar</code></pre>
<p>But all these steps take a long time. Following each change, it is necessary to recompile, restart,… The round trips are therefore frequent and a waste of our time. Besides, here it doesn’t run in the background, the terminal needs to be up (although you can use <code>nohup</code>). This leads us to use the “ShinyProxy in container” version.</p>
<h2>ShinyProxy in Docker container</h2>
<p><img src="https://i2.wp.com/rtask.thinkr.fr/wp-content/uploads/shinyproxy-docker-shiny_mini.png?w=150&ssl=1" data-recalc-dims="1"></p>
<p>Do not forget to install docker too to follow this part (cf. Second step: Install Docker)!</p>
<p>To make our lives easier, there are ShinyProxy templates that can be easily deployed with Docker. You will find an example <a rel="nofollow" href="https://github.com/openanalytics/ShinyProxy-config-examples/tree/master/02-containerized-docker-engine" target="_blank">here</a>!</p>
<p>First, it is necessary to clone the GitHub repository:</p>
<pre><code>mkdir ShinyProxy-docker
cd ShinyProxy-docker/
git clone https://github.com/openanalytics/ShinyProxy-config-examples.git</code></pre>
<p>We get four files. For this example, we will use the folder starting with <code>02</code>. It contains two important files: (1) the <code>application.yml</code> file that allows the configuration of our ShinyProxy and (2) the <code>dockerfile</code>, which allows the ShinyProxy image to be mounted.</p>
<h3>Application.yml</h3>
<p>We will detail the important code lines of this file. We will also be able to discuss the differences between versions 1.1 and 2.0 of ShinyProxy because the writing of the `.yml’ has changed.</p>
<p>Changes between versions are in the part dedicated to Shiny applications. <code>docker</code> is replaced by <code>container</code>, <code>name</code> by <code>id</code> and <code>groups</code> by <code>access-groups</code>.</p>
<p>In ShinyProxy version 1.1, we had:</p>
<pre><code>  apps:
  - name: 01_hello
    display-name: Hello Application
    description: Application which demonstrates the basics of a Shiny app
    docker-cmd: [&quot;R&quot;, &quot;-e&quot;, &quot;ShinyProxy::run_01_hello()&quot;]
    docker-image: openanalytics/ShinyProxy-demo
    groups: [scientists, mathematicians]
    </code></pre>
<p>And in version 2.0, we have:</p>
<pre><code>  specs:
  - id: 01_hello
    display-name: Hello Application
    description: Application which demonstrates the basics of a Shiny app
    container-cmd: [&quot;R&quot;, &quot;-e&quot;, &quot;ShinyProxy::run_01_hello()&quot;]
    container-image: openanalytics/ShinyProxy-demo
    container-network: sp-example-net
</code></pre>
<p>Here is the example of the <code>.yml</code> proposed in the <code>02</code> folder that we downloaded earlier:</p>
<pre><code>proxy:  
  port: 8080
  authentication: simple
  admin-groups: admins
  users:
  - name: jack
    password: password
    groups: admins
  - name: jeff
    password: password
  docker:
      internal-networking: true
  specs:
  - id: 01_hello
    display-name: Hello Application
    description: Application which demonstrates the basics of a Shiny app
    container-cmd: [&quot;R&quot;, &quot;-e&quot;, &quot;ShinyProxy::run_01_hello()&quot;]
    container-image: openanalytics/ShinyProxy-demo
    container-network: sp-example-net
  - id: 06_tabsets
    container-cmd: [&quot;R&quot;, &quot;-e&quot;, &quot;ShinyProxy::run_06_tabsets()&quot;]
    container-image: openanalytics/ShinyProxy-demo
    container-network: sp-example-net
logging:
  file:
    ShinyProxy.log</code></pre>
<p>In this file, <code>port</code> indicates that the container will launch on port <code>8080</code>, <code>authentication</code> gives us the type of identification chosen, in our case it is “simple”. In other words, we create <code>users</code> which can be long (there are many others). Then we find the <code>specs</code> part for the different Shiny applications.</p>
<p>A brief focus on interesting options:</p>
<ul>
<li><code>heartbeat-timeout: 60000</code> allows you to configure how long (in milliseconds) the container remains active if it is not used. After that, he is stopped.</li>
<li><code>hide-navbar : true</code> hides the navigation bar.</li>
</ul>
<p>There are many more options that you can find <a rel="nofollow" href="https://www.shinyproxy.io/configuration/" target="_blank">here</a>.</p>
<h3>The Dockerfile</h3>
<p>The Dockerfile allows us to build our Docker image according to what we are interested in. In the <code>02</code> folder, it allows you to directly change the <code>.yml</code> but you might also want to change the home page for example (<code>04</code> folder for the most curious).</p>
<h3>The installation of ShinyProxy</h3>
<p>It is now time to practice.</p>
<p>First, we need to create a network so that ShinyProxy can communicate with the containers of the Shiny apps. Let’s name this network <code>sp-example-net</code>:</p>
<pre><code>sudo docker network create sp-example-net</code></pre>
<p>Then, we have to create our ShinyProxy image: <em>You need to make sure you’re inside the right folder (in 02-containerized-docker-engine)!</em></p>
<pre><code>cd 02-containerized-docker-engine
docker build -t mon_ShinyProxy .</code></pre>
<p>Finally, let’s run our container:</p>
<pre><code>sudo docker run -d -v /var/run/docker.sock:/var/run/docker.sock --net sp-example-net -p 8080:8080 mon_ShinyProxy</code></pre>
<p>We save a lot of time compared to the off-Docker version. In two command lines, you can restart your ShinyProxy if you made changes to the <code>.yml</code>.</p>
<p>And there are many other possibilities with ShinyProxy and Docker.</p>
<p>That’s it ! You have all the information you need to put your Shiny application into production! Don’t forget to turn your application into a package! If this is not the case, you can read our article <a rel="nofollow" href="https://rtask.thinkr.fr/blog/our-shiny-template-to-design-a-prod-ready-app/" target="_blank">on designing a shiny template in an R package</a>. And if you don’t make it, you can always <a rel="nofollow" href="https://rtask.thinkr.fr/contact/" target="_blank">ask us</a> !</p>
<p>The post <a rel="nofollow" href="https://rtask.thinkr.fr/blog/shiny-application-in-production-with-shinyproxy-docker-and-debian/" target="_blank">Shiny application in production with ShinyProxy, Docker and Debian</a> appeared first on <a rel="nofollow" href="https://rtask.thinkr.fr/" target="_blank">(en) The R Task Force</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rtask.thinkr.fr/blog/shiny-application-in-production-with-shinyproxy-docker-and-debian/"> (en) The R Task Force</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/qJfZd-tEP-8" height="1"></a></div>