ron-r
3/5/2014 - 4:36 PM

http://binalytics.wordpress.com/2012/06/18/quick-web-app-with-r-rook-and-ggplot2-for-a-stock-symbol-candle-chart/

# Example script using Rook to build a simple dynamic web application, very simple
library(Rook) # for web functionality
library(ggplot2) # for graphing
library(tseries) # used to grab time series from yahoo for stock symbols
library(plyr) # data tweaks
 
# define the web page form
newapp = function(env) {
  req = Rook::Request$new(env)
  res = Rook::Response$new()
   
  # retain previous answer, if there was one
  if (!is.null(req$POST())) {
    stock.symbol <- req$POST()[["stock.symbol"]]
    day.window <- req$POST()[["day.window"]]
  } else {
    stock.symbol <- 'AAPL'
    day.window <- 60
  }
  res$write('What stock ticker would you like to see:\n')
  res$write('<BR/>')
  res$write('Stock Symbol: ')
  res$write('<form method="POST">\n')
  stock.input <- paste('<input type="text" name="stock.symbol" value="',stock.symbol,'">\n', sep='')
  res$write( stock.input ) 
  res$write('<form method="POST">\n')
  res$write('<input type="radio" name="day.window" value="30">30 Days \n')
  res$write('<input type="radio" name="day.window" value="60" checked>60 Days \n')
  res$write('<input type="radio" name="day.window" value="90">90 Days \n')
  res$write('<input type="submit" name="Go!">\n</form>\n<br>')
   
  if (!is.null(req$POST())) {
    # get the stock data as a data frame
    df <- as.data.frame(get.hist.quote(stock.symbol,start=as.character(Sys.Date() -  as.numeric(day.window)),quote=c("Open", "High", "Low", "Close")))
     
    # add an average and the top/bottom for the candle
    df <- mutate(df, Average =(High + Low + Close)/3, Bottom = pmin(Open, Close), Top = pmax(Open, Close), Open.to.Close = ifelse(sign(Open - Close) == 1,'Decrease','Increase'), Date = row.names(df), Date.Label = ifelse(weekdays(as.Date(row.names(df))) == 'Friday',row.names(df),'')) # this gets the date from row.names into a column
     
    # create a box plot
    my.plot <- ggplot(data=df, aes(x=Date, lower=Bottom, upper=Top, middle=Average,  ymin=Low,  ymax=High, color=Open.to.Close, fill=Open.to.Close), xlab='Date', ylab='Price') +
      geom_boxplot(stat='identity') +
      # add the line for average price from HCL
      geom_line(data=df, aes(x=Date,y=Average, group=0), color='black') +
      # tweak the labeling
      opts(axis.text.x = theme_text(angle=270), legend.position = 'top', legend.direction='horizontal') +
      scale_x_discrete(labels=df$Date.Label)
    # save to the new subdir
    ggsave(plot=my.plot, paste(PIC.DIR, "/pic", stock.symbol, day.window, ".png", sep = ""))
     
    res$write(paste(day.window,' days stock price trend for ',stock.symbol,'<BR/>', sep=''))
    res$write(paste("<img src='", 
                    s$full_url("pic"), 
                    '/pic', stock.symbol, day.window, ".png'", 
                    "width='650 px' height='650 px' />", sep = ""))
  }
  res$finish()
}
 
# create a pic subdir in the working dir
PIC.DIR = paste(getwd(), 'tmp', sep='/')
 
s = Rhttpd$new()
s$add(app = newapp, name = "stockCandle")
s$add(app = File$new(PIC.DIR), name = "pic")
s$start()
s$browse("stockCandle")