首页 > 解决方案 > 在R中的重复循环内的指定时间生成数据摘要的PDF

问题描述

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

我将mpg在 R 中使用此处的数据集作为一个非常基本的示例(不能共享我的实际数据集,因为它是敏感数据。这里会发生的是每 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;使用我的真实数据集,这将是前一天数据的摘要。但出于所有意图和目的,让我们坚持使用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. 如何根据我指定的时间触发数据摘要 PDF?

任何帮助表示赞赏!

标签: rpdfggplot2dplyrrepeat

解决方案


似乎您的 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()
  }
}

推荐阅读