"Shiny" is a nice tool for data analysis and visualisation available for download ín R. You can create interactive webpages and use supporting packages like "highcharter" to make the plotting more interactive by directly manipulating the plot to change the input and reflect the changed input to your outputs.
These notes are an ongoing series of experiments in my learning of the package.
Lets plot the time series of Google stock. The data for Google is downloaded using Quandl. Every Quandl code has 2 parts: the database code which specifies where the data comes from, and the dataset code which identifies the specific time series you want. To browse you can use https://www.quandl.com/browse . You need an api key for this which you can easily get using your linkedin login or google login. For example here is what a request would look like for downloading Google stock data for the year 2015.
This command will download the GOOGLE stock data (open,high, low, close and volume) for each of the days starting from 1st of Jan 2015 to 31st of Dec 2015 and arranges the dates in ascending order. If you run the head command on the stkPrice_Google2015 variable
and tail
The Shiny application will be called Experiment1. To set things up I will need to create 4 things
1. Create an Experiment1 folder.
2. Create a server.R script file in the Experiment1 folder.
3. Create a ui.R script file in the Experiment1 folder.
4. Create an Experiment1_App.R script file outside the Experiment1 folder.
The server.R script is
The Experiment1_App.R script is
Experiment1: Google stock for 2015 using Quandl and Shiny
These notes are an ongoing series of experiments in my learning of the package.
Lets plot the time series of Google stock. The data for Google is downloaded using Quandl. Every Quandl code has 2 parts: the database code which specifies where the data comes from, and the dataset code which identifies the specific time series you want. To browse you can use https://www.quandl.com/browse . You need an api key for this which you can easily get using your linkedin login or google login. For example here is what a request would look like for downloading Google stock data for the year 2015.
library("Quandl")stkPrice_Google2015 <- Quandl("GOOG/NASDAQ_GOOGL",
api_key="B-_yxBQnSM6NzL1j_B_b",
trim_start="2015-01-01",
trim_end="2015-12-31",
order="asc")
This command will download the GOOGLE stock data (open,high, low, close and volume) for each of the days starting from 1st of Jan 2015 to 31st of Dec 2015 and arranges the dates in ascending order. If you run the head command on the stkPrice_Google2015 variable
head(stkPrice_Google2015)
Date Open High Low Close Volume
1 2015-01-02 532.60 535.80 527.88 529.55 1327870
2 2015-01-05 527.15 527.99 517.75 519.46 2059119
3 2015-01-06 520.50 521.21 505.55 506.64 2731813
4 2015-01-07 510.95 511.49 503.65 505.15 2345875
5 2015-01-08 501.51 507.50 495.02 506.91 3662224
6 2015-01-09 508.18 508.60 498.65 500.72 2100024
and tail
tail(stkPrice_Google2015)
Date Open High Low Close Volume
243 2015-12-23 770.69 771.90 757.65 768.51 1529742
244 2015-12-24 768.52 769.20 764.39 765.84 521141
245 2015-12-28 770.00 782.82 767.73 782.24 1557755
246 2015-12-29 786.99 798.69 786.20 793.96 1921480
247 2015-12-30 793.96 796.46 787.20 790.30 1428280
248 2015-12-31 787.82 788.33 777.32 778.01 1637561
Lets now prepare the data for displaying on the webpage with the historical returns of Google for 2015 binned into a histogram using Shiny.The Shiny application will be called Experiment1. To set things up I will need to create 4 things
1. Create an Experiment1 folder.
2. Create a server.R script file in the Experiment1 folder.
3. Create a ui.R script file in the Experiment1 folder.
4. Create an Experiment1_App.R script file outside the Experiment1 folder.
The server.R script is
The ui.r script islibrary(shiny)
library(Quandl)
library(ggplot2)#The server does the processing work
shinyServer(function(input, output) {
# Download stock price of Google using the Quandl library
stkPrice_Google2015 <- Quandl("GOOG/NASDAQ_GOOGL",
api_key="xxxxxxxxxxxxxxxxxxx",
trim_start="2015-01-01",
trim_end="2015-12-31",
order="asc")
#renderPlot function is required to plot the graph
output$distPlot <- renderPlot({
#ggplot qplot function
qplot(stkPrice_Google2015$Date,stkPrice_Google2015$Close,
geom=c("line","point"),
xlab="Time",
ylab="stock price")
})
})
library(shiny)# The ui does the front end and takes inputs from the user
shinyUI(fluidPage(
# Application title
titlePanel("Google stock plot"),
#Plot the graph
plotOutput("distPlot")
)
)
The Experiment1_App.R script is
library(shiny)After deploying the application you will see the output looking like this
runApp("Experiment1")
Experiment1: Google stock for 2015 using Quandl and Shiny
Comments
Post a Comment