rweb-scrapinggoogle-drive-apigooglesheets4

How to update Google sheet in R


I want to web scrape some data from a web page daily. I need to save it on a Google sheet. Then I need to update this data set daily (by running the Web scraping code) and update the same Google sheet with new data, replacing the old data. I do not want to create a new Google sheets during the updating process.

This is the data frame that I need to save in the Google Sheet.

# creating a data frame including all the information insert rooms, surface, cost and address
df_two <- cbind.data.frame(flat_cl_one$apt_link,flat_cl_one$rooms, flat_rm, flat_cl_one$surface,
                           flat_sf, flat_cl_one$cost, flat_ct, address, flat_ad)
colnames(df_two) <- c("apt_link", "rooms", "rooms clean", "Surface", "surface clean", 
                      "cost", "cost clean", "address", "address district" )

This is how I save this data frame as a CSV file in "my documents" folder in the computer.

# Saving
con <- file('Real_Estate_Wien_Data.csv',encoding="UTF-8")
write.csv(df_two, file=con, row.names = T)

This is how I tried to upload this data to the drive

# Write Google sheets
library(googlesheets4)
library(googledrive)
drive_auth()
1
# Link to the folder
tf <- drive_get("~/Real_Estate_Wien_Data.csv")
# Update
drive_put('Real_Estate_Wien_Data.csv', name = "Real_Estate_Wien_Data", type="spreadsheet", path=as_id(td)) # keeps id because of other links

But I keep getting this error

Error: Parent specified via path is invalid: x Is neither a folder nor a shared drive. Run rlang::last_error() to see where the error occurred.

Thank You in advance for having a look at this and recommending me a solution?


Solution

  • I figured out the problem. I got this error because I didn't create a separate folder in my Google Drive to put the data. I assigned the URL to "td" the newly created folder in my Google Drive. Then assign that variable to the "path" in the function "drive_put". It worked perfectly.