Git Product home page Git Product logo

shinyparallel's Introduction

ShinyParallel

Run Shiny applications in a multi-session mode.

Prevents that if a user executes a long computing task penalizing others.

ShinyParallel manages incoming users and redistributes them between multiple sessions created for your Shiny app. It provides two modes of use:

  • From an R console: ShinyParallel reimplements the function shiny::runApp(<params>). In this sense, the only thing to do to run an app in multi-session mode is to call it using shinyParallel::runApp(<params>).
  • Installing ShinyParallel in a Shiny server (may require root): by the shinyParallel::installShinyParallel(<params>) function, ShinyParallel is installed in your Shiny server for any desired app.

Note: ShinyParallel should work on any operating system that supports R, however it has been tested only under Linux (Ubuntu).

Features

  • Run a Shiny app in multiple sessions (processes / physical cores).
  • Decide the maximum number of users per session.
  • It allows to visualize the number of users currently present in each session.

Installation

ShinyParallel is currently only available as a GitHub package. To install it run the following from an R console:

if (!require("remotes")) {
  install.packages("remotes")
}
remotes::install_github("jcrodriguez1989/shinyParallel")

runApp mode

Usage

If you run your Shiny app like this:

runApp(appDir=myApp, <otherParams>)

Just replace it by:

shinyParallel::runApp(appDir=myApp, <otherParams>)

The only parameter that varies is port, in shinyParallel::runApp the parameter is modified by ports. And instead of being numeric of length 1, it will now be numeric of length equal to the number of ports available to use. Where the first port will be used by the ShinyParallel app, and the rest by the generated sessions.

The shinyParallel::runApp function has two additional parameters:

  • max.sessions: Maximum number of sessions to use.
  • users.per.session: Maximum number of admited users per each session.

Example

library("shiny")

# Create a Shiny app object
app <- shinyApp(
  ui = fluidPage(
    column(3, wellPanel(
      numericInput("n", label = "Is it prime?", value = 7, min = 1),
      actionButton("check", "Check!")
    ))
  ),
  server = function(input, output) {
    # Check if n is prime.
    # Not R optimized.
    # No Fermat, Miller-Rabin, Solovay-Strassen, Frobenius, etc tests.
    # Check if n is divisable up to n-1 !!
    isPrime <- function(n) {
      res <- TRUE
      i <- 2
      while (i < n) {
        res <- res && n %% i != 0
        i <- i + 1
      }
      return(res)
    }
    observeEvent(input$check, {
      showModal(modalDialog(
        ifelse(isPrime(isolate(input$n)),
          "Yes it is!", "Nope, not a prime."
        ),
        footer = NULL,
        easyClose = TRUE
      ))
    })
  }
)

# Run it with Shiny
shiny::runApp(app)
# Run it with ShinyParallel default params
shinyParallel::runApp(app)
# Run it with ShinyParallel, give one session per user
shinyParallel::runApp(app, max.sessions = Inf, users.per.session = 1)

In this example, if the app is run with shiny::runApp, and a user wants to calculate if the number 179424691 is prime then the app will be blocked for other users for some minutes, if the app is run with shinyParallel::runApp not.

If the shiny app url is http://<url>:<port>/ then enter http://<url>:<port>/?admin to view a panel that lists the number of users currently present in each session.

installShinyParallel mode

Usage

If your application is at <myAppPath>, i.e., from an R terminal runApp(<myAppPath>) starts the app, then to install it on the server just run R as root (or make sure the actual user has write permissions on the Shiny server) and run the installShinyParallel(<myAppPath>) command.

Example

First, let’s create our Shiny app, from a Linux terminal type:

cd ~
mkdir myShinyApp
echo "
    library('shiny')
    
    # Create a Shiny app object
    app <- shinyApp(
      ui = fluidPage(
        column(3, wellPanel(
          numericInput('n', label = 'Is it prime?', value = 7, min = 1),
          actionButton('check', 'Check!')
        )
      )),
      server = function(input, output) {
        # Check if n is prime.
        # Not R optimized.
        # No Fermat, Miller-Rabin, Solovay-Strassen, Frobenius, etc tests.
        # Check if n is divisable up to n-1 !!
        isPrime <- function(n) {
          res <- TRUE
          i <- 2
          while (i < n) {
            res <- res && n %% i != 0
            i <- i + 1
          }
          return(res)
        }
        observeEvent(input\$check, {
          showModal(modalDialog(
            ifelse(isPrime(isolate(input\$n)),
                'Yes it is!', 'Nope, not a prime.'),
            footer = NULL,
            easyClose = TRUE
          ))
        })
      }
    )
" > myShinyApp/app.R

So now we can try our app, and install it with multi-session feature, from a R (sudo) console type:

library("shinyParallel")
# And install it
shinyParallel::installShinyParallel("./myShinyApp",
  max.sessions = 20,
  users.per.session = 5
)

Limitations

  • Each session that ShinyParallel generates is independent of the others, i.e., the global variables of a session (shiny app) will not be modified in another one. Two users present in different session will not be able to interact with the same values of the variables.

shinyparallel's People

Contributors

jcrodriguez1989 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

shinyparallel's Issues

Error: Permission denied

Hi,

Thanks for this awesome package!

When I follow your instructions in the README.md, I met the following error in the log file:

su: ignore --preserve-environment, it's mutually exclusive to --login.
-bash: line 0: cd: /srv/shiny-server/shinyParallel_appLinks/myShinyApp/myShinyApp_1: Permission denied

