本文介绍了Knitr必须下载zip文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于与网络安全系统冲突,我可以将数据文件从Internet下载到网络中可以在R中使用的区域的唯一方法是通过R本身下载.当我在RStudio中运行脚本时,它可以正常工作.当我尝试编织脚本时,我会收到消息Unsupported URL或得到

Due to clash with the network security system the only way I cand download a data file from the internet to an area in the network where it can be used in R it is by downloading it through R itself. When I run the script in RStudio it works fine. When I try to knit the script I will get either the message Unsupported URL or I will get

Error in file(file, "rt") : cannot open the connection
Calls: <Anonymous> ... withVisible -> eval -> eval -> read.csv ->
    read.table -> file
Execution halted

下面是在正常运行下但在编织过程中不起作用的代码.

below is the code that works under normal running but not during the knitting process.

url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(url, "repdata-data-activity.zip")
unzip("repdata-data-activity.zip")

如果文件不是zip格式,则可以使用RCurl下载,但是当我尝试R崩溃时,我也尝试了method = "curl", setInternet2(TRUE),并尝试从https中删除s,但是没有他们中的有工作.

If the file wasn't a zip I could download it using RCurl, but when I tried that R crashed, I have also tried method = "curl", setInternet2(TRUE), and trying to remove the s from https but none of them have worked.

结果是我无法制作出有问题的编织文件.我以前遇到过一个非常类似的问题(一个CSV文件而不是一个压缩CSV文件,请参见下面的链接),并尝试了该建议,但没有成功:

The result is that I cannot produce a knitted document which is a problem. I have previous on a very similar problem ( a CSV file not a zipped CSV file, see link below) and have tried the advice but without success: R produces "unsupported URL scheme" error when getting data from https sites

我正在使用Windows 7和RStudio

I am using Windows 7 and RStudio

要重复一遍:这仅是在编织文档时出现的问题,而不是在运行脚本时出现的问题.

To repeat: this is ONLY a problem WHEN KNITTING the document, not when running the script.

推荐答案

问题不仅是https,而且文件是二进制文件.通过将URL更改为http并将file.download设置为mode ="wb",此问题已解决,脚本可以成功创建.

The problem was both the https and that the file is a binary. By changing the URL to http and setting file.download to mode="wb" the problem was resolved and the script could be knitted successfully.

最终代码块如下

url <- "http://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(url, "repdata-data-activity.zip", mode="wb")
unzip("repdata-data-activity.zip")

这篇关于Knitr必须下载zip文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:50