Skip to main content

Shiny programming: Google stock for 2015 using Quandl and Shiny

"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.

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

library(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")   
   
  })
 
})
The ui.r script is

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)
runApp("Experiment1")
After deploying the application you will see the output looking like  this

Experiment1: Google stock for 2015 using Quandl and Shiny

Comments

Popular posts from this blog

Basic Econometrics - Chapter 1 - Exercise 1

Exercise 1.1 Table 1.2 gives data on the Consumer Price Index (CPI) for seven industrialized countries with 1982-1984 = 100 as base of the index. a. From the given data, compute the inflation rate of each country. b. Plot the inflation rate for each country against time (i.e. use the horizantal axis for time and the vertical axis for the inflation rate) c. What broad conclusions can you draw abou the inflation experience in the seven countries? d. Which countries inflation seems to be most variable? Can you offer any explanation? ## Note here I have to skip several rows and add column names. Have a look at ## the raw data. Column names are c('Year', 'Canada', 'France', 'Germany', ## 'Italy','Japan', 'UK', 'US') cpi <- read.table("https://raw.githubusercontent.com/cablegui/Econometrics/master/OriginalData/Table%201.2.txt", skip = 6, col.names = c("Ye...

Step by step guide to installing and using miktex with RStudio (Windows)

Using miktex with Rstudio is very easy with the miktex portable app available from http://miktex.org/portable. Steps 1. Follow the instructions from http://miktex.org/portable to download and unzip the miktex portable application in a loccation of your choice. 2. In R write the following code in a script and save it. Note that the E:\\Software-Silo\\Miktex\\miktex\\bin location is the location where I unzipped the miktex portable application. # Install miktex y <- Sys.getenv("PATH") x <- paste0(y,";","E:\\Software-Silo\\Miktex\\miktex\\bin") Sys.setenv(PATH=x) 3. Run Miktex by double clicking the following application "miktex-portable.cmd" in the Miktex main directory. 4. Run step 2 in RStudio to install the path into R environment. 5. Open a new RNW in RStudio to test whether Miktex works . 6. Run Compile PDF in RStudio. It should be just at the top of the RNW file created in step 5. 7. You will now see a PDF file whic...

Installing and using ROracle in R

Hi, Hope this post keeps you in the best of health. I am an oracle user and wanted to know how to fetch database information in R. There is a package out there called ROracle but there are no binaries for it and it thus needs to be built and then installed. Here are the steps to install it on Windows 7 machines. 1. Download the package from http://cran.r-project.org/web/packages/ROracle/index.html. Since I wrote this post the latest that was available was  ROracle_1.1-12.tar.gz . 2. Place the package in the directory where R is installed. I placed mine in E:\R\R-3.0.2\bin folder. 3. Install RTools from http://cran.r-project.org/bin/windows/Rtools/. Since my R version is R-3.0.2 the toolkit I needed was RTools31.exe. 4. Install the Rtools software in the R home directory. I placed mine in E:\R\Rtools. Place all the extras in there too. For example I placed my 32 bit extras in E:\R\RExtras32 and the 64 bit in E:\R\RExtras64 folder. These extras are not necessary for ...