http://stackoverflow.com/questions/11365857/real-time-auto-updating-incremental-plot-in-r
n=1000 df=data.frame(time=1:n,y=runif(n)) window=100 for(i in 1:(n-window)) { flush.console() plot(df$time,df$y,type='l',xlim=c(i,i+window)) Sys.sleep(.09) }
任意のデータが来ても大丈夫なように拡張した(ただし初期値(要素数:20)が必要)
realtime.R
n=20 window=20 update = 1.0; df=data.frame(time=1:n,y=runif(n)) #initial data(if any) #df <- data.frame(); i = 0; while(TRUE) { flush.console() df <- rbind(df,data.frame(time=nrow(df)+1,y=runif(1))) plot(df$time,df$y,type='l',xlim=c(i,i+window)) i <- i+1; Sys.sleep(update) }
RPy2でPythonに渡すとこうなる
realtime_draw.py
from rpy2.robjects import r r('source("realtime.R")')
初期値がない場合はこっち
window=20; update = 1.0; #n=20;df=data.frame(time=1:n,y=runif(n)) #initial data(if any) n=0; df <- data.frame(); i = 0; while(TRUE) { flush.console() df <- rbind(df,data.frame(time=nrow(df)+1,y=runif(1))) if(nrow(df) > window){ plot(df$time,df$y,type='l',xlim=c(i-window,i)) } else { plot(df$time,df$y,type='l',xlim=c(0,window)) } i <- i+1; Sys.sleep(update) }