簡體   English   中英

使用 R 包“googledrive”從我的 googledrive 加載 R 文件

[英]Use R package “googledrive” to load in R a file from my googledrive

我的谷歌驅動器中有一個 xlsx 文件。 它太大,所以它不會自動轉換為 googlesheet(這就是使用 googlesheets 包不起作用的原因)。 該文件很大,我什至無法通過在我的 googledrive 上單擊來預覽它。 查看它的唯一方法是下載為 .xlsx 。 雖然我可以將它作為 xlsx 文件加載,但我正在嘗試使用 googledrive 包。

到目前為止,我所擁有的是:

library(googledrive)
drive_find(n_max = 50)
drive_download("filename_without_extension.xlsx",type = "xlsx")

但我收到以下錯誤:

'file' 未標識至少一個 Drive 文件。

也許是我沒有指定文件在驅動器中的路徑。 例如:Work\\Data\\Project1\\filename.xlsx

你能給我一個關於如何在 R 中加載名為filename.xlsx的文件的想法,該文件嵌套在驅動器中?

我閱讀了文檔,但無法弄清楚如何做到這一點。提前致謝。

您應該能夠通過以下方式做到這一點:

library(googledrive)
drive_download("~/Work/Data/Project1/filename.xlsx")

type參數僅適用於 Google 原生電子表格,不適用於原始文件。

我想分享我的方法。
我這樣做是因為我一直在更新 xlsx 文件。 它是來自 ERP 的查詢結果。
因此,當我嘗試通過 googleDrive Id 執行此操作時,它給了我錯誤,因為每次 ERP 更新文件時,其 Id 都會更改。
這是我的上下文。 你的可以完全不同。 該文件每月僅更改 2 或 3 次。 即使很難,它是一個“大”xlsx 文件(78-80K 記錄,有 19 個因子),我只用它幾秒鍾來計算一些值,然后我就可以把它扔掉。 存儲它沒有任何意義。 (存儲比上傳更貴)

library(googledrive)    
library(googlesheets4) # watch out: it is not the CRAN version yet 0.1.1.9000

drive_folder_owner<-"carlos.sxxx@xxxxxx.com" # this is my account in this gDrive folder.
drive_auth(email =drive_folder_owner) # previously authorized account
googlesheets4::sheets_auth(email =drive_folder_owner) # Yes, I know, should be the same, but they are different. 


d1<-drive_find(pattern = "my_file.xlsx",type = drive_mime_type("xlsx")) # This is me finding the file created by the ERP, and I do shorten the search using the type
meta<-drive_get(id=d1$id)[["drive_resource"]] # Get the id from the file in googledrive
n_id<-glue("https://drive.google.com/open?id=",d1$id[[1]]) # here I am creating a path for reading
meta_name<- paste(getwd(),"/Files/",meta[[1]]$originalFilename,sep = "") # and a path to temporary save it.

drive_download(file=as_id(n_id),overwrite = TRUE, path = meta_name) # Now read and save locally.
V_CMV<-data.frame(read_xlsx(meta_name)) # store to data frame
file.remove(meta_name) # delete from R Server
rm(d1,n_id) # Delete temporary variables

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM