0% found this document useful (0 votes)
96 views

Shiny Application - From Package Development To Server Deployment

The document discusses developing and deploying Shiny applications from package development through server deployment. It begins by outlining challenges in maintaining large Shiny apps and collaborating on apps. It then provides a template for structuring Shiny apps within an R package to help address these challenges. The template includes files for modules, UI, server logic, and running the app. It also discusses deploying Shiny apps using Docker and Shinyproxy for easy deployment to a server.

Uploaded by

nexroth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Shiny Application - From Package Development To Server Deployment

The document discusses developing and deploying Shiny applications from package development through server deployment. It begins by outlining challenges in maintaining large Shiny apps and collaborating on apps. It then provides a template for structuring Shiny apps within an R package to help address these challenges. The template includes files for modules, UI, server logic, and running the app. It also discusses deploying Shiny apps using Docker and Shinyproxy for easy deployment to a server.

Uploaded by

nexroth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Shiny Application

from package development to server


deployment
Cervan Girard
28 octobre 2018

Cervan Girard - satRday 2018 - https://thinkr.fr    1 / 34


ThinkR

Cervan Girard - satRday 2018 - https://thinkr.fr    2 / 34


Context
Maintain your app (more than one thousand of lines for ui.R and server.R)

Working together on the same app (more than 4 or 5 developer)

Not really easy without git

Back and forth to see the changes in the application

Coding the application too quickly

How to deploy your application ?

Cervan Girard - satRday 2018 - https://thinkr.fr    3 / 34


Download the package and install it.
Find the package ThinkR-open/shinytemplate

After that, create a new shiny app with this template :

Cervan Girard - satRday 2018 - https://thinkr.fr    4 / 34


What can be found inside this package

Cervan Girard - satRday 2018 - https://thinkr.fr    5 / 34


devstu _history.R
Why do we use devstuff_hisory.R?

So we have an history of development code

Example:

usethis::use_build_ignore("devstuff_history.R")
# Dependencies
usethis::use_package("shiny")
usethis::use_package("DT")
usethis::use_package("stats")
usethis::use_package("graphics")
usethis::use_package("glue")
# For data
usethis::use_data_raw()
# If you want to use the MIT licence, code of conduct, lifecycle badge,
and README
usethis::use_mit_license(name = "ThinkR")

Cervan Girard - satRday 2018 - https://thinkr.fr    6 / 34


In the R folder

Files: app_prod file:

if(app_prod()){
cat("Hey! this is prod mode")
}else{
cat("Hey, this is dev mode :)
!")
}

app_prod
mod_first (we use modules)
app_server and app_ui
run_app

Cervan Girard - satRday 2018 - https://thinkr.fr    7 / 34


Always use shinymodules
Motivations:

Easier to maintain your app, to organize your project, and to call your code in another
app.

UI Server

mod_firstUI <- function(id){ mod_first <- function(input,


ns <- NS(id) output , session, r){

fluidPage( my_data <- reactive({


h3("Choose your data"), switch(input$data,
fluidRow( "iris" = iris,
div( "mtcars" = mtcars)
selectInput(ns("data"), })
label = "" , choices =
c("iris","mtcars")) output$my_summary <-
) renderPrint({
) summary(my_data())
) })
} } Cervan Girard - satRday 2018 - https://thinkr.fr    8 / 34
In the R folder

app_server app_ui()

app_server <- function(input, app_ui <- function() {


output,session) {
fluidPage(
r <- reactiveValues() div(
h2("My application", style =
if(app_prod()){ "text-align:center"),
cat("Hey! this is prod mode") tabsetPanel( id = "my_panel",
}else{ tabPanel("data",
cat("Hey, this is dev mode :) mod_firstUI(id =
!") "first")),
} style = "text-align:center")
)
callModule(mod_first,"first",r
= r) )
} }

Cervan Girard - satRday 2018 - https://thinkr.fr    9 / 34


In the R folder

run_app.R

run_app <- function() {


shinyApp(ui = app_ui(), server = app_server)
}

Cervan Girard - satRday 2018 - https://thinkr.fr    10 / 34


In the inst folder

-app
-server.R
-ui.R

You don't need to modify them

ui.R:

belgradeapp:::app_ui()

server.R:

belgradeapp:::app_server

Cervan Girard - satRday 2018 - https://thinkr.fr    11 / 34


In the inst folder
We also find a run_dev folder.

run_dev.R:

# This script allow you to quick clean your R session


# update documentation and NAMESPACE, localy install the package
# and run the main shinyapp from 'inst/app'
.rs.api.documentSaveAll() # close and save all open file
try(suppressWarnings(lapply(paste("package:",
names(sessionInfo()$otherPkgs), sep = ""),
detach, character.only = TRUE, unload =
TRUE)), silent = TRUE)
rm(list=ls(all.names = TRUE))
devtools::document('.')
devtools::load_all('.')

options(app.prod=FALSE) # TRUE = production mode, FALSE = development mode


shiny::runApp('inst/app')

Cervan Girard - satRday 2018 - https://thinkr.fr    12 / 34


Working process at ThinkR
First phase: UI validation

Second phase: vignettes


Third phase: module coding

Cervan Girard - satRday 2018 - https://thinkr.fr    13 / 34


First phase : UI validation
Why this first phase?

Visual validation
Allows a better understanding of the customer's request
Better overall vision of the project

Cervan Girard - satRday 2018 - https://thinkr.fr    14 / 34


Second phase: Vignettes
Don't code your shiny app too fast!

Easy to develop a Rmd document with an example


Propose several visual renderings with advantages and disadvantages

Very useful for graphical rendering and expected tables

Cervan Girard - satRday 2018 - https://thinkr.fr    15 / 34


Third phase : module coding
Easier thanks to first and second phase
With git and modules, it's easy to work separately to avoid conflicts

When you code your module, use run_dev.R

It saves time
No need to install your package each time

App with "Next button" :

Cervan Girard - satRday 2018 - https://thinkr.fr    16 / 34


App with "next button"
First page:

Cervan Girard - satRday 2018 - https://thinkr.fr    17 / 34


App with "next button"
Second page:

Cervan Girard - satRday 2018 - https://thinkr.fr    18 / 34


App with "next button"
Third page:

Cervan Girard - satRday 2018 - https://thinkr.fr    19 / 34


Example for the third module:

.rs.api.documentSaveAll()
suppressWarnings(lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),

rm(list=ls(all.names = TRUE))
devtools::document('.')
devtools::load_all('.')
options(app.prod=FALSE)
library(shiny)
library(DT)
if (interactive()){
ui <- fluidPage(
mod_thirdUI("test")
)
server <- function(input, output, session) {
r <- reactiveValues(x = "Sepal.Width",y = "Petal.Width", data = iris)
callModule(mod_third,"test", r = r)
}
shinyApp(ui, server)
}

Cervan Girard - satRday 2018 - https://thinkr.fr    20 / 34


Cervan Girard - satRday 2018 - https://thinkr.fr    21 / 34
Deploy your application
Shinyproxy

Cervan Girard - satRday 2018 - https://thinkr.fr    22 / 34


Shinyproxy

Docker
First thing to do: install Docker!

What is docker?

Why we use shinyproxy:

Allows to launch one container per


person
Just needs Docker
All docker advantages

Cervan Girard - satRday 2018 - https://thinkr.fr    23 / 34


Build our Shinyapp image
We need a dockerfile:

FROM rocker/r-ver:3.4
RUN apt-get update
RUN apt-get install -y libssl-dev libssh2-1-dev libcurl4-openssl-dev

RUN R -e "install.packages('DT')"
RUN R -e "install.packages('pacman')"
RUN R -e "pacman::p_load(shiny, stats, dplyr, tidyr, magrittr,
ggplot2)"

COPY belgradeapp*.tar.gz /belgradeapp.tar.gz

RUN R -e "install.packages('belgradeapp.tar.gz', repos = NULL, type =


'source')"

COPY Rprofile.site /usr/local/lib/R/etc

EXPOSE 3838
CMD ["R", "-e belgradeapp::run_app()"]
Cervan Girard - satRday 2018 - https://thinkr.fr    24 / 34
Build our Shinyapp image
Rprofile.site file:

local({
options(shiny.port = 3838, shiny.host = "0.0.0.0")
})

We also need our package.tar.gz.

Put everything in the same folder!

Then:

cd my-app/

docker build -t mon_app .

Cervan Girard - satRday 2018 - https://thinkr.fr    25 / 34


Get shinyproxy
On github:

cd thinkr/

git clone https://github.com/openanalytics/ShinyProxy-config-examples.git

Cervan Girard - satRday 2018 - https://thinkr.fr    26 / 34


Shinyproxy and docker
Dockerfile

FROM openjdk:8-jre

RUN mkdir -p /opt/shinyproxy/


RUN wget https://www.shinyproxy.io/downloads/shinyproxy-2.0.5.jar -O
/opt/shinyproxy/shinyproxy.jar
COPY application.yml /opt/shinyproxy/application.yml

WORKDIR /opt/shinyproxy/
CMD ["java", "-jar", "/opt/shinyproxy/shinyproxy.jar"]

Cervan Girard - satRday 2018 - https://thinkr.fr    27 / 34


Shinyproxy and docker
yaml

proxy:
port: 8080
authentication: simple
admin-groups: admins
users:
- name: jack
password: password
groups: admins
- name: jeff
password: password
docker:
url: http://localhost:2375
specs:
- id: 01_hello
display-name: Hello Application
description: Application which demonstrates the basics of a Shiny app
container-cmd: ["R", "-e", "shinyproxy::run_01_hello()"]
container-image: openanalytics/shinyproxy-demo
Cervan Girard - satRday 2018 - https://thinkr.fr    28 / 34
Yaml
Let's modify this file:

proxy:
port: 8080
authentication: none
admin-groups: admins
docker:
internal-networking: true
specs:
- id: Belgrade_application
display-name: Belgradeapp
description: Application which demonstrates the basics of a Shiny app
container-cmd: ["R", "-e", "belgradeapp::run_app()"]
container-image: mon_app
container-network: sp-example-net

logging:
file:
shinyproxy.log

Cervan Girard - satRday 2018 - https://thinkr.fr    29 / 34


Last step
Deploy our app!!!

We also need folder wtih a dockerfile and application.yaml file.

### Create docker network


sudo docker network create sp-example-net

###Build the image


docker build -t mon_ShinyProxy .

### Run it
sudo docker run -d -v /var/run/docker.sock:/var/run/docker.sock --net sp-
example-net -p 8080:8080 mon_ShinyProxy

Cervan Girard - satRday 2018 - https://thinkr.fr    30 / 34


Cervan Girard - satRday 2018 - https://thinkr.fr    31 / 34
Conclusion

Cervan Girard - satRday 2018 - https://thinkr.fr    32 / 34


Would you come to Paris?

satRday Paris

23th February 2019


AgroParisTech

Cervan Girard - satRday 2018 - https://thinkr.fr    33 / 34


Thank you!
Cervan Girard
[email protected]
http://thinkr.fr

Cervan Girard - satRday 2018 - https://thinkr.fr    34 / 34

You might also like