Happy coding | 用R画了个时钟,该上班了!

来源:Happy coding | 用R画了个时钟,该上班了!

最近上网的时候发现了用R画时钟的代码,觉得非常有趣,我就尝试了一下,还蛮好玩,代码如下:

require(grid)
install.packages("beepr")#若你已经安装过了不用重复安装

1.画圆形时钟

# 1.函数1
DrawClock <- function(hour, minute, second) {
  
  t <- seq(0, 2*pi, length=13)[-13]
  x <- cos(t)
  y <- sin(t)
  
  
  grid.newpage()
  pushViewport(dataViewport(x, y, gp=gpar(lwd=3)))
  
  # 时针的轮廓
  grid.circle(x=0, y=0, default="native", r=unit(1, "native"))
  
  # 时针
  hourAngle <- pi/2 - (hour + minute/60)/12*2*pi
  grid.segments(0, 0, 0.6*cos(hourAngle), .6*sin(hourAngle), default="native", gp=gpar(lex=4, col="red"))
  
  # 分针
  minuteAngle <- pi/2 - (minute)/60*2*pi
  grid.segments(0, 0, 0.8*cos(minuteAngle), .8*sin(minuteAngle),default="native", gp=gpar(lex=2))    
  
  # 秒针
  secondAngle <- pi/2 - (second)/60*2*pi
  grid.segments(0, 0, 
                0.8*cos(secondAngle), .7*sin(secondAngle), default="native", gp=gpar(lex=1, col = "blue"), draw=TRUE)    
  grid.circle(0,0, default="native", r=unit(1, "mm"), gp=gpar(fill="white"))
}

# 2.函数2
AnalogClock <- function() {
  while(TRUE){
    hh <- as.integer(format(Sys.time(), format="%H"))
    mm <- as.integer(format(Sys.time(), format="%M"))
    ss <- as.integer(format(Sys.time(), format="%S"))
    Sys.sleep(1)
    DrawClock(hh,mm,ss)
    beepr::beep(sound = 1, expr = NULL)
  }
}

# 运转的时钟
AnalogClock()

这是北京时间哦,我是晚上9点45画的时钟

方形时钟

#1.创建数字
n0 <-
  c("██████"
    ,"██  ██"
    ,"██  ██"
    ,"██  ██"
    ,"██████")

n1 <-
  c("    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██")

n2 <-
  c("██████"
    ,"    ██"
    ,"██████"
    ,"██    "
    ,"██████")

n3 <-
  c("██████"
    ,"    ██"
    ,"██████"
    ,"    ██"
    ,"██████")

n4 <-
  c("██  ██"
    ,"██  ██"
    ,"██████"
    ,"    ██"
    ,"    ██")

n5 <-
  c("██████"
    ,"██    "
    ,"██████"
    ,"    ██"
    ,"██████")

n6 <-
  c("██████"
    ,"██    "
    ,"██████"
    ,"██  ██"
    ,"██████")

n7 <-
  c("██████"
    ,"    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██")

n8 <-
  c("██████"
    ,"██  ██"
    ,"██████"
    ,"██  ██"
    ,"██████")

n9 <-
  c("██████"
    ,"██  ██"
    ,"██████"
    ,"    ██"
    ,"██████")

colon <-
  c("      "
    ,"  ██  "
    ,"      "
    ,"  ██  "
    ,"      ")

df0 <- as.data.frame(n0)
df1 <- as.data.frame(n1)
df2 <- as.data.frame(n2)
df3 <- as.data.frame(n3)
df4 <- as.data.frame(n4)
df5 <- as.data.frame(n5)
df6 <- as.data.frame(n6)
df7 <- as.data.frame(n7)
df8 <- as.data.frame(n8)
df9 <- as.data.frame(n9)
dfc <- as.data.frame(colon)

numbers <- cbind(df0, df1,df2,df3,df4,df5,df6,df7,df8,df9, dfc)
rm(df0, df1,df2,df3,df4,df5,df6,df7,df8,df9, dfc,n0,n1,n2,n3,n4,n5,n6,n7,n8,n9, colon)

# 2.从数据框中获取数据
getVariable <- function(x) {
  stopifnot(is.numeric(x))
  if (x == 0) {return (numbers$n0)}
  if (x == 1) {return (numbers$n1)}
  if (x == 2) {return (numbers$n2)}
  if (x == 3) {return (numbers$n3)}
  if (x == 4) {return (numbers$n4)}
  if (x == 5) {return (numbers$n5)}
  if (x == 6) {return (numbers$n6)}
  if (x == 7) {return (numbers$n7)}
  if (x == 8) {return (numbers$n8)}
  if (x == 9) {return (numbers$n9)}
}


