次のコードはRook
、 で構築された株価ローソク足チャートをプロットするための非常に単純な webapp を構築するために使用しggplot2
ます。これは、 Jeff Horner による元の例と同じパターンに従います。
スクリプトを実行したときに RStudio に表示されるエラー メッセージは次のとおりです。
Warning Message: In Multipart$parse(env) : bad content body
私が間違っていて、私のウェブページの画像を失っているアイデアはありますか?
(割り当てに更新my.plot <- ggplot
)
( を含むように更新ggplot(plot=my.plot, ...)
)
require(Rook) # for web functionality
require(ggplot2) # for graphing
require(tseries) # used to grab time series from yahoo for stock symbols
require(plyr) # data tweaks
# define the web page form
newapp = function(env) {
req = Rook::Request$new(env)
res = Rook::Response$new()
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')
res$write('<input type="text" name="stock.symbol" value="AAPL"> \n')
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())) {
stock.symbol <- req$POST()[["stock.symbol"]]
day.window <- req$POST()[["day.window"]]
# 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("/tmp/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"), stock.symbol, day.window, ".png'", " />", sep = ""))
}
res$finish()
}
s = Rhttpd$new()
s$add(app = newapp, name = "visbin")
s$add(app = File$new("/tmp"), name = "pic")
s$start()
s$browse("visbin")