强大的R
来源:互联网 发布:unity3d游戏开发流程 编辑:程序博客网 时间:2024/05/17 11:58
说是原创有点勉强。应该是理解原创。
文章大部分来自
http://blog.fens.me/series/
本人非常震惊作者对R的熟悉程度。也发现以前对R的应用领域的看法有些狭隘。
library(plyr)
library(zoo)
library(xts)
library(TTR)
library(quantmod)
library(ggplot2)
library(scales)
download <- function(stock,from="2010-01-01"){
df <- getSymbols(stock,from = from,env = environment(),auto.assign = FALSE)
names(df)<-c("Open","High","Low","Close","Volume","Adjusted")
write.zoo(df,file = paste(stock,".csv",sep=""),sep=",",quote=FALSE)
}
read <- function(stock){
as.xts(read.zoo(file=paste(stock,".csv",sep=""),header = TRUE,sep=",", format="%Y-%m-%d"))
}
stock<-"IBM"
download(stock,from="2010-01-01")
IBM<-read(stock)
class(IBM)
head(IBM)
chartSeries(IBM)
运行截图:
R贪吃蛇
init<-function(){
e<<-new.env()
e$stage<-0 #场景
e$width<-e$height<-20 #切分格子
e$step<-1/e$width #步长
e$m<-matrix(rep(0,e$width*e$height),nrow=e$width) #点矩阵
e$dir<-e$lastd<-'up' # 移动方向
e$head<-c(2,2) #初始蛇头
e$lastx<-e$lasty<-2 # 初始化蛇头上一个点
e$tail<-data.frame(x=c(),y=c())#初始蛇尾
e$col_furit<-2 #水果颜色
e$col_head<-4 #蛇头颜色
e$col_tail<-8 #蛇尾颜色
e$col_path<-0 #路颜色
}
# 获得矩阵的索引值
index<-function(col) which(e$m==col)
# 游戏中
stage1<-function(){
e$stage<-1
# 随机的水果点
furit<-function(){
if(length(index(e$col_furit))<=0){ #不存在水果
idx<-sample(index(e$col_path),1)
fx<-ifelse(idx%%e$width==0,10,idx%%e$width)
fy<-ceiling(idx/e$height)
e$m[fx,fy]<-e$col_furit
print(paste("furit idx",idx))
print(paste("furit axis:",fx,fy))
}
}
# 检查失败
fail<-function(){
# head出边界
if(length(which(e$head<1))>0 | length(which(e$head>e$width))>0){
print("game over: Out of ledge.")
keydown('q')
return(TRUE)
}
# head碰到tail
if(e$m[e$head[1],e$head[2]]==e$col_tail){
print("game over: head hit tail")
keydown('q')
return(TRUE)
}
return(FALSE)
}
# snake head
head<-function(){
e$lastx<-e$head[1]
e$lasty<-e$head[2]
# 方向操作
if(e$dir=='up') e$head[2]<-e$head[2]+1
if(e$dir=='down') e$head[2]<-e$head[2]-1
if(e$dir=='left') e$head[1]<-e$head[1]-1
if(e$dir=='right') e$head[1]<-e$head[1]+1
}
# snake body
body<-function(){
e$m[e$lastx,e$lasty]<-0
e$m[e$head[1],e$head[2]]<-e$col_head #snake
if(length(index(e$col_furit))<=0){ #不存在水果
e$tail<-rbind(e$tail,data.frame(x=e$lastx,y=e$lasty))
}
if(nrow(e$tail)>0) { #如果有尾巴
e$tail<-rbind(e$tail,data.frame(x=e$lastx,y=e$lasty))
e$m[e$tail[1,]$x,e$tail[1,]$y]<-e$col_path
e$tail<-e$tail[-1,]
e$m[e$lastx,e$lasty]<-e$col_tail
}
print(paste("snake idx",index(e$col_head)))
print(paste("snake axis:",e$head[1],e$head[2]))
}
# 画布背景
drawTable<-function(){
plot(0,0,xlim=c(0,1),ylim=c(0,1),type='n',xaxs="i", yaxs="i")
}
# 根据矩阵画数据
drawMatrix<-function(){
idx<-which(e$m>0)
px<- (ifelse(idx%%e$width==0,e$width,idx%%e$width)-1)/e$width+e$step/2
py<- (ceiling(idx/e$height)-1)/e$height+e$step/2
pxy<-data.frame(x=px,y=py,col=e$m[idx])
points(pxy$x,pxy$y,col=pxy$col,pch=15,cex=4.4)
}
furit()
head()
if(!fail()){
body()
drawTable()
drawMatrix()
}
}
# 开机画图
stage0<-function(){
e$stage<-0
plot(0,0,xlim=c(0,1),ylim=c(0,1),type='n',xaxs="i", yaxs="i")
text(0.5,0.7,label="Snake Game",cex=5)
text(0.5,0.4,label="Any keyboard to start",cex=2,col=4)
text(0.5,0.3,label="Up,Down,Left,Rigth to control direction",cex=2,col=2)
text(0.2,0.05,label="Author:DanZhang",cex=1)
text(0.5,0.05,label="http://blog.fens.me",cex=1)
}
# 结束画图
stage2<-function(){
e$stage<-2
plot(0,0,xlim=c(0,1),ylim=c(0,1),type='n',xaxs="i", yaxs="i")
text(0.5,0.7,label="Game Over",cex=5)
text(0.5,0.4,label="Space to restart, q to quit.",cex=2,col=4)
text(0.5,0.3,label=paste("Congratulations! You have eat",nrow(e$tail),"fruits!"),cex=2,col=2)
text(0.2,0.05,label="Author:DanZhang",cex=1)
text(0.5,0.05,label="http://blog.fens.me",cex=1)
}
# 键盘事件
keydown<-function(K){
print(paste("keydown:",K,",stage:",e$stage));
if(e$stage==0){ #开机画面
init()
stage1()
return(NULL)
}
if(e$stage==2){ #结束画面
if(K=="q") q()
else if(K==' ') stage0()
return(NULL)
}
if(e$stage==1){ #游戏中
if(K == "q") {
stage2()
} else {
if(tolower(K) %in% c("up","down","left","right")){
e$lastd<-e$dir
e$dir<-tolower(K)
stage1()
}
}
}
return(NULL)
}
#######################################
# RUN
#######################################
run<-function(){
par(mai=rep(0,4),oma=rep(0,4))
e<<-new.env()
stage0()
# 注册事件
getGraphicsEvent(prompt="Snake Game",onKeybd=keydown)
}
run()
作者文章还有R编写网络程序。链接为
http://blog.fens.me/series/
- 强大的R
- R-Studio,一款强大的数据恢复工具
- 【R语言 字符串处理】stringr 包的强大之处
- R语言可视化---介绍一些强大的画图和数据处理包及函数(ggplot,melt())
- 二十三、R语言强大工具包ggplot绘图以外的那些事
- R语言绘图功能之强大
- 强大的是更强大的战斗力
- 一次强大的疲倦
- 强大的ORACLE CLIENT
- 强大的ASCII码表
- typedef的强大功能
- 强大的IWEB接口
- 强大的Perl
- 认识强大的WebDAV
- 强大的using语句
- 强大的axel!
- 强大的SPGridView
- SMARTY强大的IF
- IO File学习
- 标准C++中的string类的用法总结
- com引用计数
- LeetCode OJ算法题(十四):Longest Common Prefix
- mysql最基本也是最常用的几个命令
- 强大的R
- was cached in the local repository, resolution will not be reattempted until the update interval of
- 大神求解
- usaco2.4.6分数化小数
- 黑马程序员-static关键字的小总结
- Could not execute JDBC batch update
- Plan
- Android在初始化时弹出popwindow的方法
- com关于GUID