BigDitigalClock <- function() {
  
  while(TRUE){
    Sys.sleep(1)
    cat("\014")
    
    #时
    h1 <- substr(strftime(Sys.time(), format="%H"),1,1)
    h2 <- substr(strftime(Sys.time(), format="%H"),2,2)
    
    #分
    m1 <- substr(strftime(Sys.time(), format="%M"),1,1)
    m2 <- substr(strftime(Sys.time(), format="%M"),2,2)
    
    #秒
    s1 <- substr(strftime(Sys.time(), format="%S"),1,1)
    s2 <- substr(strftime(Sys.time(), format="%S"),2,2)
    
    dfh1 <- as.data.frame(getVariable(as.integer(h1)))
    dfh2 <- as.data.frame(getVariable(as.integer(h2)))
    dfm1 <- as.data.frame(getVariable(as.integer(m1)))
    dfm2 <- as.data.frame(getVariable(as.integer(m2)))
    dfs1 <- as.data.frame(getVariable(as.integer(s1)))
    dfs2 <- as.data.frame(getVariable(as.integer(s2)))
    
    current_time <- cbind(dfh1, dfh2, numbers$colon, 
                          dfm1, dfm2 , numbers$colon,
                          dfs1, dfs2)
    
    #删除行名和列明
    colnames(current_time) <- c(" "," "," "," "," "," "," "," ")
    print.data.frame(current_time,  row.names = F)
  }
}

# 转动时钟
BigDitigalClock()

happy coding!祝大家学R学得开心!欢迎大家关注我们的公众号。

代码来源:>最近上网的时候发现了用R画时钟的代码,觉得非常有趣,我就尝试了一下,还蛮好玩,代码如下:

require(grid)
install.packages("beepr")#若你已经安装过了不用重复安装

1.画圆形时钟

# 1.函数1
DrawClock <- function(hour, minute, second) {
  
  t <- seq(0, 2*pi, length=13)[-13]
  x <- cos(t)
  y <- sin(t)
  
  
  grid.newpage()
  pushViewport(dataViewport(x, y, gp=gpar(lwd=3)))
  
  # 时针的轮廓
  grid.circle(x=0, y=0, default="native", r=unit(1, "native"))
  
  # 时针
  hourAngle <- pi/2 - (hour + minute/60)/12*2*pi
  grid.segments(0, 0, 0.6*cos(hourAngle), .6*sin(hourAngle), default="native", gp=gpar(lex=4, col="red"))
  
  # 分针
  minuteAngle <- pi/2 - (minute)/60*2*pi
  grid.segments(0, 0, 0.8*cos(minuteAngle), .8*sin(minuteAngle),default="native", gp=gpar(lex=2))    
  
  # 秒针
  secondAngle <- pi/2 - (second)/60*2*pi
  grid.segments(0, 0, 
                0.8*cos(secondAngle), .7*sin(secondAngle), default="native", gp=gpar(lex=1, col = "blue"), draw=TRUE)    
  grid.circle(0,0, default="native", r=unit(1, "mm"), gp=gpar(fill="white"))
}

# 2.函数2
AnalogClock <- function() {
  while(TRUE){
    hh <- as.integer(format(Sys.time(), format="%H"))
    mm <- as.integer(format(Sys.time(), format="%M"))
    ss <- as.integer(format(Sys.time(), format="%S"))
    Sys.sleep(1)
    DrawClock(hh,mm,ss)
    beepr::beep(sound = 1, expr = NULL)
  }
}

# 运转的时钟
AnalogClock()

这是北京时间哦,我是晚上9点45画的时钟

方形时钟

#1.创建数字
n0 <-
  c("██████"
    ,"██  ██"
    ,"██  ██"
    ,"██  ██"
    ,"██████")

n1 <-
  c("    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██")

n2 <-
  c("██████"
    ,"    ██"
    ,"██████"
    ,"██    "
    ,"██████")

n3 <-
  c("██████"
    ,"    ██"
    ,"██████"
    ,"    ██"
    ,"██████")

n4 <-
  c("██  ██"
    ,"██  ██"
    ,"██████"
    ,"    ██"
    ,"    ██")

n5 <-
  c("██████"
    ,"██    "
    ,"██████"
    ,"    ██"
    ,"██████")

n6 <-
  c("██████"
    ,"██    "
    ,"██████"
    ,"██  ██"
    ,"██████")

n7 <-
  c("██████"
    ,"    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██")

n8 <-
  c("██████"
    ,"██  ██"
    ,"██████"
    ,"██  ██"
    ,"██████")

n9 <-
  c("██████"
    ,"██  ██"
    ,"██████"
    ,"    ██"
    ,"██████")

colon <-
  c("      "
    ,"  ██  "
    ,"      "
    ,"  ██  "
    ,"      ")

df0 <- as.data.frame(n0)
df1 <- as.data.frame(n1)
df2 <- as.data.frame(n2)
df3 <- as.data.frame(n3)
df4 <- as.data.frame(n4)
df5 <- as.data.frame(n5)
df6 <- as.data.frame(n6)
df7 <- as.data.frame(n7)
df8 <- as.data.frame(n8)
df9 <- as.data.frame(n9)
dfc <- as.data.frame(colon)

numbers <- cbind(df0, df1,df2,df3,df4,df5,df6,df7,df8,df9, dfc)
rm(df0, df1,df2,df3,df4,df5,df6,df7,df8,df9, dfc,n0,n1,n2,n3,n4,n5,n6,n7,n8,n9, colon)

# 2.从数据框中获取数据
getVariable <- function(x) {
  stopifnot(is.numeric(x))
  if (x == 0) {return (numbers$n0)}
  if (x == 1) {return (numbers$n1)}
  if (x == 2) {return (numbers$n2)}
  if (x == 3) {return (numbers$n3)}
  if (x == 4) {return (numbers$n4)}
  if (x == 5) {return (numbers$n5)}
  if (x == 6) {return (numbers$n6)}
  if (x == 7) {return (numbers$n7)}
  if (x == 8) {return (numbers$n8)}
  if (x == 9) {return (numbers$n9)}
}


BigDitigalClock <- function() {
  
  while(TRUE){
    Sys.sleep(1)
    cat("\014")
    
    #时
    h1 <- substr(strftime(Sys.time(), format="%H"),1,1)
    h2 <- substr(strftime(Sys.time(), format="%H"),2,2)
    
    #分
    m1 <- substr(strftime(Sys.time(), format="%M"),1,1)
    m2 <- substr(strftime(Sys.time(), format="%M"),2,2)
    
    #秒
    s1 <- substr(strftime(Sys.time(), format="%S"),1,1)
    s2 <- substr(strftime(Sys.time(), format="%S"),2,2)
    
    dfh1 <- as.data.frame(getVariable(as.integer(h1)))
    dfh2 <- as.data.frame(getVariable(as.integer(h2)))
    dfm1 <- as.data.frame(getVariable(as.integer(m1)))
    dfm2 <- as.data.frame(getVariable(as.integer(m2)))
    dfs1 <- as.data.frame(getVariable(as.integer(s1)))
    dfs2 <- as.data.frame(getVariable(as.integer(s2)))
    
    current_time <- cbind(dfh1, dfh2, numbers$colon, 
                          dfm1, dfm2 , numbers$colon,
                          dfs1, dfs2)
    
    #删除行名和列明
    colnames(current_time) <- c(" "," "," "," "," "," "," "," ")
    print.data.frame(current_time,  row.names = F)
  }
}

# 转动时钟
BigDitigalClock()

happy coding!祝大家学R学得开心!欢迎大家关注我们的公众号。
代码来源:>最近上网的时候发现了用R画时钟的代码,觉得非常有趣,我就尝试了一下,还蛮好玩,代码如下:

require(grid)
install.packages("beepr")#若你已经安装过了不用重复安装

1.画圆形时钟

# 1.函数1
DrawClock <- function(hour, minute, second) {
  
  t <- seq(0, 2*pi, length=13)[-13]
  x <- cos(t)
  y <- sin(t)
  
  
  grid.newpage()
  pushViewport(dataViewport(x, y, gp=gpar(lwd=3)))
  
  # 时针的轮廓
  grid.circle(x=0, y=0, default="native", r=unit(1, "native"))
  
  # 时针
  hourAngle <- pi/2 - (hour + minute/60)/12*2*pi
  grid.segments(0, 0, 0.6*cos(hourAngle), .6*sin(hourAngle), default="native", gp=gpar(lex=4, col="red"))
  
  # 分针
  minuteAngle <- pi/2 - (minute)/60*2*pi
  grid.segments(0, 0, 0.8*cos(minuteAngle), .8*sin(minuteAngle),default="native", gp=gpar(lex=2))    
  
  # 秒针
  secondAngle <- pi/2 - (second)/60*2*pi
  grid.segments(0, 0, 
                0.8*cos(secondAngle), .7*sin(secondAngle), default="native", gp=gpar(lex=1, col = "blue"), draw=TRUE)    
  grid.circle(0,0, default="native", r=unit(1, "mm"), gp=gpar(fill="white"))
}

