[R] creating graphs using Rook
punitha
punitha.c87 at wipro.com
Tue Sep 18 08:12:08 CEST 2012
hi,
I m said to display graph in browser using Rook.
So i found a code i.e
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
PIC.DIR = paste(getwd(), 'pic', sep='/')
# define the web page form
newapp = function(env) {
req = Rook::Request$new(env)
res = Rook::Response$new()
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:\n')
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>')
myNormalize = function (target) {
return((target - min(target))/(max(target) - min(target)))
}
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,'Increase','Decrease'), 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)
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(" <",
s$full_url("pic"),
> ", sep = ""))
}
res$finish()
}
s = Rhttpd$new()
s$add(app = newapp, name = "visbin")
s$add(app = File$new(PIC.DIR), name = "pic")
s$start()
s$browse("visbin")
but when i execute this code i get an error saying
Error in grDevices::png(..., width = width, height = height, res = dpi, :
unable to start png() device
Or Is there any other code which can help me in displaying graph in Rook
using ggplot2
-----
Thank you,
with regards,
Punitha
--
View this message in context: http://r.789695.n4.nabble.com/creating-graphs-using-Rook-tp4643458.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list