[英]How to NOT write_csv if data frame is empty
I have a dataframe that is gathered everyday via a sql query.我有一个 dataframe 每天通过 sql 查询收集。 Sometimes it'll have rows in it, sometimes it wont.有时它会有行,有时它不会。 I then write_csv it into a onedrive location which triggers an automated email.然后我将其 write_csv 写入一个触发自动 email 的 onedrive 位置。
df and code like this if relevant: df 和这样的代码(如果相关):
df<-structure(list(PROTOCOL_ID = numeric(0), PROTOCOL_NO = character(0),
STATUS = character(0), STATUS_DATE = structure(numeric(0), tzone = "", class = c("POSIXct",
"POSIXt")), PROCESSED_FLAG = character(0), INITIATOR_CODE = numeric(0),
CHANGE_REASON_CODE = numeric(0), PR_STATUS_ID = numeric(0),
COMMENTS = character(0), CREATED_DATE = structure(numeric(0), tzone = "", class = c("POSIXct",
"POSIXt")), CREATED_USER = character(0), MODIFIED_DATE = structure(numeric(0), tzone = "", class = c("POSIXct",
"POSIXt")), MODIFIED_USER = character(0), OUTCOME_ID = numeric(0),
IRB_NO = character(0), NCT_NUMBER = character(0), PI_NAMES = character(0)), row.names = integer(0), class = "data.frame")
write_csv(df, "df.csv")
If the dataframe has zero rows that day, I'd rather it DIDN'T write the csv.如果 dataframe 当天的行数为零,我宁愿它不写 csv。 I'm sure I could figure out a step that deletes the data frame if empty and then the write_csv line would error, but I'd rather not do that.我敢肯定,我可以想出一个步骤,如果为空,则删除数据框,然后 write_csv 行会出错,但我宁愿不这样做。 Is there an easy way to 'turn off' the write?有没有一种简单的方法可以“关闭”写入?
We could have a condition to only write to csv when the number of rows is greater than 0当行数大于 0 时,我们可以有一个条件只写入 csv
if(nrow(df) > 0) readr::write_csv(df, "df.csv")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.