Skip to content

analythium/heroku-shiny

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Heroku deployment examples

Workflow (works with public and privare repositories):

  • start with this template or modify your app taking this as an example
  • run renv::init() or renv::snapshot() to capture dependencies in the renv.lock file
  • work on your app
  • add secrets to your GitHub repo settings and follow steps below

The /app folder contains the shiny app. The demo app was taken from here: https://github.com/analythium/shinyproxy-demo

Note: Shiny apps time out after 55 seconds:

After the initial response, each byte sent from the server restarts a rolling 55 second window. A similar 55 second window is restarted for every byte sent from the client.

If no data is received from the dyno within the 55 second window the connection is terminated and an H15 error is logged.

Similarly, if no data is received from the client within the 55 second window, the connection is terminated and an H28 error is logged.

A workaround was posted on SO to print a dot to the console every passage of 10 seconds. A counter set at 50 seconds interval is added to /app/server.R:

## prevent timeout
autoInvalidate <- reactiveTimer(intervalMs = 50*1000)
observe({
    autoInvalidate()
    cat(".")
})

The following deployment options are explained here:

Deployment using GitHub actions

Log into Heroku, in the dashboard, click on 'New' then select 'Create new App'. Give a name (e.g. shiny-cicd, if available, this will create the app at https://shiny-cicd.herokuapp.com/) to the app and create the app.

Got to the Settings tab of the repo, scrolld down to Secrets and add the following new repository secrets:

  • HEROKU_EMAIL: your Heroku email
  • HEROKU_APP_NAME: you application name from above
  • HEROKU_API_KEY: your Heroku api key, you can find it under your personal settings, click on reveal and copy

See the .github/workflows/deploy.yml file for additional options (dockerfile_name, docker_options, dockerfile_directory)

Deployment using local Heroku CLI

Prerequisites

  • Install Heroku CLI
  • R & Shiny
  • Docker Engine installed & a Docker Hub account

Building Docker Images with heroku.yml

See docs: https://devcenter.heroku.com/articles/build-docker-images-heroku-yml

Pieces

Create a heroku.yml file in your application's root directory. The following example heroku.yml specifies the Docker image to build for the app’s web process:

build:
  docker:
    web: Dockerfile

Heroku uses the CMD specified in the Dockerfile.

The Dockerfile defines system requirements, R package dependencies.

FROM rocker/r-base:latest

...

ENV PORT=8080

CMD ["R", "-e", "shiny::runApp('/home/app', host = '0.0.0.0', port=as.numeric(Sys.getenv('PORT')))"]

The port settings and the CMD part is different from ShinyProxy apps, port number is passed as an env var by the Heroku container runtime.

Create app

See also here: https://github.com/virtualstaticvoid/heroku-docker-r

Commit your changes if any (only repos with new commits will deploy once the app is already on Heroku):

git commit -m "Changed"

Create the Heroku application with the container stack

heroku create --stack=container

Now you should see the app url.

Or configure an existing application to use the container stack.

heroku stack:set container

Deploy

Deploy your application to Heroku, replacing <branch> with your branch, e.g. main:

git push heroku <branch>

Now you see the build logs on the Heroku remote.

Open app

Now open the app in your browser:

heroku open

https://morning-springs-64281.herokuapp.com/

Test the Docker container locally

To build the image from the Dockerfile, run:

sudo docker build -t analythium/heroku-demo .

Test:

docker run -p 4000:8080 analythium/heroku-demo

then visit 127.0.0.1:4000.

(c) Copyright Analythium Solutions Inc., 2021 (MIT).

About

Deploy Shiny Apps to Heroku with CLI and CI/CD

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published