Skip to main content

Shiny programming: Date range select for stock plot using Quandl and Shiny

This experiment will plot the Microsoft stock or the Google stock depending on user input. User can also select a date range.

The Shiny application will be called Experiment2. To set things up I will need to create 4 things

1. Create an Experiment2 folder.
2. Create a server.R script file in the Experiment2 folder.
3. Create a ui.R script file in the Experiment2 folder.
4. Create an Experiment2_App.R script file outside the Experiment1 folder.

The server.R script is


library(shiny)
library(Quandl)
library(ggplot2)


shinyServer(function(input, output) {
 
  # Place this variable here. It will be run only once each time a user visits the app.
  # This makes the output unique to a particular user
  stkList <- data.frame(name=c("Microsoft", "Google"),symbol=c("GOOG/NASDAQ_MSFT","GOOG/NASDAQ_GOOGL" ),stringsAsFactors = FALSE)
 
  output$text1 <- renderText({
    paste("You have selected ",input$var)
  })
 
  #renderPlot function is required to plot the graph
  output$distPlot <- renderPlot({
   
    # Use grep to find the Quandl data code name
    # Place the Quandl function inside the render* function otherwise it does not become part of the
    # reactive component
    stkPrice <- Quandl(stkList[grep(input$var,stkList[,1], ignore.case = TRUE),2],
                       api_key="xxxxxxxxxxxxxxx",
                       trim_start=input$daterange[1],
                       trim_end=input$daterange[2],
                       order="asc")
   
    #ggplot qplot function
    qplot(stkPrice$Date,stkPrice$Close,
          geom=c("line","point"),
          xlab="Time",
          ylab=paste(input$var, " stock price"))

})
 
})
The ui.R script is

library(shiny)

shinyUI(fluidPage(
  titlePanel("Stock plotter with dateRangeInput reactive programming"),
 
  sidebarLayout(
    sidebarPanel(
      helpText("Stock plot using Quandl and Shiny"),
     
      selectInput("var",
                  label = "Choose a Microsoft or Google",
                  choices = c("Microsoft", "Google"),
                  selected = "Microsoft"),
     
      #dateRangeInput to select the dates required for the plotting
      dateRangeInput("daterange", "Date range:",
                     start  = "01-Jan-2015",
                     end    = "31-Jan-2016",
                     min    = "01-Jan-2000",
                     max    = "01-Jan-2017",
                     format = "dd-M-yyyy",
                     separator = " - ")
     
      ),
   
    mainPanel(
      textOutput("text1"),
      br(),
      plotOutput("distPlot")
    )
  )
))

The Experiment2_app.R script is

library(shiny)
runApp("Experiment2")

The output is rendered here

https://andrnev.shinyapps.io/Experiment2/

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