# 2.函数2
AnalogClock <- function() {
  while(TRUE){
    hh <- as.integer(format(Sys.time(), format="%H"))
    mm <- as.integer(format(Sys.time(), format="%M"))
    ss <- as.integer(format(Sys.time(), format="%S"))
    Sys.sleep(1)
    DrawClock(hh,mm,ss)
    beepr::beep(sound = 1, expr = NULL)
  }
}

# 运转的时钟
AnalogClock()

这是北京时间哦,我是晚上9点45画的时钟

方形时钟

#1.创建数字
n0 <-
  c("██████"
    ,"██  ██"
    ,"██  ██"
    ,"██  ██"
    ,"██████")

n1 <-
  c("    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██")

n2 <-
  c("██████"
    ,"    ██"
    ,"██████"
    ,"██    "
    ,"██████")

n3 <-
  c("██████"
    ,"    ██"
    ,"██████"
    ,"    ██"
    ,"██████")

n4 <-
  c("██  ██"
    ,"██  ██"
    ,"██████"
    ,"    ██"
    ,"    ██")

n5 <-
  c("██████"
    ,"██    "
    ,"██████"
    ,"    ██"
    ,"██████")

n6 <-
  c("██████"
    ,"██    "
    ,"██████"
    ,"██  ██"
    ,"██████")

n7 <-
  c("██████"
    ,"    ██"
    ,"    ██"
    ,"    ██"
    ,"    ██")

n8 <-
  c("██████"
    ,"██  ██"
    ,"██████"
    ,"██  ██"
    ,"██████")

n9 <-
  c("██████"
    ,"██  ██"
    ,"██████"
    ,"    ██"
    ,"██████")

colon <-
  c("      "
    ,"  ██  "
    ,"      "
    ,"  ██  "
    ,"      ")

df0 <- as.data.frame(n0)
df1 <- as.data.frame(n1)
df2 <- as.data.frame(n2)
df3 <- as.data.frame(n3)
df4 <- as.data.frame(n4)
df5 <- as.data.frame(n5)
df6 <- as.data.frame(n6)
df7 <- as.data.frame(n7)
df8 <- as.data.frame(n8)
df9 <- as.data.frame(n9)
dfc <- as.data.frame(colon)

numbers <- cbind(df0, df1,df2,df3,df4,df5,df6,df7,df8,df9, dfc)
rm(df0, df1,df2,df3,df4,df5,df6,df7,df8,df9, dfc,n0,n1,n2,n3,n4,n5,n6,n7,n8,n9, colon)

# 2.从数据框中获取数据
getVariable <- function(x) {
  stopifnot(is.numeric(x))
  if (x == 0) {return (numbers$n0)}
  if (x == 1) {return (numbers$n1)}
  if (x == 2) {return (numbers$n2)}
  if (x == 3) {return (numbers$n3)}
  if (x == 4) {return (numbers$n4)}
  if (x == 5) {return (numbers$n5)}
  if (x == 6) {return (numbers$n6)}
  if (x == 7) {return (numbers$n7)}
  if (x == 8) {return (numbers$n8)}
  if (x == 9) {return (numbers$n9)}
}


BigDitigalClock <- function() {
  
  while(TRUE){
    Sys.sleep(1)
    cat("\014")
    
    #时
    h1 <- substr(strftime(Sys.time(), format="%H"),1,1)
    h2 <- substr(strftime(Sys.time(), format="%H"),2,2)
    
    #分
    m1 <- substr(strftime(Sys.time(), format="%M"),1,1)
    m2 <- substr(strftime(Sys.time(), format="%M"),2,2)
    
    #秒
    s1 <- substr(strftime(Sys.time(), format="%S"),1,1)
    s2 <- substr(strftime(Sys.time(), format="%S"),2,2)
    
    dfh1 <- as.data.frame(getVariable(as.integer(h1)))
    dfh2 <- as.data.frame(getVariable(as.integer(h2)))
    dfm1 <- as.data.frame(getVariable(as.integer(m1)))
    dfm2 <- as.data.frame(getVariable(as.integer(m2)))
    dfs1 <- as.data.frame(getVariable(as.integer(s1)))
    dfs2 <- as.data.frame(getVariable(as.integer(s2)))
    
    current_time <- cbind(dfh1, dfh2, numbers$colon, 
                          dfm1, dfm2 , numbers$colon,
                          dfs1, dfs2)
    
    #删除行名和列明
    colnames(current_time) <- c(" "," "," "," "," "," "," "," ")
    print.data.frame(current_time,  row.names = F)
  }
}

# 转动时钟
BigDitigalClock()

happy coding!祝大家学R学得开心!欢迎大家关注我们的公众号。
代码来源:https://github.com/tomaztk/Useless_R_functions

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容