developing shiny app as a package and deploying it to shiny server

drmariod picture drmariod · Jun 15, 2016 · Viewed 7.5k times · Source

I am developing a shiny app and since I wanted to use automated testing and documentation of the function, I started to develop the interface within a package (as recommended here).

I develop this shiny app within RStudio and I have a server.R file which I can click on the Run App button within RStudio and everything works. I commit my package to a github repository and from this point I want to install it on my R shiny server using devtools and install_github function.

Now I am wondering how to start my app within the server. I was naively thinking to install the package and pointing to the server.R file, but this is somehow not working. Not sure where the problems are.

My second try was creating a file called app.R in a folder accessible by the shiny server and in this file I basically load my package, shiny and other needed packages but it somehow complains because of missing variables...

Can somebody give me an advice? I am also happy to answer any question since I am not sure how to ask for this problem properly. Thanks in advance.

EDIT Deploying to shiny server

Since Colin D was asking in the comments, how to deploy these packages on a shiny server, I wanted to demonstrate how I do this.

First of all I install my package on the shiny server directly via the command line as root with the following command.

su - -c "R -e \"devtools::install_github('user/shinypackage')\"" 

The next step is to change the owner of the package folder

chown -R shiny:shiny /usr/local/lib/R/site-library/shinypackage/

Then I restart the shiny server since this was sometimes problematic with caching or so.

systemctl restart shiny-server

These are the steps I do when I update my shiny app. I do this normally again as root in a single line

su - -c "R -e \"devtools::install_github('user/shinypackage')\"" &  chown -R shiny:shiny /usr/local/lib/R/site-library/shinypackage/ & systemctl restart shiny-server

One thing we still need to do is to setup the directory in the shiny-server.conf file. I just added the package path+the application directory where the ui.R and server.R is saved.

  location /shinypackage {
    app_dir /usr/local/lib/R/site-library/shinypackage/application;
    log_dir /var/log/shiny-server;
  }

Then I have to restart the server again by using systemctl restart shiny-server.

This is in use on a Ubuntu Server.

Answer

Benjamin picture Benjamin · Jun 15, 2016

When I make shiny applications as a stand-alone package, I usually organize the files as so:

In the R directory:

  • All of my methods to support the application (these should be exported if they will be used in either the ui.R, server.R, or global.R files)
  • A launch_application function

The definition of launch_application is similar to:

launch_application <- function(x, ...)
{
  shiny::runApp(appDir = system.file("application", package = [my_pkg]),
                ...)
}

In the inst directory

  • application/server.R
  • application/ui.R
  • application/global.R

After building and installing the package, I then just need to run

library(my_pkg)
launch_application(...)