美文网首页
Happy coding | 用R画了个时钟,该上班了!

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

作者: R语言与SPSS学习笔记 | 来源:发表于2021-10-22 18:21 被阅读0次

    来源: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

    相关文章

      网友评论

          本文标题:Happy coding | 用R画了个时钟,该上班了!

          本文链接:https://www.haomeiwen.com/subject/osrkoltx.html