简体   繁体   English

为什么需要为此.rds文件使用mode = wb和download.file()?

[英]Why do I need to use mode = wb with download.file() for this .rds file?

I was getting hung up on Shiny Apps Tutorial Lesson 5 because I was unable to open the counties.rds file. 我无法进入Shiny Apps教程第5课,因为无法打开counties.rds文件。 readRDS() threw: error reading from connection . readRDS()抛出: error reading from connection

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. .rds如果使用download.file(URL, dest, mode = "wb")下载该.rds文件,或者只是使用我的浏览器将该文件下载到本地目录download.file(URL, dest, mode = "wb")则可以打开.rds

Outstanding Question: Why does the counties.rds file not open properly if I use download.file() without setting mode = "wb" ? 悬而未决的问题:如果在没有设置mode = "wb"情况mode = "wb"使用download.file()为什么counties.rds文件无法正确打开? I expect the answer will be something obvious like: "Duh, counties.rds is a binary file." 我希望答案将是显而易见的:““, counties.rds是一个二进制文件。” However, before I try to answer my own question, I'd like confirmation from someone with more experience. 但是,在尝试回答自己的问题之前,我想请经验丰富的人确认。

Repro steps: 复制步骤:

    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

Resolution: Download via browser or use binary mode ( wb ). 解决方法:通过浏览器下载或使用二进制模式( wb )。

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

My suggestion is to always specify 'mode' regardless and that it pretty safe to always use mode="wb". 我的建议是始终指定“ mode”,并且始终使用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 https://stat.ethz.ch/pipermail/r-devel/2012-August/064739.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM