R从URL下载图片 [英] R Downloading Pictures from URL

查看:90
本文介绍了R从URL下载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从R的网站下载图片时遇到问题。
似乎图像已被 download.file 正确识别:



尝试URL' http://i.imgur.com/pszAeGh.png '
内容类型'image / png'长度16592字节(16 Kb)
打开的URL
下载的16 Kb



并且还会以正确的名称和以(.png)结尾的文件下载到正确的目录。

  download.file( http://i.imgur.com/pszAeGh.png,粘贴(**您的EST * *, Stackoverflow,。png,sep =)))

但是,文件被屠杀,无法打开。



我在这里做什么错了?

解决方案

我无法找不到重复的内容,所以我就回答。这样,但如果仔细阅读,就会发现在Windows上,默认的 mode = w 仅适用于文本文件。对于二进制文件(除文本外几乎所有内容),您需要 mode = wb 。如果网址以 gz,.bz2,.xz,.tgz,.zip,.rda .RData ,但是对于其他任何事情,您需要自己指定 mode = wb


I have a problem downloading pictures from websites with R. It seems like the image is properly recognized by download.file:

trying URL 'http://i.imgur.com/pszAeGh.png' Content type 'image/png' length 16592 bytes (16 Kb) opened URL downloaded 16 Kb

And it also is downloaded with the right name and file ending (.png) to the right directory.

download.file("http://i.imgur.com/pszAeGh.png",paste(**yourDEST**,"Stackoverflow",".png",sep=""))

however, the file is butchered and can't be opened.

What am I doing wrong here?

解决方案

I couldn't find a duplicate, so I'll just answer.

The download.file help is a bit fragmented on this, but if you read it all carefully you can find that, on Windows, the default mode = "w" is only suitable for text files. For binary files (pretty much everything but text) you need mode = "wb". This will be done automagically if the URL ends in any of gz, .bz2, .xz, .tgz, .zip, .rda or .RData, however for anything else you need to specify mode = "wb" yourself.

这篇关于R从URL下载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