Deploying Shiny
From Local to Cloud
Deploying a Shiny application is the final step in bringing your interactive web app to the public. While building and testing a Shiny app locally is crucial, deploying it so others can access and interact with it is equally important. In this blog post, we’ll explore how to deploy Shiny apps using shinyapps.io and how to set up custom servers for more control over deployment. By the end of this post, you will be familiar with the deployment options and best practices for Shiny apps.
1. Hosting on shinyapps.io
shinyapps.io is the cloud-based hosting platform provided by RStudio for deploying Shiny applications. It offers a simple and user-friendly way to get your app online without the need to worry about server setup, maintenance, or scalability. It’s ideal for smaller projects, prototypes, and individual developers.
Setting Up shinyapps.io
Before you can deploy your Shiny app on shinyapps.io, you’ll need to:
- Create an Account:
- Go to shinyapps.io and sign up for an account.
- Once signed up, you’ll be provided with a free tier that allows for up to 5 active apps with limited resources. For larger projects, you may need a paid plan.
- Install the
rsconnect
Package:- To deploy from RStudio, you need to install and load the
rsconnect
package. This package helps connect your local R environment to shinyapps.io.
r Copy codeinstall.packages("rsconnect") library(rsconnect)
- To deploy from RStudio, you need to install and load the
- Authenticate Your Account:
- After installing the
rsconnect
package, authenticate your account by setting up your account information in RStudio. You can do this by using thesetAccountInfo
function, which requires your shinyapps.io token and secret.
You can find the token and secret in the Account Settings section of shinyapps.io.r Copy code::setAccountInfo(name = 'your_username', rsconnecttoken = 'your_token', secret = 'your_secret')
- After installing the
Deploying Your App
Once everything is set up, deploying your app is easy:
- Open the app in RStudio.
- Click on the Publish button in the top-right corner of RStudio.
- Select shinyapps.io as the deployment target.
- Select the account you want to deploy to.
- Click Publish.
RStudio will then upload your Shiny app to shinyapps.io. After a few moments, you’ll receive a link to access your app live on the web.
Monitoring and Scaling Your App
Once your app is deployed, you can manage it directly from shinyapps.io’s dashboard. Here, you can monitor the performance of your app, review logs, and see usage statistics. If your app becomes popular, you can scale it by upgrading to a higher-tier plan for more resources.
Advantages of shinyapps.io
- Ease of Use: Simple deployment process with no server setup required.
- Automatic Scaling: Handles scaling for small to medium-sized apps automatically.
- Integrated with RStudio: Seamless integration for developers working in RStudio.
- Free Tier: The free plan allows you to deploy apps with some limitations, making it perfect for prototypes or personal projects.
2. Setting Up Custom Servers for Deployment
For more control over your Shiny app’s deployment, you may want to host it on a custom server. This is particularly useful if you require more resources, need specific configurations, or want to integrate the app with a larger infrastructure.
Using Shiny Server
Shiny Server is an open-source server application developed by RStudio for hosting Shiny apps on Linux-based servers. With Shiny Server, you can deploy apps on your own servers, whether on physical hardware or cloud services like AWS or DigitalOcean.
Installing Shiny Server
Install R: Ensure that R is installed on your server.
Install Shiny Server: You can install Shiny Server by running the following commands on your Linux server (Ubuntu/Debian example):
bash Copy code sudo apt-get update sudo apt-get install r-base sudo apt-get install gdebi-core sudo gdebi shiny-server.deb
Start Shiny Server: After installation, start the Shiny Server service:
bash Copy code sudo systemctl start shiny-server
Access the Admin Interface: Once Shiny Server is installed and running, you can access the server’s administration interface by going to
http://<server-ip>:3838
in your web browser. The default configuration serves Shiny apps from the/srv/shiny-server/
directory.
Deploying Your App to Shiny Server
Once Shiny Server is set up, you can deploy your app by:
Copying your Shiny app folder to the
/srv/shiny-server/
directory:bash Copy code sudo cp -r /path/to/your/app /srv/shiny-server/
Ensuring that the app folder has the correct permissions:
bash Copy code sudo chown -R shiny:shiny /srv/shiny-server/your_app
Accessing your app in the browser via
http://<server-ip>:3838/your_app/
.
Advanced Customization
- Authentication: You can set up basic authentication to restrict access to your Shiny app. This can be done using
.htpasswd
files or by configuring Shiny Server to require login credentials. - Multiple Apps: You can deploy multiple apps on the same server by organizing them into subdirectories under
/srv/shiny-server/
. - SSL: For secure communication, you can set up SSL using a reverse proxy like Nginx or Apache.
Using Docker for Shiny Deployment
Another option for hosting Shiny apps is using Docker containers. Docker allows you to package your Shiny app and its dependencies into a container that can run on any system with Docker installed. This approach simplifies deployment and scaling, especially when managing multiple instances.
Setting Up a Docker Container for Shiny
Create a Dockerfile: This file contains instructions on how to set up the environment for your Shiny app.
Example
Dockerfile
:dockerfile Copy code FROM rocker/shiny COPY . /srv/shiny-server/ RUN chown -R shiny:shiny /srv/shiny-server EXPOSE 3838 CMD ["/usr/bin/shiny-server"]
Build the Docker Image: Build the image using the following command:
bash Copy code docker build -t my_shiny_app .
Run the Docker Container: Run the container with the command:
bash Copy code docker run -d -p 3838:3838 my_shiny_app
Access the App: Your app will now be accessible via
http://localhost:3838
on your local machine orhttp://<server-ip>:3838
if running on a remote server.
3. Conclusion
Deploying a Shiny application is an exciting milestone in your app development journey, and there are several ways to get your app online, ranging from using the convenient shinyapps.io service to setting up your own custom server.
- shinyapps.io provides an easy and seamless deployment process for smaller projects, with minimal setup and maintenance.
- Shiny Server offers more control for advanced users who want to manage their own infrastructure.
- Docker provides a portable, scalable solution for deploying Shiny apps in a containerized environment.
Whether you choose shinyapps.io or set up your custom server, understanding the deployment process is key to ensuring that your Shiny app runs smoothly and remains accessible to users.