R语言查询并连续可视化时间

获取当前时间

current_time <- Sys.time()
print(current_time)

使用 lubridate 包(如果未安装请先安装)

install.packages("lubridate")

创建一个简单的时钟显示,12点钟在上

library(plotrix)

12点钟在下方的基础时钟

plot_clock_12_bottom <- function() {
  current <- Sys.time()
  hours <- as.numeric(format(current, "%H"))
  minutes <- as.numeric(format(current, "%M"))
  seconds <- as.numeric(format(current, "%S"))
  
  # 转换为角度 - 12点钟在下方(0度)
  hour_angle <- (hours %% 12 + minutes/60) * 30  # 0度在12点钟位置
  minute_angle <- minutes * 6  # 0度在12点钟位置
  second_angle <- seconds * 6  # 0度在12点钟位置
  
  # 创建绘图区域
  plot(0, 0, 
       xlim = c(-1.2, 1.2), 
       ylim = c(-1.2, 1.2),
       type = "n", 
       axes = FALSE,
       xlab = "", 
       ylab = "",
       asp = 1,
       main = paste("时钟 - 12点钟在上\n", format(current, "%Y-%m-%d %H:%M:%S")))
  
  # 绘制时钟外圈
  draw.circle(0, 0, 1, border = "black", lwd = 2)
  
  # 绘制刻度 - 12点钟在下
  for(i in 0:11) {
    # 从12点(下方)开始,顺时针计算角度
    angle <- i * 30 * pi/180  # 0度在12点钟位置
    
    # 小时数字 - 12在下,3在左,6在上,9在右
    hour_num <- ifelse(i == 0, 12, i)
    
    # 计算数字位置
    text_x <- 0.85 * sin(angle)
    text_y <- 0.85 * cos(angle)
    
    # 绘制小时数字
    text(text_x, text_y, hour_num, cex = 1.2, font = 2)
    
    # 绘制小时刻度线
    inner_x <- 0.9 * sin(angle)
    inner_y <- 0.9 * cos(angle)
    outer_x <- 1.0 * sin(angle)
    outer_y <- 1.0 * cos(angle)
    segments(inner_x, inner_y, outer_x, outer_y, lwd = 2)
  }
  
  # 绘制时针
  hour_x <- 0.5 * sin(hour_angle * pi/180)
  hour_y <- 0.5 * cos(hour_angle * pi/180)
  arrows(0, 0, hour_x, hour_y, lwd = 4, col = "blue", length = 0.1)
  
  # 绘制分针
  minute_x <- 0.8 * sin(minute_angle * pi/180)
  minute_y <- 0.8 * cos(minute_angle * pi/180)
  arrows(0, 0, minute_x, minute_y, lwd = 3, col = "green", length = 0.1)
  
  # 绘制秒针
  second_x <- 0.9 * sin(second_angle * pi/180)
  second_y <- 0.9 * cos(second_angle * pi/180)
  arrows(0, 0, second_x, second_y, lwd = 2, col = "red", length = 0.1)
  
  # 添加中心点
  points(0, 0, pch = 19, cex = 1.5, col = "black")
}

# 显示时钟
plot_clock_12_bottom()
# 方法1:for循环,执行60次,每次间隔1秒
for (i in 1:60) {
  plot_clock_12_bottom() # 执行命令p
  Sys.sleep(1)  # 等待1秒
}
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容