在 R 中从 Google 驱动器下载 xlsx 文件

Download xlsx file from Google Drive in R

我在 Google 云端硬盘上公开分享了一个小数据集,并且我已经让拥有 link 权限的任何人都可以访问该文件。

我希望将此文件下载到 R 中进行分析,但我无法从临时目录中解压缩文件。

我的代码如下所示:

install.packages("pacman")
library(pacman)
#Load Libraries
pacman::p_load(tidyverse,tidymodels,modeltime,timetk,googledrive)

temp <- tempfile(fileext = ".zip")

dl <- drive_download(
  as_id("https://drive.google.com/file/d/17ZhE3nxqtGYNzeADMzU02YzfKU9H9f5j/view?usp=sharing"),
  path = temp, 
  overwrite = TRUE, 
  type = "xlsx")

out <- unzip(temp, exdir = tempdir())

#Import Data
Three_Time_Series <- read_excel(out[1])

当我检查 out 变量时,我看到它是一个大小为 1:10 的字符向量,但每个字符串引用和 xml 文件。在最后一行中,我尝试阅读 out[1:10] 但每次它都说:

Error: Can't establish that the input is either xls or xlsx. 

如有任何提示,我们将不胜感激。

您拥有的是供查看的 URL,您应该获取 editing/downloading 文件的 URL。

以下适合我。

library(googledrive)

dl <- drive_download(
 as_id("https://docs.google.com/spreadsheets/d/17ZhE3nxqtGYNzeADMzU02YzfKU9H9f5j/edit#gid=1748893795"),
  path = 'temp1.xlsx', 
  overwrite = TRUE, 
  type = "xlsx")


Three_Time_Series <- readxl::read_excel('temp1.xlsx')
Three_Time_Series

# A tibble: 528 x 3                                                                                                
#   DATE_TIME           CELL  AVG_SIGNAL_LEVEL
#   <chr>               <chr>            <dbl>
# 1 04.21.2017 10:00:00 CELL1            -106.
# 2 04.21.2017 10:00:00 CELL2            -105.
# 3 04.21.2017 10:00:00 CELL3            -105.
# 4 04.21.2017 11:00:00 CELL1            -106.
# 5 04.21.2017 11:00:00 CELL3            -105.
# 6 04.21.2017 11:00:00 CELL2            -105.
# 7 04.21.2017 12:00:00 CELL2            -105.
# 8 04.21.2017 12:00:00 CELL1            -106.
# 9 04.21.2017 12:00:00 CELL3            -105.
#10 04.21.2017 13:00:00 CELL1            -106.
# … with 518 more rows