Giter Club home page Giter Club logo

shinydb's Introduction

Shiny App with database

There are a few purposes of this repo.

  • show you how to access a database from a shiny app
  • how to setup CRON job to update the database
  • and how to deploy the app to google cloud run

Access Database

To access a database in Shiny application it is recommended to use the pool package.

con <- dbPool(
  RPostgres::Postgres(),
  dbname = "dbname",
  user = "user", password = Sys.getenv("DATABASEPW"), host = host
)

where the password DATABASEPW is stored in .Renviron.

Avoid fetching the data in the global frame. You should fetch the data from the database per session or per user interaction. See app.R and shiny scope rules for details.

Setup CRON job

It is easy to use GitHub actions to setup a CRON job. Check the files cron.R and cron.yaml.

  1. copy the cron.yaml to your repo, put it under .github/workflows/
  2. copy the file cron.R and edit it as needed.
  3. add the database password in your github repo settings as follow

Deploying Shiny app to Google Cloud Run

  • passwords should be stored in .Renviron
  • you also need the file Dockerfile
  • the files .gitignore and .gcloudignore are also important
    • .gitignore ensures the password is not exposed to github
    • .gcloudignore ensures the password is uploaded to google

1. Install gcloud SDK

Link: https://cloud.google.com/sdk/install

2. Initializing Cloud SDK

Go to terminal and run the following command and follow the instruction

gcloud init

3. Build and deploy the app

PROJECTID=$(gcloud config get-value project)
# Build the image of your shiny app
gcloud builds submit --tag gcr.io/$PROJECTID/shinydb
# Deploy to Google Cloud Run
gcloud run deploy --image gcr.io/$PROJECTID/shinydb --platform managed --max-instances 1 --memory 512M

ps: specify a reasonable amount of memory

shinydb's People

Contributors

randy3k avatar

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.