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
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.
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 (18.104.22.168) 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
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
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
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
sudo dpkg -i shiny-server-22.214.171.1247-amd64.deb
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.
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
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.