为什么我需要为此.rds文件使用mode = wb和download.file()? [英] Why do I need to use mode = wb with download.file() for this .rds file?

查看:131
本文介绍了为什么我需要为此.rds文件使用mode = wb和download.file()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我因为 Shiny Apps教程第5课而挂断电话,因为我无法打开 counties.rds 文件。 readRDS()抛出:从连接中读取错误

I was getting hung up on Shiny Apps Tutorial Lesson 5 because I was unable to open the counties.rds file. readRDS() threw: error reading from connection.

我发现如果使用 download.file(URL,dest,mode = wb)下载,可以打开 .rds 罚款或只是使用浏览器将文件下载到本地目录中。

I figured out I could open the .rds fine if I downloaded it with download.file(URL, dest, mode = "wb") or simply used my browser to download the file to my local directory.

未解决的问题:为什么<$如果我在未设置 mode =的情况下使用 download.file(),c $ c> counties.rds 文件无法正确打开wb ?我希望答案将是显而易见的:, counties.rds 是二进制文件。但是,在尝试回答自己的问题之前,我想得到经验丰富的人的确认。

Outstanding Question: Why does the counties.rds file not open properly if I use download.file() without setting mode = "wb"? I expect the answer will be something obvious like: "Duh, counties.rds is a binary file." However, before I try to answer my own question, I'd like confirmation from someone with more experience.

复制步骤:

    download.file("http://shiny.rstudio.com/tutorial/lesson5/census-app/data/counties.rds",
    "counties.rds")

    counties <- readRDS("counties.rds")
    Error in readRDS("counties.rds") : error reading from connection

解决方案:通过浏览器下载或使用二进制模式( wb )。

Resolution: Download via browser or use binary mode (wb).

    download.file("http://shiny.rstudio.com/tutorial/lesson5/census-app/data/counties.rds",
    "counties.rds", mode = "wb")
    counties <- readRDS("counties.rds") # Success!


推荐答案

我的建议是始终指定'mode',无论始终使用mode = wb非常安全。我认为后者应该是默认值,并且通过文件扩展名自动识别是错误的,不应依赖于它。 https://stat.ethz.ch/pipermail/r-devel /2012-August/064739.html

My suggestion is to always specify 'mode' regardless and that it pretty safe to always use mode="wb". I argue it the latter should be the default and that the automatic recognition by file extension is faulty and should not be relied upon, cf. https://stat.ethz.ch/pipermail/r-devel/2012-August/064739.html

这篇关于为什么我需要为此.rds文件使用mode = wb和download.file()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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