r - 在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?
任何帮助表示赞赏!
解决方案
似乎您的 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()
}
}
推荐阅读
- java - 这个语法在 Kotlin 中是什么意思?
- html - 在多个多边形 SVG 中定位文本
- java - java.time.format.DateTimeParseException:无法解析文本“2019-02-16 09:29:32.959”,在索引 10 处找到未解析的文本
- vb.net - 对非共享成员的引用需要对象引用尝试在 VB.net 中传递对象
- php - 使用 preg 替换获取完整的图像链接
- apache-spark - Hive-On-Spark - 执行后运行查询时出错。引擎更改为 Spark
- bazel - 如何增加重试次数或延长抓取时间?
- java - 打印字符串数组中的所有名称
- apache - 在 Apache 上的 LDAP 身份验证后传递用户名
- jquery - 使用 html ids 查询 mongoDB 文档