I think this issue is related to the permission. However, I've already run R as root.

sudo -u root R

And the R code is

library('shinyParallel');
shinyParallel::installShinyParallel('~/myShinyApp', max.sessions=20, users.per.session=5);

Could you point out what mistake I've made?
Thanks!

> sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.5.2 (2018-12-20)
 os       CentOS Linux 7 (Core)       
 system   x86_64, linux-gnu           
 ui       X11                         
 language (EN)                        
 collate  en_US.utf8                  
 ctype    en_US.utf8                  
 tz       Asia/Shanghai               
 date     2019-06-22Packages ───────────────────────────────────────────────────────────────────
 package       * version date       lib
 assertthat      0.2.0   2017-04-11 [1]
 callr           3.1.1   2018-12-21 [1]
 cli             1.0.1   2018-09-25 [1]
 crayon          1.3.4   2017-09-16 [1]
 digest          0.6.18  2018-10-10 [1]
 htmltools       0.3.6   2017-04-28 [1]
 httpuv          1.4.5.1 2018-12-18 [1]
 later           0.7.5   2018-09-18 [1]
 magrittr        1.5     2014-11-22 [1]
 mime            0.6     2018-10-05 [1]
 processx        3.2.1   2018-12-05 [1]
 promises        1.0.1   2018-04-13 [1]
 ps              1.3.0   2018-12-21 [1]
 R.methodsS3     1.7.1   2016-02-16 [1]
 R.oo            1.22.0  2018-04-22 [1]
 R.utils         2.9.0   2019-06-13 [1]
 R6              2.3.0   2018-10-04 [1]
 Rcpp            1.0.0   2018-11-07 [1]
 sessioninfo     1.1.1   2018-11-05 [1]
 shiny           1.2.0   2018-11-02 [1]
 shinyParallel   0.1.001 2019-06-21 [1]
 withr           2.1.2   2018-03-15 [1]
 xtable          1.8-3   2018-08-29 [1]
 source                                        
 CRAN (R 3.5.0)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.0)                                
 CRAN (R 3.5.0)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.0)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.0)                                
 CRAN (R 3.5.0)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.0)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.2)                                
 CRAN (R 3.5.2)                                
 CRAN (R 3.5.2)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.1)                                
 CRAN (R 3.5.1)                                
 Github (jcrodriguez1989/shinyParallel@0d4e322)
 CRAN (R 3.5.0)                                
 CRAN (R 3.5.0)                                

[1] /usr/lib64/R/library
[2] /usr/share/R/library

cran version?

Hi,

Thanks for this nice package!

I'm just curious whether you will send this package to CRAN. Any plans?

Thanks again!

Windows server version?

This package is great! I would like to know if it will be available to use on windows server, since shinyserver cannot be configured there and shinyproxy configuration is very tedious. This package would become a reference for windows server users and those who develop applications in shiny

Example no running in shiny-server

Hi all! The example provided in the documentation is not running for me. I use to have regular shinny apps and now under the shinyParallel configuration is not working.
I'm doing something wrong?

Step0. Install shiniParallel
Step1. Create the app.R file into /srv/shiny-server/myShinnyApp folder. No shiny::runApp() or shinyParallel::runApp() command at the end. Here a single directory and single app.R file are created
Step2. Run the shinyParallel::installShinyParallel( ) function in a R console having '/srv/shiny-server' as a working directory. Here a shortcut to "/srv/shiny-server/shinyParallel_appLinks/myShinyApp" folder is created with N subfolder replicates of the app, provided as an argument in the function called in this step. Each subfolder myShinyApp_[N] has a app, global, server, settings and ui R files.
Step 3. Launch the [IP]:[port]/myShinyApp/ page. All blank, not working.
Step 4. Launch the [IP]:[port]/myShinyApp/?admin. A render table with users. Is working.

My original port is 3838. All shiny apps are running, but no with shinyParallel configuration. Some advice?
Thanks!

Basic docker container do not work

I am new to shinyParallel and I try to make the example app running in a docker container. The dockerfile is basic

# Base image https://hub.docker.com/u/rocker/
FROM rocker/r-ver:4.0.3

# system libraries of general use
## install debian packages
RUN apt-get update -qq && apt-get -y --no-install-recommends install \
    libxml2-dev \
    libcairo2-dev \
    libsqlite3-dev \
    libmariadbd-dev \
    libpq-dev \
    libssh2-1-dev \
    unixodbc-dev \
    libcurl4-openssl-dev \
    libssl-dev \
    libgit2-dev \
    libsodium-dev 

## update system libraries
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get clean

# install packages
RUN Rscript -e 'install.packages("shiny")'
RUN Rscript -e 'install.packages("remotes")'
RUN Rscript -e 'remotes::install_github("jcrodriguez1989/shinyParallel")'

# copy app folder
COPY . ./app

# expose port
EXPOSE 3838

# run app on container start
# CMD ["R", "-e", "shiny::runApp('/app', host='0.0.0.0', port = 3838)"]

CMD ["R", "-e", "shinyParallel::runApp('/app', ports=c(3838,3201,3023,3003), max.sessions=3, users.per.session=1)"]

Running standard shiny is fine, but with shinyParallel not. There is no error in the console, but the browser do not get any data.

This page isn’t working. localhost didn’t send any data.
ERR_EMPTY_RESPONSE

I am using Win 10 Pro, Docker for Windows and Ubuntu App 20.04 LTS
I must run with explicit ports.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.