Venkat RamanNov 29, 2017
How to host a R Shiny App on AWS cloud in 7 simple steps
What is 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
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
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
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 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
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:
Running R on AWS
Shiny server troubleshoots
How to host a R Shiny App on AWS cloud in 7 simple steps
Martin DoyleNov 29, 2017
Think Like a Billionaire. Think Clean Data
Steve Ballmer the multibillionaire former CEO of Microsoft could very justifiably purchase a beach mansion in the Bahamas, erect a hammock and not have to lift a finger for the rest of his life.
But there is more on the mind of Ballmer, and that’s why he has chosen to write $10 million worth of personal cheques to USAfacts.org. A website which aims to provide accurate, accessible USA governmental data for everyone. It is a user friendly site which provides a wide range of statistics from how much tax US citizens pay to divorce rates.
Launched on April 18th 2017, USAfacts.org had so much traffic on its first day that it crashed for hours. Within 48 hours it had had more than half a million visitors and over 2 million page views. So it would seem Steve Ballmer is not the only one who cares about clean data.
Ballmer hopes that with a clear set of data at the hands of ordinary Americans, people will be able to make the best, informed decisions regarding political elections. In his own words “I’m a numbers guy. They’re how I understand the world – what’s good and what’s bad, what’s working and what isn’t”.
Do Business like a Microsoft CEO
Data is at the centre of all decisions we make. Our brains are subconsciously taking in masses of information every second. It is therefore so important that you provide your brain and computer systems with clean and easily accessible data.
All over the world, businesses make erroneous decisions simply because they had poor data to work with or couldn’t find the data that they needed. A problem that is easily avoided.
If Billionaire Steve Ballmer finds clean data such a meaningful pursuit then surely it should be first on your to do list.
The Good the Bad and the Ugly
Below we will look at three ways that data can affect your business. One good, one bad and one downright ugly.
With good data, really good things can happen. Organizations can leverage their goldmine of data to achieve a wide range of business goals from automating workflows in order to save time and money to improving customer satisfaction.
The opportunities that having good data provide are endless. The world really becomes your oyster as you choose to apply your data towards an expansive range of possibilities.
With bad data, bad things can happen. Automated processes come to a halt or can’t be implemented. Process automation absolutely requires clean, consistent data. If data becomes inconsistent, it can break processes. Likewise processes can be directed incorrectly because bad data guides them in the wrong direction.
With bad data as with good data, the possible outcomes are endless however with a negative spin. Bad data can cause havoc with all business systems, interpersonal and computational.
With bad data, the effects can become ugly very quickly. It is the problem that Steve Ballmer is trying to avoid with USAfacts.org. It is the continuous stream of bad decisions based on incomplete poor data.
There are no bounds to the destruction that bad decisions can have on your business. Every aspect of business from asset allocation to human resources can be affected. Decisions are at the root of everything we do and without quality data how can we expect to trust our decisions?
Think Like a Billionaire. Think Clean Data
Recent Comments