Shiny by Rstudio accomplishes an extraordinary and disruptive feat: it puts dissemination of interactive, analytic results in the hands of the analysts. In professional settings, there has historically been Business Intelligence (BI) software, with an interactive, user-friendly interface, separating those performing analyses in a program like R from business users. Shiny makes it possible to design that interactive, user-friendly interface in R itself, obviating the need to use an additional tool to make interactive, analytic results accessible to business users.
So, how would one take a Shiny app, such as the one we created in the previous post, and make it accessible to business users who probably don’t have R installed on their machines and may not be particularly technically savvy?
Shiny apps are accessible over the internet as webpages, so users need only have an internet connection. There are two hosting solutions: Shiny by Rstudio can host the app for you (see shinyapps.io to learn more), or you can host it on your own server. Generally, hosting it yourself is cheaper and offers greater control over who has access to the Shiny tool you’ve created, but there’s extra work involved in setting up and maintaining a server. This post discusses how to set up your own server in Amazon’s cloud and host your Shiny app yourself.
Hosting your Shiny app on your own server does require knowledge of how to set up your own server, though, so I’d like to introduce data warehouse, Business Intelligence, and software architecture expert Michael Helms. Mr. Helms has over 20 years of experience in IT, and has worked in senior roles designing and managing the data systems for some of the largest and most successful companies in the country. He has kindly agreed to share his insights with you in this guest post. I’ll let him take over from here.
Launch a Shiny App on Your Own Server in 4 Steps (Michael Helms)
There are four major steps involved in setting up your own Shiny server to host your R application. In this example we’ll use Amazon’s EC2 cloud service to setup a basic server. I’ve chosen to use Ubuntu Server 14.04 here, but you should be able to adapt this to other Linux distributions with minimal effort.
Step 1: Setup the Linux Server in Amazon’s Cloud
To setup a server in Amazon’s cloud, you’ll need an Amazon account. Head over to http://aws.amazon.com and either create an account, or login with your existing Amazon credentials.
Once you’re logged in, you’ll want to go to “EC2” in the console. EC2 stands for “Elastic Compute Cloud,” and it allows you to create virtual servers. In the EC2 dashboard you should see a large blue button titled “Launch Instance;” click on this to get started creating your server.
Amazon provides a number of prebuilt server options for you to pick from. The basic “Ubuntu Server 14.04 LTS (HVM), SSD Volume Type” option is what we’ll use here. LTS stands for “Long Term Support,” which means that this version of the operating system will be supported for at least 5 years. 14.04 was released in the middle of 2014, so it will provide reliable service until at least 2019.
You may also note that this option (and several others) are “Free tier eligible.” Some of Amazon’s basic servers can be stood up for free, which will be the goal of this exercise. It’s worth noting that there may still be network bandwidth fees and static IP address fees, but running a free tier server should keep the cost of this down to a few dollars per month.
After you’ve selected the “Ubuntu Server 14.04 LTS (HVM)” option, you’ll be asked which tier of server you want. The “Free Tier Eligible” option should be selected by default, which will suffice for this demo. If you need more power later you can upgrade to a more powerful virtual server, but you’ll want to make sure you research the costs of doing this at https://aws.amazon.com/ec2/pricing/.
Click the grey “Next: Configure Instance Details” button. You won’t need to change any of the default settings here.
Click the grey “Next: Add Storage” button. You won’t need to change any of the default settings here.
Click the grey “Next: Tag Instance” button. You won’t need to change any of the default settings here.
Click the grey “Next: Configure Security Group” button. Shiny uses port 3838 for inbound web connections, so we’ll need to add a rule for this. Click the “Add Rule” button and on the “Custom TCP Rule” line, set the “Port Range” to 3838 and the “Source” to “Anywhere.”
Click the blue “Review and Launch” button. You won’t need to change any of the settings here.
Click the blue “Launch” button. A window will now appear prompting you to “Select an existing key pair or create a new key pair.” The key is what you will use to login to your Linux server; without that you won’t be able to access your server and you’ll need to start over (losing all of your work). In the first dropdown select “Create a New Pair,” and in the “Key pair name” box type “Shiny.”
Now, click on the “Download Key Pair” button. Your browser will now download a file onto your local filesystem. Don’t lose this file!
You can now click on the “Launch Instance” button. Congratulations; if you’ve followed these steps you now have a virtual Linux server on which to install Shiny!
Click on the blue “View Instances” button in the bottom, right-hand corner of the screen (you may need to scroll down) to return to the console. You should now see a single entry in the list of instances; this is your very own Ubuntu Linux server booting up! In a few seconds the “Instance State” will change to “running,” and your server will be ready to login to.
Step 2: Logging into your Linux Server
Now that our server is setup, we’ll need to login to it to install Shiny. The instructions here will assume that you’re using an Apple computer running OS X. Amazon also provides instructions for how to login with PuTTY, which is generally the best way for Microsoft Windows users to connect.
Click the box to the left of your Linux server in the web browser. This will highlight the line, and the “Connect” button should now appear. Clicking “Connect” will prompt you with a box of instructions.
In the box will be an “Example” line with a lot of text that may not make sense to you (it will start with “ssh” and end with “amazonaws.com”. Highlight that line and copy it to the clipboard; we’ll need it in a few steps.
In OS X, you’ll want to click on the Spotlight search button in the menu bar. Type “terminal” and hit enter. You should now see a white window appear with some text in it. Don’t worry if you’ve never used the command line terminal before; this is simply a text-based interface through which you can control your computer.
You probably downloaded your security key file into your Downloads folder. In your terminal window, type “cd Downloads” (without the quotes) and hit Enter. Now type “ls” and hit Enter again. A list of files in your Downloads folder should now appear, and in that list you should see Shiny.pem file.
Sometimes the web browser will append “.txt” to the name of the file. If it did, type the following exactly as it appears here:
mv Shiny.pem.txt Shiny.pem
Don’t worry if you don’t get any feedback in the terminal window; this is one place where less is sometimes more!
You will now need to make a small change to the security on that file. Type the following exactly as it appears here:
chmod 400 Shiny.pem
Now you’re ready to execute the text you copied onto your clipboard. Paste the contents of your clipboard into the terminal window (this will be a long line starting with “ssh” and ending with “amazonaws.com”) and hit Enter.
You may be promoted with a confusing question about “The authenticity of host ... can’t be established.” This is entirely normal, and you’ll want to type “yes” to allow your computer to trust this server.
You should now see a prompt that begins with “Ubuntu@ip-000-000-000-000” (your numbers will be different). If you do, then congratulations; you’ve successfully logged into your Linux server!
To log back into your server at any time, you’ll need to copy and paste the connection string line (starting with “ssh” and ending with “amazonaws.com”) into a terminal window. Make sure you’re in the same directory as your Shiny.pem file.
Your terminal window is now remote-controlling the Linux server you setup. The PEM file you downloaded is a secure “key” that lets you in; without that file nobody can connect to this server.
Step 3: Installing Shiny
The next step is to install the Shiny server. To do that, copy and paste the following lines into your terminal window. Some of these lines will take some time to execute (and will output a lot of text into the window); that’s perfectly normal. The whole process will probably take five to ten minutes.
sudo su -c "echo 'deb http://archive.linux.duke.edu/cran/bin/linux/ubuntu trusty/' >> /etc/apt/sources.list" sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9 sudo apt-get update sudo apt-get upgrade -y sudo apt-get dist-upgrade -y sudo apt-get install r-base -y sudo su - \ -c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\"" sudo su - \ -c "R -e \"install.packages('fpp', repos='https://cran.rstudio.com/')\"" sudo su - \ -c "R -e \"install.packages('rmarkdown', repos='https://cran.rstudio.com/')\"" sudo su - \ -c "R -e \"install.packages('ggplot2', repos='https://cran.rstudio.com/')\"" sudo apt-get install gdebi-core -y wget https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-22.214.171.1249-amd64.deb sudo gdebi shiny-server-126.96.36.1999-amd64.deb -y
Kimberly's note: If you get an error, try running each line of code by itself. For lines that end with "-y", omit that part of the line, and then when prompted, respond with the "Y" response (for "yes"). You will need to install any R packages used by your app. Use the "sudo su - \" line and then the following line of code that specifies the package to add. The example above loads rmarkdown and ggplot2 on the server, as well as shiny.
The last line to appear should say something like this: “shiny-server start/running, process 6895.” The number may be different … but if you see this, then Congratulations! You now have Shiny running on your Linux server!
To test this, let’s do an experiment. In the Amazon control panel in your web browser, near the right hand side of the screen (you may need to scroll) is the “Public IP” column. In this you’ll see four numbers separated by periods. This group of numbers and periods is the IP address of your server; think of this as your server’s unique address on the internet.
Copy and paste that IP address to your clipboard. Now, open a new web browser window and go to the following address (using the IP address that you copied to your clipboard in place of the zeros I have here):
The “:3838” is important to keep at the end, since this is the port Shiny uses. You may remember setting up a rule for this when we setup the Amazon Linux server.
If you’ve done everything correctly, you should see a “Welcome to Shiny Server!” page in your web browser. Shiny is now installed and is ready to host your applications!
Step 4: Installing Your Application on Shiny
Your application consists of a “.R” file and one or more “.Rda” files. We will now need to get these files onto your Shiny server. Before we do anything else, you’ll need to copy these files into your Downloads folder (the same folder that your Shiny.pem file is in). Do this now.
Copying the files will be the most challenging part of this exercise. To do this, you’ll need to use the “scp” command in your terminal window. “scp” stands for “Secure Copy,” which will allow your laptop to securely send files to your Amazon server.
You’ll need three things: the “pem” file (already in your Downloads folder), your .R and .Rda files (already in your Downloads folder) and the full login string for your Linux server. We’ll need to go back to the Amazon console window. Highlight the line for your server by clicking on the box on the left, and click the “Connect” button at the top. In the window that appears, you’ll want to copy part of the line that starts with “ssh”. You will only copy the part that starts with “ubuntu@” and ends with “amazonaws.com”. Once this is on your clipboard, you’ll want to go back to your terminal window.
Your terminal window is currently logged in to your Linux server (you’ll know this because the bottom line starts with “ubuntu@”). If this is the case, type “exit” and hit enter.
You are now logged out of your Linux server and are back to controlling your computer. The command line prompt should now begin with the name of your local computer (probably something like “KimberlyMacBookPro”).
Kimberly's note: Your may need to type "exit" a couple of times to return to your local computer. If this doesn't work, you can also quit out of the Terminal window and then re-open it.
To see where you are, type “ls” and hit Enter. You should see “Shiny.pem,” your .R and your .Rda files. If not, type the following command:
Now we will copy your .R and .Rda files to your Linux server. To do this, you’ll need to type the following command (but don’t hit enter yet):
scp –i Shiny.pem *.R <connectionstring>:~
You’ll need to replace <connectionstring> with the text you copied onto your clipboard from the Amazon control panel.
After the file(s) finish copying, you’ll need to do the same thing with the following line:
scp –i Shiny.pem *.Rda <connectionstring>:~
Once these files finish copying, we’ll need to log back into your Linux server to put the files in the correct location. To do this, go back to your Amazon control panel and copy the entire connection string (starting with “ssh” and ending with “amazonaws.com”) into your clipboard. In your terminal window, paste that string in and hit enter.
You’ll know that you’re logged in correctly because the command prompt will now begin with “ubuntu.”
Type “ls” and hit Enter to see a list of files stored on the Linux server. You should now see your .R and .Rda files listed.
The last step is to copy the .R and .Rda files into the correct folder in the Shiny server. The following block of text will remove any old apps that you’ve deployed and replace them with the app in your current .R and .Rda files:
sudo rm /srv/shiny-server/*.R sudo rm /srv/shiny-server/*.Rda
Kimberly's note: Don't worry if the Terminal tells you that there are no files with these names - this just means you don't already have app files by these names in the folder.
sudo cp *.R /srv/shiny-server sudo cp *.Rda /srv/shiny-server
Now you can go to the web address of your shiny server (see how to find that near the end of step 3) to see your newly deployed app in action! You’ll need the “Public IP” of your Linux server, which you can find in the Amazon console window. Your URL should look something like this:
You've done it! If you go to the web address for your server, you should now see your deployed app.