Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 523
drmariod Avatar asked Jun 15 '16 09:06

drmariod


People also ask

Where can I publish a Shiny app?

The easiest way to turn your Shiny app into a web page is to use shinyapps.io, RStudio's hosting service for Shiny apps. shinyapps.io lets you upload your app straight from your R session to a server hosted by RStudio.

Is Shiny a package?

Let users interact with your data and your analysis. And do it all with R. Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards.

How do you structure a Shiny app?

Shiny applications have two components, a user interface object and a server function, that are passed as arguments to the shinyApp function that creates a Shiny app object from this UI/server pair. The source code for both of these components is listed below.


2 Answers

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(...) 
like image 165
Benjamin Avatar answered Oct 20 '22 23:10

Benjamin


There is already an accepted answer with many votes, but I'd like to add a few things, so I'll answer myself as well. For more information, you can read my article Supplementing your R package with a Shiny app.

This is the folder structure I use:

- mypacakge   |- inst       |- myapp          |- ui.R          |- server.R   |- R      |- runApp.R      |- ...   |- DESCRIPTION   |- ... 

Inside the R/ folder is where I place all the non-shiny code. The code for the shiny app itself lives in inst/. The R/runApp.R file is defined as

#' @export runExample <- function() {   appDir <- system.file("myapp", package = "mypackage")   if (appDir == "") {     stop("Could not find myapp. Try re-installing `mypackage`.", call. = FALSE)   }    shiny::runApp(appDir, display.mode = "normal") } 

(You can see this in action; for example, shinyalert uses this structure for its demo app).

In a comment, you asked how can this be deployed on a shiny server. It's simple, you can simply have a file /srv/shiny-server/myapp.app.R that calls and runs that package (after you've installed the package on the server):

dir <- system.file("myapp", package = "mypackage") setwd(dir) shiny::shinyAppDir(".") 

(You can see this in action as well, code here)

like image 21
DeanAttali Avatar answered Oct 20 '22 23:10

DeanAttali