r - 为什么 `download.file()` 从有效的公共 Dropbox 链接下载损坏的文件?
问题描述
编辑:所以我设法让其他东西工作(包括在下面)。但我仍然想了解为什么我的第一次尝试没有成功。
这是我尝试使用 download.file 下载我的公开可用的 Dropbox 文件:
library("readxl")
# Define public download link location ------------------------------------------
excelUrl = "https://www.dropbox.com/s/v9lm9y7nso8yw1x/dashboardTotals.xlsx?dl=1"
# I've experimented with appending '&raw=1' to this, doesn't seem to do anything.
# Function for downloading .xlsx into current wd --------------------------------
read_url_excel <- function(url,saveas = paste0(getwd(),"/newExcelFile.xlsx")){
download.file(url, destfile = saveas)
url_excel <- readxl::read_excel(saveas)
return(url_excel)
}
# This doesn't work, because the file is corrupted and can't be read by read_excel()
excelTable = read_url_excel(excelUrl)
#> Error: Evaluation error: error reading from the connection.
由reprex 包于 2020-01-09 创建(v0.3.0)
你可能会说我不太确定连接/文件在 R 中是如何工作的。我已经设法让它工作了:
excelUrl = "https://www.dropbox.com/s/v9lm9y7nso8yw1x/dashboardTotals.xlsx?dl=1&raw=1"
tmpF <- paste0(getwd(),"/newTing.xlsx")
theFile <- url(excelUrl, open="rb")
binary = readBin(theFile,raw(),100000)
writeBin(binary,tmpF)
由reprex 包于 2020-01-09 创建(v0.3.0)
为什么方法1失败,方法2成功?
解决方案
推荐阅读
- javascript - 如何防止倒数计时器将文本推到它旁边
- oracle - 如何通过将一行与其下一行进行比较来填充新列
- javascript - 如何显示 Github 存储库的贡献者
- java - 如何在 Spring 的静态字段中注入值?
- gcc - MSYS2环境的使用
- java - 如何从另一个活动中调用一个活动的方法?
- firebase - 将文件上传到 Firebase 存储时出现“任务尚未完成”错误
- angular - 使用 angular-auth-oidc-client 的 Cognito OpenId 刷新令牌
- node.js - MongoDB Node.js 驱动程序 4.0.0:Vercel 生产中的光标会话 id 问题
- c++ - 类 basic__ostream 的运算符 << 中的参数