How to host R shiny app
R shiny app is an interactive web interface. R shiny app has two components user interface object (UI.R) and server function (Server .R). The two components are passed as arguments to the shiny app function that creates a shiny app object. For more info on how to build Shiny apps please refer this link. Basics of R Shiny App
Step 1: Create an EC2 instance
Log in to AWS account, click on EC2 under the โComputeโ header or click on EC2 under โRecently visited servicesโ
Click on Launch Instance
Choose a Machine Image of your choice, here I have chosen Ubuntu server 16.04 LTS (HVM)
Choose an Instance type; one can start out with t2.micro, t2.small or t2.medium instances. For larger apps one can use t2.large and beyond.
Then click on launch instance, you will then be directed to the below page
Click on Edit security groups, you will be directed to below page (Configure security group).
In the SSH row, change source to โMy IPโ
Click on add a Rule, custom TCP Rule would be added. Under the โPort rangeโ enter 3838. This is the port for R shiny server.
Click โReview and Launchโ and then click โLaunchโ. Upon doing this you will get a dialogue box like below. The dialogue box helps in creating a private key which will enable us to ssh into the EC2 instance. Give a name to the key and click โDownload Key Pairโ. You will get .pem file. Save the .pem file/key securely.
Press Launch Instances. You will get a screen like below
If the instance is created successfully, the instance state will show โrunningโ
Copy the IP address under public DNS (IPv4), this will be form the basis of our URL to host the R shiny app later.
Step 2: Access the EC2 instance via SSH from Putty (Windows based)
Download putty, after downloading, convert the .pem file into ppk file.
To convert .pem file to ppk, type puttygen in the windows start dialog box. Click on โputtygenโ. The below box should appear.
Click on File tab and click on load โprivate keyโ.
Navigate to folder or path where you have saved the .pem file and select it. The .pem file will be imported and you should see a screen like below.
Now save the key as โsave private keyโ, give a name to key and save it in your desired location. Upon saving the key the below icon should appear.
Open putty and in the host name box enter the IP of EC2 instance i.e. one adjacent IPv4 Public IP (54.148.189.55) as shown in Fig a
Next navigate to โAuthโ on the left hand side panel and browse for the ppk key that you had saved earlier.
After browsing the ppk key click open. If your key is a valid one you will get a command prompt screen like below. Enter your log in credentials and then press enter
Step 3: Install WinSCP to transfer files from host machine to EC2 instance to vice versa
Enter the IP address of EC2 instance in the host name box. The click on โAdvancedโ.
Navigate to the left hand side panel and under SSH click on โAuthenticationโ, enter the private key as shown below
After entering the private key click ok. You will get the below screen. Click on โLoginโ.
You will get a dialog box like below. Just click โYesโ.
The final result will be the screen below
Step 4: Installing R base and R shiny server in EC2 instance.
The first prerequisite to run R shiny app is to install r base, shiny server, shiny package and associated packages.
To install the above, the first step is to go to the root and install them. The reason being if you are logged in as non root user in Ec2, you will have your own library path and probably the R packages, r base, shiny server may not get installed system wide. To install it system wide, go to root and install the above
Steps to go to root:
In the prompt type the below
sudo โi
You should then get a # symbol like below
Now run the following commands
sudo apt-get update
sudo apt-get install r-base
sudo apt-get install r-base-dev
The below command installs R shiny package
sudo su โ -c โR -e \โinstall.packages(โshinyโ, repos = โhttp://cran.rstudio.com/โ)\โโ
The below command installs shiny server
wget https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.4.4.807-amd64.deb
sudo dpkg -i shiny-server-1.4.4.807-amd64.deb
Step 5: Transfer the R shiny app components
After execution of above steps a directory(folder) by the name โshiny-serverโ would have been created in the path /srv/shiny-server/
The next step is to create a folder inside the directory shiny-server where we can place our R shiny app components (UI.R, Server. R, configuration file, R workspace, data files or R programs).
At first we may not be able to create folder inside the shiny-server folder, to do this execute the below commands first
sudo chmod 777 /srv/shiny-server
sudo mkdir /srv/shiny-server/myapp
In the above command I have created a folder โmyappโ to place all the R shiny app components.
Step 6: Use Winscp to transfer the R shiny app components from local machine to Ec2 instance
Now copy the R shiny app components from your local machine to Ec2 instance under the following path /srv/shiny-server/myapp/
One important thing to be taken into consideration is to configure the shiny-server.conf
The shiny-server.conf is available in the location /etc/shiny-server/
Again you may not be able to access the shiny-server directory under /etc/.
Hence run the below command
sudo chmod 777 /etc/shiny-server
After executing the above command you can copy the configuration file into local system, edit it and then transfer back the edited configuration file in the location /etc/shiny-server.
The edition to be made is as follows; please note the words after # are comments.
The result below shows shiny components copied in the path /srv/shiny-server/myapp
Step 7: Hosting the app
Now the final step. In the amazon console, go to your running EC2 instance. Copy the Public DNS (Ipv4) e.g. : ec2โ34โ215โ115โ68.us-west-2.compute.amazonaws.com.
Copy this in the browser and suffix: 3838/myapp like below and press enter.
Your R shiny app is hosted successfully!!!!
Important Notes to consider:
ยท It is important to copy the R workspace as it will contain the R objects and data files in the folder created i.e. myapp. In the above example we are using a simple shiny app โa hello world equivalentโ hence we do not have any R workspace or data files.
ยท Sometimes the app may not load for some idiopathic reasons or the screen could get โgreyedโ out. Please refresh and try again.
Sources: