【发布时间】:2021-03-17 23:39:55
【问题描述】:
我在 R 中有一个下面提到的数据框,我使用 RMySQL 每隔 3 小时从数据库中获取它。
查询:
Select Form_No, Date, Name, Age from Test where Date(Date)>='2020-12-01';
DF:
Form_No Date Name Age
POS-1 2020-12-01 10:16:12 Alex 29
POS-2 2020-12-01 10:18:34 Mike 31
POS-3 2020-12-01 14:16:22 Paul 21
POS-4 2020-12-01 18:33:14 John 27
POS-5 2020-12-01 20:13:34 Cohe 45
当我运行脚本每 3 小时后,可能会在数据框中创建一些额外的条目,当我使用下面提到的代码在 googlesheet 中上传数据时,它会覆盖以前的信息并更新新的数据。
问题在于,对于每一行,信息的审阅者可能已经捕获了一些观察结果。
我使用的代码是:
library(dplyr)
library(stringr)
library(RMySQL)
library(googlesheets)
connection<-dbConnect(MySQL(), user='User', password='Password',
dbname='Db name',
host='host info')
Query<- paste0("Select Form_No, Date, Name, Age from Test where Date(Date)>='2020-12-01';")
DF <- dbGetQuery(connection,Query)
setwd("/home/path")
write.csv(x = DF, file = "cust_data.csv", row.names = FALSE)
as<-gs_title("TargetSheet")
gs_upload("cust_data.csv", sheet_title = "TargetSheet", overwrite = TRUE)
我希望在上传新一批信息时保持Form_No的顺序和每个细节相同。
例如,如果我在上午 06:00 运行查询并且有五个条目,如 DF 中所示,并且所有这些条目都上传到谷歌表格,那么现在如果我在上午 09:00 运行脚本,然后在我的查询有可能除了这五个条目之外还有更多条目。
我现在需要在 google sheet 中附加这些额外的条目,除了上次已经上传的五个条目。
【问题讨论】:
标签: r dataframe google-sheets dplyr r-googlesheets