Building an Interactive Data Exploration App with R Shiny


In this tutorial, we will walk through the creation of an interactive data exploration application using R Shiny. This app allows users to filter data, view various charts, and download them for further analysis.


  • Basic understanding of R programming
  • R and RStudio installed
  • Shiny, ggplot2, and DT packages installed

App Overview

Our R Shiny app includes:

  • A filterable table
  • Interactive charts including bar plots, scatter plots, and line plots
  • Data download functionality

Getting Started

First, ensure you have the required libraries:


Data Preparation

Load and preprocess your data. In our case, we are reading from a CSV file and creating bins for age and income:

dataset = read.csv("dataset.csv")
# Create bins for age and income
dataset$AGE_Bin = cut(dataset$AGE,5,include.lowest = TRUE)
dataset$INCOME_Bin = cut(dataset$INCOME,5,include.lowest = TRUE,dig.lab = 6)

The code contains the UI and Server in two parts. I will layout the complete code of each part here, and later in the article, I will delve into the very intuitive UI design in Shiny.

Building the UI

The user interface (UI) is designed with fluidPage for a responsive layout.

ui <-   fluidPage(
    h1("Rshiny Homework"),
    h2("Demographic Exploartion"),
    h3("Filterable Table"),
        selected = NULL,
        multiple = FALSE,
        selectize = TRUE,
        width = NULL,
        size = NULL
    actionButton("gobutton", "View Chart", class = "btn-success"),
    downloadButton(outputId = "disPlot_download", label = "Download Chart",class = "btn-success"),
    h3("Relationship Between Variables"),
                 plotOutput("Scatter", brush="selected_range"),
                 downloadButton(outputId = "scatter_download", label = "Download Chart",class = "btn-success"),
                 downloadButton(outputId = "displot2_download", label = "Download Chart",class = "btn-success"),
                 downloadButton(outputId = "displot3_download", label = "Download Chart",class = "btn-success")
    h3("Line Plot"),
    downloadButton(outputId = "lineplot_download", label = "Download Chart",class = "btn-success"),
    downloadButton(outputId = "lineplot2_download", label = "Download Chart",class = "btn-success")

Server Logic

The server function contains the logic for rendering plots and tables based on user input. As you may find, all backend data handling and visual design goes in here.

server <- function(input,output, session) {
    # library(stringr)
    dataset = read.csv("dataset.csv")
    dataset$AGE_Bin = cut(dataset$AGE,5,include.lowest = TRUE)
    dataset$INCOME_Bin = cut(dataset$INCOME,5,include.lowest = TRUE,dig.lab = 6)
    # dataset$INCOME_Bin <- lapply(strsplit(gsub("]|[[(]", "", levels(dataset$INCOME_Bin)), ","),
    #           prettyNum, big.mark=".", decimal.mark=",", input.d.mark=".", preserve.width="individual")
    plot_var <- eventReactive(input$gobutton,{
        selection <- input$option
        data_agg <-aggregate(x=dataset$Customer, by=list(SELECTION=dataset[,c(selection)],TREATMENT = dataset[,"TREATMENT"]),length)
        names(data_agg) = c("SELECTION","TREATMENT", "Customer")
    output$disPlot <- renderPlot({
        displot = ggplot(plot_var(), aes(x=SELECTION,y=Customer,fill=TREATMENT)) + geom_bar(position="stack",stat="identity")
        output$disPlot_download <- downloadHandler(
            filename = function() { paste(input$option, '.jpg', sep='') },
            content = function(file){

    output$table <- DT::renderDataTable(datatable(dataset))
    scatter_plot <- ggplot(dataset, aes(x=AGE,y=INCOME)) + geom_point()
    scatter_plot = scatter_plot + facet_grid(GENDER ~ TREATMENT)
    output$Scatter <- renderPlot({
    scatter_brushed <- reactive({
        my_brush <- input$selected_range
        sel_range <- brushedPoints(dataset, my_brush)
    output$brushed_table <- DT::renderDataTable(DT::datatable(scatter_brushed()))
    displot2 <- ggplot(dataset, aes(online.Activity.A)) + geom_histogram(aes(fill=AGE_Bin), bins = 5)
    displot2 = displot2 + facet_grid(GENDER ~ TREATMENT)
    displot3 <- ggplot(dataset, aes(online.ACTIVITY.B)) + geom_histogram(aes(fill=AGE_Bin), bins = 5)
    displot3 = displot3 + facet_grid(GENDER ~ TREATMENT)
    output$displot2 <- renderPlot({
    output$displot3 <- renderPlot({
    # scatter_brushed2 <- reactive({
    #   my_brush <- input$selected_range2
    #   sel_range <- brushedPoints(dataset, my_brush)
    #   return(sel_range)
    # })
    # output$brushed_table2 <- DT::renderDataTable(DT::datatable(scatter_brushed2()))
    data_agg2 <-aggregate(list(Activity_A=dataset$online.Activity.A), by=list(DAY=dataset$DAY,TREATMENT=dataset$TREATMENT,GENDER=dataset$GENDER),mean)
    lineplot <- ggplot(data_agg2, aes(x=DAY, y=Activity_A, group=c(TREATMENT))) + geom_line(aes(color=TREATMENT)) + geom_point()
    lineplot = lineplot + facet_grid(GENDER ~ TREATMENT)
    output$lineplot <- renderPlot({
    data_agg2 <-aggregate(list(Activity_B=dataset$online.ACTIVITY.B), by=list(DAY=dataset$DAY,TREATMENT=dataset$TREATMENT, GENDER=dataset$GENDER),mean)
    lineplot2 <- ggplot(data_agg2, aes(x=DAY, y=Activity_B, group=c(TREATMENT))) + geom_line(aes(color=TREATMENT)) + geom_point()
    lineplot2 = lineplot2 + facet_grid(GENDER ~ TREATMENT)
    output$lineplot2 <- renderPlot({
    output$lineplot2_download <- downloadHandler(
        filename = "Activity_B Line.jpg",
        content = function(file){
    output$lineplot_download <- downloadHandler(
        filename = "Activity_A Line.jpg",
        content = function(file){
    output$displot2_download <- downloadHandler(
        filename = "ActivityA_Dist.jpg",
        content = function(file){
    output$displot3_download <- downloadHandler(
        filename = "ActivityB_Dist.jpg",
        content = function(file){
    output$scatter_download <- downloadHandler(
        filename = "Age_Income.jpg",
        content = function(file){


UI Design in R Shiny

UI design in R Shiny is easy and intuitive. It’s an HTML element as a function concept. Let’s dive into how UI is designed in our R Shiny app, using the provided code as an example.

Basic Structure

R Shiny UI is structured using functions defining the layout and its elements. The fluidPage() function is often used for its responsive layout capabilities, meaning the app’s interface adjusts nicely to different screen sizes.

ui <- fluidPage(
    # UI components are nested here

Organizing Content with Headers and Separators

Headers (h1, h2, h3, etc.) and separators (hr()) are used to organize content and improve readability. In our app, headers indicate different sections:

h1("Rshiny Homework"),
h2("Demographic Exploration"),
h3("Filterable Table"),

Data Display

The DT::dataTableOutput() function is used to render data tables in the UI. This function takes an output ID as an argument, linking it to the server logic that provides the data:


Interactive Inputs

Interactive inputs, such as selectInput, allowing users to interact with the app and control what data or plot is displayed. In our app, selectInput is used for choosing demographic aspects to display in a chart:

    c("AGE_Bin", "INCOME_Bin", "GENDER"),
    selected = NULL,
    multiple = FALSE,
    selectize = TRUE,
    width = NULL,
    size = NULL

Action Buttons

Action buttons, created with actionButton(), trigger reactive events in the server. Our app uses an action button to generate plots based on user selection:

actionButton("gobutton", "View Chart", class = "btn-success"),

Displaying Plots

To display plots, plotOutput() is used. This function references an output ID from the server side where the plot is rendered:


Interactive Plots

I use ggplot2 for creating interactive plots. For example, a scatter plot is generated based on user-selected variables:

scatter_plot <- ggplot(dataset, aes(x=AGE,y=INCOME)) + geom_point()

Tabbed Panels

Tabbed panels, created with tabsetPanel(), help in organizing content into separate views within the same space. Each tabPanel holds different content:

    tabPanel("Scatter", ...),
    tabPanel("Distribution", ...)

Download Handlers

We provide functionality for users to download plots as JPEG files:

output$scatter_download <- downloadHandler(
    filename = "Age_Income.jpg",
    content = function(file){

downloadButton(outputId = "scatter_download", label = "Download Chart", class = "btn-success"),

Running the App

Finally, to run the app, use:

shinyApp(ui = ui, server = server)

Set up AWS Lightsail for Multiple WordPress Sites

This is a documentation of creating an AWS account, setting up a Lightsail (one of the functions offered by AWS) resource, and installing multiple WordPress websites. I will be covering the following topics:

  • Creating an AWS account and navigating through the dashboard of AWS
  • Setting up Lightsail LAMP Linux instance and Statics IP
  • Using SSH to connect to the terminal
  • Setting server configuration for WordPress
  • Installing WordPress
  • Create SSL Certification using Let’s Encrypt

Creating AWS Account

First of all, you need an AWS account to get started Simply visit and click “Sign in to the Console”, then register a new account. To get the account active and be able to set up Lightsail, you fill in credit information for the account. It usually takes them overnight to verify the account. If longer than that, I suggest you contact them. For corporate account, they may need to call in and verify company information.

Once the account is verified, you can set up Lightsail as followed.

Click Services on the top bar and select Lightsail (usually on the first column). When you land on Lightsail page you will see something like this.

When you create an instance, here’s what you will see.

To create a stable multisite instance without purchasing expensive plugin (especially the All-in-one backup) to support WordPress Multisite, in this documentation, we will create a LAMP (PHP 7) to build from scratch. After choosing the image, you will be asked to choose a plan. After it’s done, you will see an instance showed up on your Lightsail dashboard.

The first thing to do after creating a new instance is to attach a static IP. The IP given by AWS in your new created Lightsail is by default a floating IP.

Setting up Lightsail LAMP Linux instance and Statics IP

To attach a static IP, click the Networking tap on the instance dashboard.

On the Networking page, hit Create a Static IP. Just follow the instruction, a statics IP can be created easily.

Now we need to connect to SSH terminal to perform some cool command line work. You can either use AWS website’s SSH portal (the Connect using SSH button on the previous picture) or use your own Command Prompt or Terminal.

If you connect using the later method, you need to download the SSH Key. To download it, click Account on the top bar, and click Account on the dropdown. Then, click SSH key and download it.

Use the following command to connect through SSH

ssh USE IP AS WELL) -l bitnami -i PATH TO KEY FILE

After logging in the instance, you will see this screen on your Terminal.

Now you can perform Linux commands to the instance.

For a new created instance, I usually perform a routine update before doing anything.

sudo apt update && sudo apt upgrade

Setting server configuration for WordPress

When done, download wordpress and extract the package.

wget -c
tar -xzvf latest.tar.gz

Now, you will get a wordpress file on your folder, if you perform the command.


Next, you need to move the wordpress folder to /apps/ , where Bitnami instance usually stores website folder. You may rename it if you want.

sudo mv wordpress /apps/wordpress_opp

You need to set up the correct profile permission for it to work

chown daemon:daemon -R ./wordpress_opp
chmod 755 -R ./wordpress_opp

As you see, we set the ownership of the wordpress file to daemon. That’s what the

Before we started the installation process, we also need to create a mysql database for WordPress.

You can find the password to login using the following commands.

cat ~/bitnami_credentials

Login MySQL by typing the following commands:

mysql -u root -p

Then, a prompt to enter password shows up, and enter the password you find in the previous step. In the MySQL console, create a database using the following SQL command.

CREATE DATABASE wordpress_opp;

Remember the database name you use, because we need it for the installation of wordpress, as well as the MySQL password. You can exit the MySQL concole using:


Now, we are one step away from actually installing WordPress. We need to configure Apache2, the server software and make it point to our WordPress folder.

cd /opt/bitnami/apache2/conf/bitnami

This is where the configureation file locates. Then open the file using Vim editor.

sudo vim bitnami.conf

Press i to start editing. Find a block that is embraced by <VirtualHost _default_:80> and </VirtualHost>. For example, something like that:

<VirtualHost _default_:80>
   DocumentRoot "/opt/bitnami/apps/wordpress_opp/"
  <Directory "/opt/bitnami/apps/wordpress_opp/">
    Options Indexes FollowSymLinks
    AllowOverride All
    <IfVersion < 2.3 >
      Order allow,deny
      Allow from all
    <IfVersion >= 2.3 >
      Require all granted
  # Error Documents
  ErrorDocument 503 /503.html

  # Bitnami applications installed with a prefix URL (default)
  #Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf"

Change the path next to DocumentRoot and Directory to the path of the target WordPress folder. And comment out the “Include …” line near the bottom.

When you need to create a second/third/… site, you just need to copy and paste a new <VirtualHost> block and change the path to the right folder.

Type :w to save the file, and type :q to quit the Vim editor. Just restart the server before you are going to isntall.

sudo /opt/bitnami/ restart

Installing WordPress

Visit the ip address or the URL (if you have pointed to the ip) of the server on the browser. If everything goes right, you will see the installation page. You just need to follow the instruction. During the process, you will need the MySQL database password and the name of the database you created.

On the above page, MySQL username is root, Database Host is localhost.

Wow, you made it! A new WordPress site has been installed. When you want to install a different site, just repeat the steps with a different folder name and database name.

Create SSL Certification using Let’s Encrypt

First, you need to stop apache2.

sudo /opt/bitnami/ stop

Then, run this command to create the certificate to the default folder (or the one you chose).

sudo lego --tls --email="EMAIL-ADDRESS" --domains="DOMAIN" --domains="www.DOMAIN" --path="/etc/lego" run

At the same time, make sure to add these two lines to the apache2 config file that tells it where’s the certificate files.

SSLCertificateFile /etc/lego/certificates/
SSLCertificateKeyFile /etc/lego/certificates/

Last, it’s to set up a routine to periodically renew the certificate. You can create a bash file (.sh) and set up a cron job for it.


sudo /opt/bitnami/ stop apache
sudo /usr/local/bin/lego --tls --email='xxx' --domains=''  --domains="" --path="/etc/lego" renew

echo "lego done"
sudo certbot --apache certonly -n -d
sudo certbot --apache certonly -n -d

sudo cp /etc/lego/certificates/ /home/bitnami/somewhere/server.crt
sudo cp /etc/lego/certificates/ /home/bitnami/somewhere/server.key

#sudo reboot
sudo /opt/bitnami/ start apache

In the process there’re 3 and optionally 4 steps:

  • Stop the Apache process
  • Execute the renewal process
  • (optional) move the certicate to wherever needed
  • Start the Apache process

Setup SSL 443 Port in Apache Config

You need to add the following block to the apache config file.

<VirtualHost *:443>
  DocumentRoot "/opt/bitnami/abc/xxx"
  SSLEngine on           
  <Directory "/opt/bitnami/abc/xxx">
    Options Indexes FollowSymLinks
    AllowOverride All
    <IfVersion < 2.3 >
Order allow,deny
Allow from all
    <IfVersion >= 2.3 >
Require all granted

Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf"
  Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/
SSLCertificateKeyFile /etc/letsencrypt/live/

This block will tell the apache proxy where to look for the certificate file and what server name/alias to server. If you have set up a rewrite rule to route all HTTP to HTTPS. You can detele the :80 virtual host block mentioned above as well.

Getting Fundamental Data

The fundamental data source I use has a different format than other stock price data, so I create a different object to get the information.  The coding logic is similar to the one of getting stock price data but I use enum class to differentiate three different statements. Please check, and of course, we will add database connection later. One of the advantages of using object to get data is that we can modify them easily without affecting other parts of the code.

Get Fundamental Data

In [1]:
import pandas as pd
from datetime import datetime, timedelta
import requests as re
import json
import urllib as ur
from enum import Enum
In [21]:
class fundamentals(Enum):
    income = "income-statement"
    cash = "cash-flow-statement"
    balance = "balance-sheet-statement"

class get_fundamentals():
    def __init__(self, ticker, fun, output ="table"):
        if not isinstance(fun, fundamentals):
            raise "should input a instance of fundamentals"
        self.ticker = ticker.upper() = fun
        self.output = "table"
        self.result = {}

    def data_bulk_output(self):
        if self.output == "table":
            return self.result
    def get_fundamentals_data(self):
        while trial < 3:

                temp = re.get(profile,verify=False).text


                temp = temp.replace("<pre>","")

                temp= json.loads(temp)

                temp = pd.DataFrame(temp[self.ticker])

                self.result[self.ticker] =temp

                return temp
            except Exception as e:
                print e
In [31]:
my =
foo = get_fundamentals("AAPL",my)

Basic Cost of revenue Diluted EBITDA Gross profit Income before taxes Interest Expense Net income Net income available to common shareholders Net income from continuing operations Operating income Other income (expense) Provision for income taxes Research and development Revenue Sales, General and administrative Total operating expenses
2013-09 6477 106606 6522 57048 64304 50155 136 37037 37037 37037 48999 1292 13118 4475 170910 10830 15305
2014-09 6086 112258 6123 61813 70537 53483 384 39510 39510 39510 52503 1364 13973 6041 182795 11993 18034
2015-09 5753 140089 5793 84505 93626 72515 733 53394 53394 53394 71230 2018 19121 8067 233715 14329 22396
2016-09 5471 131376 5500 73333 84263 61372 1456 45687 45687 45687 60024 2804 15685 10045 215639 14194 24239
2017-09 5217 141048 5252 76569 88186 64089 2323 48351 48351 48351 61344 5068 15738 11581 229234 15261 26842
TTM 5171 147254 5209 79386 91922 66939 2532 50525 50525 50525 64259 5212 16414 12117 239176 15546 27663

    High QualityMedium QualityLow Quality