This example is the first Shiny app I ever deployed as part of my masters program many years ago and analyzes three plays by Shakespeare. It's always fun looking back at old code especially when I am able to deploy it straight on Snowflake when back then I didn't even know what Snowflake was.
Prerequisites:
- Snowflake Account: Ensure you have SYSADMIN privileges, cool with someone who does, or if working in a DEV environment, have the necessary privileges to create a stage, image repository, and compute pool.
- Docker: Docker needs to be installed. Learn more about Docker and Snowpark Container Services.
- Visual Studio Code (optional): Install VS Code along with the Snowflake extension.
Step 1: Set up infrastructure in Snowflake
- Log in to Snowsight or use the Snowflake extension in VS Code. Select a database and schema and then proceed with the following:
USE ROLE SYSADMIN; CREATE STAGE IF NOT EXISTS chase ENCRYPTION = (TYPE='SNOWFLAKE_SSE'); -- SNOWFLAKE_SEE does server side encryption CREATE IMAGE REPOSITORY IF NOT EXISTS images;
Step 2: Configure a compute pool
- Create a compute pool configured as follows:
This compute pool provides 2 vCPU, 8 GiB of Memory, and 250GiB of storage, costing approximately $0.22/hr on AWS US East Standard pricing as shown here.
CREATE COMPUTE POOL IF NOT EXISTS chases_r_pool MIN_NODES = 1 MAX_NODES = 1 INSTANCE_FAMILY = CPU_X64_XS;
Step 3: Prepare your application
- Clone my repository or create your own and within it, create a folder named 'data' for any data you might use. Add your
app.R
file and the Dockerfile provided below. Note that your package list may vary based on your Shiny app requirements.# Base R Shiny image FROM rocker/shiny # Install R dependencies RUN R -e "install.packages(c('dplyr', 'ggplot2', 'gapminder', 'tidytext', 'wordcloud', 'tidyverse', 'RColorBrewer', 'shinythemes', 'shinyFiles'))" # Copy the Shiny app code COPY app.R app.R COPY data data # Expose the application port EXPOSE 8787 # Run the R Shiny app CMD Rscript app.R
Step 4: Build and deploy the Docker image
- Log in to Docker and push the built image to Snowflake in a shell:
docker login <account>.registry.snowflakecomputing.com docker build --rm --platform linux/amd64 -t <account>.registry.snowflakecomputing.com/<database>/<schema>/images/shiny . docker push <account>.registry.snowflakecomputing.com/<database>/<schema>/images/shiny
Step 5: Create the Service
- Create and configure the service in Snowflake:
CREATE SERVICE shiny_app IN COMPUTE POOL COMPUTE_POOL_1 FROM SPECIFICATION $$ spec: container: - name: shiny image: <account>.registry.snowflakecomputing.com/<database>/<schema>/images/shiny env: DISABLE_AUTH: true endpoint: - name: shiny port: 8787 public: true $$ MIN_INSTANCES=1 MAX_INSTANCES=1;
Step 6: Manage and get logs for the service
- Useful commands for service management:
desc service shiny_app; select system$get_service_status('shiny_app'); select system$get_service_logs('shiny_app','0', 'shiny',100);
Step 7: Access your Shiny app
-
Retrieve and use the URL for your hosted Shiny app:
show endpoints in service shiny_app;
Use the URL from
ingress_url
to access your Shiny app in a secure Snowflake environment. This URL can be shared with anyone who has appropriate access privileges.