在 R 中的重复循环内的指定时间生成数据摘要的 PDF

Generate PDF of data summaries at specified times within repeat loop in R

我在 R 中使用重复循环来对连续摄取的数据执行数据整理和分析,这是我对其执行自动化和接近 real-time 分析的方式。在重复循环中,系统休眠 60 秒,然后重新启动。我想要这个重复循环函数中的一些代码,这些代码将在指定时间生成数据摘要的 PDF;假设是午夜。

我将使用 R 中的 mpg 数据集作为一个非常基本的示例(无法共享我的实际数据集,因为它是 sensitive 数据。这里会发生的是每 60 秒生成一个散点图(相同的),只是标题更改为包含当前系统时间,只是为了表明它正在工作:-

interval=60 # system sleep time of 60 seconds

library(ggplot2)
repeat{

  currenttime<-Sys.time()
  
p<-ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point()+ggtitle(paste0("graph made at: ",currenttime))# adds on the current time


plot(p)
print(Sys.time())# a check to see if it is working



  Sys.sleep(interval)# system sleeps for 60 seconds then repeats 
}

我想做的是在其中添加额外的代码,当系统时间到达新一天的午夜后,将生成包含数据摘要的 PDF;对于我的 real 数据集,这将是前一天数据的摘要。但出于所有意图和目的,让我们坚持使用 mpg 数据集。包括为您提供 PDF 输出的代码。

数据汇总和PDF

library(gridExtra)
library(dplyr)
datasummary<-mpg%>%
  group_by(manufacturer)%>%
  summarise(Count=n(), MeanDispl=mean(displ))

pdf("datasummary.pdf", height = 11, width = 8.5)
grid.table(datasummary)
dev.off()

这在重复循环之外有效。

这是我尝试过但不起作用的一种解决方案。


interval=60 # system sleep time of 60 seconds

library(ggplot2)
repeat{

  currenttime<-Sys.time()
  
p<-ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point()+ggtitle(paste0("graph made at: ",currenttime))# adds on the current time


plot(p)
print(Sys.time())# a check to see if it is working



  Sys.sleep(interval)# system sleeps for 60 seconds then repeats 


reporttime<-as.ITime(Sys.time())
    
    if(reporttime>as.Date("00:00:00") &  reporttime<as.Date("00:03:00")){
      
      datasummary<-mpg%>%
      group_by(manufacturer)%>%
      summarise(Count=n(), MeanDispl=mean(displ))

      pdf("datasummary.pdf", height = 11, width = 8.5)
      grid.table(dailyreport)
      dev.off()
      
    }
}

我把它放在“00:00:00”和“00:03:00”之间,以确保我不会因为 interval 而错过这个 window。如何根据我指定的时间触发数据摘要PDF?

感谢任何帮助!

您的 if 条件似乎未正确评估。至少对我来说,这个 data.table::as.ITime(Sys.time()) > as.Date("00:00:00") 会产生错误。

如果您以不同方式设置 if 条件,您的代码似乎可以正常工作。请注意,为了使其更加透明,只要当前时间在给定的时间间隔内(在本例中为:starttime + 15s),现在就会写出名称中带有时间戳的多个文件。

#libraries
library(ggplot2)
library(dplyr)
library(gridExtra)

#set time interval around current time (+ 15s) to make it reproducible
a <- strftime(Sys.time(), format="%H:%M:%S")
z <- strftime(Sys.time() + 15, format="%H:%M:%S")

#system sleep time set to 5 seconds
interval <- 5 

repeat{
  
  currenttime<-Sys.time()
  
  p<-ggplot(mpg, aes(displ, hwy, colour = class)) + 
    geom_point()+ggtitle(paste0("graph made at: ",currenttime))# adds on the current time
  plot(p)
  print(Sys.time())# a check to see if it is working
  
  Sys.sleep(interval)# system sleeps
  
  if(currenttime > as.POSIXct(a, format="%H:%M:%S")  & 
     currenttime < as.POSIXct(z, format="%H:%M:%S")){

    datasummary<-mpg%>%
      group_by(manufacturer)%>%
      summarise(Count=n(), MeanDispl=mean(displ))
    
    pdf(file = paste0("datasummary_", strftime(currenttime, format="%H-%M-%S"), ".pdf"), 
        height = 11, width = 8.5)
    grid.table(datasummary)
    dev.off()
  }
}