Skip to content

Instantly share code, notes, and snippets.

@bleach
Last active August 3, 2017 08:10
Show Gist options
  • Save bleach/cd33999ef857fa8ce5d21bc37f71461c to your computer and use it in GitHub Desktop.
Save bleach/cd33999ef857fa8ce5d21bc37f71461c to your computer and use it in GitHub Desktop.

Pushing a shiny app to CF

We recently enabled custom buildpacks on GOV.UK PaaS after requests by members of the data science community who want to deploy shiny apps.

Although our team is not able to provide official support for R/shiny buildpacks, I was able to get a simple shiny app working, with some help from Adam Robinson, who commented on a previous version of this gist. Thanks Adam!

I'm coping the data science community will be able to try using PaaS to deploy shiny apps and share what they learn and any buildpack improvements they make.

How I did it

After a few false starts I managed to get something working.

I found a page explaining how to push R apps which told me to use this buildpack.

I tried the "hello world example".

I copied some files from the example shiny app in the buildpack source that I thought would be needed and tried pushing the app.

git clone https://github.com/rstudio/shiny-examples.git
git clone https://github.com/wjjung317/heroku-buildpack-r.git
cd shiny-examples/001-hello
cp ../../heroku-buildpack-r/test/shiny/init.r .
cp ../../heroku-buildpack-r/test/shiny/Procfile .
cp ../../heroku-buildpack-r/test/shiny/run.R .
cf push -b https://github.com/wjjung317/heroku-buildpack-r.git shiny-demo

Using beibeiyang/cf-buildpack-r

I tried using the buildpack we were asked to support and I wasn't successful. You may have better knoweldge.

Enabled custom buildpacks.

git clone https://github.com/rstudio/shiny-examples.git
git clone https://github.com/beibeiyang/cf-buildpack-r.git
cd shiny-examples
cp ../cf-buildpack-r/init.r .

cf push -b https://github.com/beibeiyang/cf-buildpack-r.git -c "R -e shiny::runApp\('001-hello'\)" shiny-demo
...
** testing if installed package can be loaded
* DONE (sourcetools)
* installing *source* package ‘Cairo’ ...
** package ‘Cairo’ successfully unpacked and MD5 sums checked

0 of 1 instances running, 1 crashed
FAILED
Error restarting application: Start unsuccessful

TIP: use 'cf logs shiny-demo --recent' for more information

Excerpt from cf logs shiny-demo --recent:

2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT Copyright (C) 2016 The R Foundation for Statistical Computing
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT Platform: x86_64-pc-linux-gnu (64-bit)
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT You are welcome to redistribute it under certain conditions.
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT Type 'license()' or 'licence()' for distribution details.
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT   Natural language support but running in an English locale
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT R is a collaborative project with many contributors.
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT Type 'contributors()' for more information and
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT 'citation()' on how to cite R or R packages in publications.
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT Type 'demo()' for some demos, 'help()' for on-line help, or
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT 'help.start()' for an HTML browser interface to help.
2017-06-26T15:12:22.45+0100 [APP/PROC/WEB/0]OUT Type 'q()' to quit R.
2017-06-26T15:12:22.58+0100 [APP/PROC/WEB/0]OUT > shiny::runApp(050-kmeans-example)
2017-06-26T15:12:22.78+0100 [APP/PROC/WEB/0]ERR Error in 50 - kmeans : non-numeric argument to binary operator
2017-06-26T15:12:22.78+0100 [APP/PROC/WEB/0]ERR Calls: <Anonymous> -> as.shiny.appobj
2017-06-26T15:12:22.78+0100 [APP/PROC/WEB/0]ERR Execution halted
2017-06-26T15:12:22.79+0100 [APP/PROC/WEB/0]OUT Exit status 1

The error in the output looked like this:

2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut"
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT Copyright (C) 2015 The R Foundation for Statistical Computing
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT Platform: x86_64-unknown-linux-gnu (64-bit)
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT R is free software and comes with ABSOLUTELY NO WARRANTY.
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT You are welcome to redistribute it under certain conditions.
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT Type 'license()' or 'licence()' for distribution details.
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT   Natural language support but running in an English locale
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT R is a collaborative project with many contributors.
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT Type 'contributors()' for more information and
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT 'citation()' on how to cite R or R packages in publications.
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT Type 'demo()' for some demos, 'help()' for on-line help, or
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT 'help.start()' for an HTML browser interface to help.
2017-06-26T15:55:39.17+0100 [APP/PROC/WEB/0]OUT Type 'q()' to quit R.
2017-06-26T15:55:39.27+0100 [APP/PROC/WEB/0]OUT > shiny::runApp(001-hello)
2017-06-26T15:55:39.43+0100 [APP/PROC/WEB/0]ERR Error in as.shiny.appobj(appDir) : object 'hello' not found
2017-06-26T15:55:39.43+0100 [APP/PROC/WEB/0]ERR Calls: <Anonymous> -> as.shiny.appobj
2017-06-26T15:55:39.43+0100 [APP/PROC/WEB/0]ERR Execution halted
2017-06-26T15:55:39.44+0100 [APP/PROC/WEB/0]OUT Exit status 1
@adamrobinson361
Copy link

Using beibeiyang/cf-buildpack-r

I've got a working version on Pivotal IO using the test app in this build pack. Steps as follows:

  1. Clone the repo and go to the example shiny app
git clone https://github.com/beibeiyang/cf-buildpack-r.git
cd cf-buildpack-r/test/shiny
  1. Open the yml file to change the name of the app as currently taken on Pivotal. I did this manually.

  2. Push the app. Just need to use cf push as everything is defined in yml file.

cf push
  1. Go to your application. Note that you need to use the port 4443 at end of address of your app on Pivotal IO.

Example: https://rshiny-test-app.cfapps.io:4443

Applying to other applications.

In this example the part thats make up the shiny app are:

  1. UI
  2. Server
  3. README.md (not required but good to have)

To apply to another application we need to do the following:

  1. Delete the above files and copy all of the files that make up shiny app from an example shiny app.
cd Desktop
git clone https://github.com/beibeiyang/cf-buildpack-r.git
git clone https://github.com/rstudio/shiny-examples.git
mkdir test-app
cp -a cf-buildpack-r/test/shiny/. test-app/
rm test-app/UI.r test-app/Server.R test-app/README.md
cp -a shiny-examples/057-plot-plus-three-columns/. test-app/
cd test-app
  1. Edit the init.r file manually to include all of the r packages required to run the application.

To do this open the init.r file in any text editor. It should look like this:

#
# Example R code to install packages
# See http://cran.r-project.org/doc/manuals/R-admin.html#Installing-packages for details
#

#remove.packages("codetools")
update.packages("codetools")

###########################################################
# Update this line with the R packages to install:

my_packages = c("shiny")

###########################################################

install_if_missing = function(p) {
  if (p %in% rownames(installed.packages()) == FALSE) {
    install.packages(p, dependencies = TRUE)
  }
  else {
    cat(paste("Skipping already installed package:", p, "\n"))
  }
}
invisible(sapply(my_packages, install_if_missing))

We need to update the my_packages variable with all the packages used in the application.

For the demo app above these are as follows:

  • shiny
  • ggplot2

Update my_packages to the following:

my_packages = c("shiny","ggplot2")

Note: Be careful to use " not ” as ” will throw an r error.

  1. Open the yml file to change the name of the app.

  2. Push the app. Just need to use cf push as everything is defined in yml file.

cf push
  1. Go to your application. Note that you need to use the port 4443 at end of address of your app on Pivotal IO.

Example: https://r-shiny-example-app.cfapps.io:4443

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment