Skip to content

Instantly share code, notes, and snippets.

@jamesmaino
Last active September 19, 2022 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmaino/603eeca8f133401fd4e73bc783ac2995 to your computer and use it in GitHub Desktop.
Save jamesmaino/603eeca8f133401fd4e73bc783ac2995 to your computer and use it in GitHub Desktop.
Guide to creating a shiny server on AWS that hosts spatial apps with persistent storage

How to install a simple shiny server for spatial apps on AWS with Ubuntu

Set up an AWS instance

https://www.charlesbordet.com/en/guide-shiny-aws

Create an AWS account. Go to the EC2 dashboard. Set up an instance using the free tier by selecting AMI Catalog in the following menu Select Ubuntu Add 30 gb of SDD

Hot tips for AWS shiny instance

  • Use the correct port (e.g. http://13.54.5.29:3838/ by default)
  • If https is not configured, make sure the url is using http
  • If performance is poor, check monitorring page on AWS EC2 dashboard
  • Restart the shiny server with sudo systemctl restart shiny-server
  • Don't use t2.micro as there is insufficient compute resources to run spatial apps

Install R and link Ubuntu repositories

https://cran.r-project.org/bin/linux/ubuntu/

# update indices
sudo apt update -qq
# install two helper packages we need
sudo apt install --no-install-recommends software-properties-common dirmngr
# add the signing key (by Michael Rutter) for these repos
# To verify key, run gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc 
# Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# add the R 4.0 repo from CRAN -- adjust 'focal' to 'groovy' or 'bionic' as needed
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

sudo apt install --no-install-recommends r-base r-base-dev r-cran-tidyverse

sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+

Install GDAL/OGR https://mothergeo-py.readthedocs.io/en/latest/development/how-to/gdal-ubuntu-pkg.html

Start R with admin priveledges and install key spatial packages

sudo R 
install.packages(c(“shiny”, "tidyverse", "rgdal", "sf", "raster", "terra", "leaflet"))

Install shiny server

https://www.rstudio.com/products/shiny/download-server/

Install shiny apps

Navigate to /srv/shiny-server/ and run git clone your_repo

This will install an app e.g. in /srv/shiny-server/app1/

Shiny server will look for folders with apps in it.

Give app write priviledge (save plots, rasters, etc) for persistent storage

See discussion here: https://groups.google.com/g/shiny-discuss/c/srWETT6uL-I

This is the correct default behavior; you generally want your R process to be running under a fairly restricted set of permissions, in case there is a security hole in your code (or in Shiny) an attacker won't be able to modify anything. However, if you do need to make modifications then you can change the config to allow that.

In this case, I'd create a subdirectory of your app dir (shiny_test) for the "shiny" user to write to: Create the directory sudo mkdir /var/shiny-server/www/shiny_test/work Make shiny the owner of the directory sudo chown shiny:shiny /var/shiny-server/www/shiny_test/work

You can use the -R flag with chown to set the owner recursively Check the ownership with ls -l /path/to/file

Automate data download with chron and Rscript calls

This is useful for scheduling background tasks such as scraping new data to keep an app current. https://www.maketecheasier.com/beginner-guide-use-cron-linux/ Given the use of relative paths in R scripts, change the present directory before calling the task. To schedule a daily task at 2am, edit the scheduled tasks sudo crontab -e.

 0 2 * * * cd /srv/shiny-server/app1 && Rscript getdata.R

Gnome desktop with RDP

It might be nice to use an interface, but it will consume resources https://ubuntu.com/tutorials/ubuntu-desktop-aws https://mremoteng.org/download

Change vim theme

https://phoenixnap.com/kb/vim-color-schemes

Download theme

sudo wget https://raw.githubusercontent.com/crusoexia/vim-monokai/master/colors/monokai.vim

Edit settings vim ~/.vimrc

then add following lines

colorscheme monokai

Continuous deployment

This will save you having to run git pull from within app folders to update. https://www.r-bloggers.com/2021/09/using-github-actions-to-update-a-shiny-app-on-a-private-server/

Using docker with R shiny and AWS

It may be useful for reproducibility to containerise apps, but the overheads of running shiny-server, R, docker may not be worth it, particularly when shiny-server can serve multiple apps. For now, I am using AWS images to backup the server (i.e. all the steps taken above). https://business-science.github.io/shiny-production-with-aws-book/

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