R 中的 readRDS(文件)

发布于 11-17 05:38 字数 234 浏览 5 评论 0原文

每当我尝试在 R 中安装软件包时,都会收到以下错误:

Error in readRDS(file) : unknown input format

这在我发生系统崩溃后才开始发生。我在 Windows 7 下运行 32 位 R 2.13.0。我尝试删除并重新安装 R,但仍然出现错误。有什么方法可以解决这个问题,而无需删除所有内容(即我安装的所有软件包)并重新开始?

谢谢

Whenever I try to install a package in R, I get the following error:

Error in readRDS(file) : unknown input format

This just started occurring after I had a system crash. I am running 32 bit R 2.13.0 under windows 7. I tried removing and re-installing R, but continue to get the error. Is there any way I can fix this without deleting everything (i.e. all the packages I've installed) and starting over?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

如日中天2024-11-24 05:38:08

以下是我遇到的建议:

  1. 删除运行 R 的目录中的 .Rhistory.RData 文件。
  2. 运行 update.packages()< /code>
  3. 尝试检测库目录中的“坏文件”。您可以在 R 中执行此操作

    # 列出库路径
    # 问题很可能出在第一个目录
    路径 = .libPaths()
    
    ## 尝试检测坏文件
    列表.文件(路径, 
           模式 = "^00LOCK*|*\\.rds$|*\\.RDS$",
           全名= TRUE)
    
    ## 列出大小为 0 的文件
    l = list.files(路径, full.names = TRUE)
    l[sapply(l, 文件大小) == 0]
    

    删除所有突出显示的文件/目录。如果您确实愿意,可以使用file.remove()

  4. 删除存储下载包的目录。

只有解决方案 3 对我有用。

参考:

  • R-sig-Debian 邮件列表
  • 选项 3 是过去几年不同人提供的答案的组合,包括 Chunxiao Xu、Larry Hunsicker 和 Frank Harrell

These are suggestions I have come across:

  1. Delete your .Rhistory and .RData files in the directory in which you are running R.
  2. Run update.packages()
  3. Try and detect "bad files" in your library directories. You can do this in R

    # List the library paths
    # The issue is likely to be in the first directory
    paths = .libPaths()
    
    ## Try and detect bad files
    list.files(paths, 
           pattern = "^00LOCK*|*\\.rds$|*\\.RDS$",
           full.names = TRUE)
    
    ## List files of size 0
    l = list.files(paths, full.names = TRUE)
    l[sapply(l, file.size) == 0]
    

    Delete any files/directories highlighted. You could use file.remove() if you really wanted to.

  4. Delete the directory in which you have stored your downloaded packages.

Only solution 3 worked for me.

Ref:

  • R-sig-Debian mailing list
  • Option 3 was a combination of answers provided by different people over the last few years, including Chunxiao Xu, Larry Hunsicker and Frank Harrell
陈独秀2024-11-24 05:38:08

运行 find /usr/local/lib/R/site-library/ /usr/lib/R/library/ /usr/lib/R/site-library/ ~/.local/lib/ -iname '*rds ' -a -size 0 然后删除找到的文件。

Run find /usr/local/lib/R/site-library/ /usr/lib/R/library/ /usr/lib/R/site-library/ ~/.local/lib/ -iname '*rds' -a -size 0 and then delete the files found.

逆夏时光2024-11-24 05:38:08

Chunxiao Xu 和 Rando Hinn 的上述解决方案对我来说很有效,只是做了一些小小的调整。

首先,将目录更改为您的个人 R 包目录,然后运行: find -iname '*rds' -a -size 0

删除上述列表中包含任何 0 长度文件的目录。然后重新打开 R(或 RStudio)并重新安装已删除的软件包。

您现在应该能够再次列出软件包。

Chunxiao Xu 的原始建议上面列出了所有 R 包目录。但这些目录的确切位置因安装而异,并且必须为您的安装编辑它们的地址。

拉里·亨斯克

Chunxiao Xu and Rando Hinn's solution above worked for me, with a minor tweak.

First, change directories to your personal R package directory, then run: find -iname '*rds' -a -size 0

Delete the directories containing any 0 length files in the above list. Then reopen R (or RStudio) and reinstall the deleted packages.

You should now be able to list the packages again.

Chunxiao Xu's original suggestion above lists ALL of the R package directories. But the exact locations of these directories will vary from installation to installation, and their addresses will have to be edited for your installation.

Larry Hunsicker

离不开的别离2024-11-24 05:38:08

尝试一些简单的方法:如果它是 .rda 文件,请使用 load 而不是 readRDS。然后,您可以将加载的文件另存为 .rds 并再次尝试 readRDS

Something simple to try: if it is an .rda file use load instead of readRDS. You can then save the loaded file as an .rds and try readRDS again.

木森分化2024-11-24 05:38:08

这是我必须做的来解决这个错误:

  • 打开你的Rstudio,转到包下的安装选项卡
  • 记下你的安装到库路径。(你的包将保存在其中)
  • 转到安装库路径。并硬删除最近的包
  • 在 Rstudio 中刷新您的包详细信息

Here is what I had to do to solve this error:

  • Open your Rstudio, go to install tab under packages
  • Note your install to library path.(where your package will be saved)
  • Go to the install library path.And hard delete the recent packages
  • Refresh you package details in Rstudio
我纯我任性2024-11-24 05:38:08

当我在安装几个 R 软件包时不得不重新启动计算机后,就发生了这种情况。我通过物理删除相应库目录中的文件来删除最新安装的软件包,然后重新安装它们,一切都很好。我还必须删除库目录中的 00LOCK 目录。

This happened to me after I had to reboot my machine in the middle of installing several R packages. I removed the latest installed packages by physically removing the files in the appropriate library directory, reinstalled them, and all was well. I also had to remove a 00LOCK directory in the library directory.

做个少女永远怀春2024-11-24 05:38:08

您可能在使用 R 包时遇到一些问题。
查看问题是否是在安装某些软件包后出现的,或者是复制了软件包后出现的。

你可以做的就是从库中复制所有包并将其保存在某个地方,然后复制库中的一些包并重新启动 R 测试它,看看它是如何工作的,你必须一一尝试所有包。
我也遇到了同样的问题,是因为包的问题。

You might have some problem with R packages .
See if the problem has occurred after you installed some packed or did you copied the packages.

What you can do is Copy all the packages from Library and save it somewhere, then Copy some packages on the Library and restart the R test it and see how it works you have to try with all the packages one by one .
I has the same issue and it was due to package problem.

云淡月浅2024-11-24 05:38:08

我在打开 .RDS 文件时也遇到了几个问题,要么是“从连接读取错误”,要么是“未知的输入格式”。我尝试了几个 R 版本,其中版本为 3.4.2。以下工作有效:
(我不小心卸载了rstudio并重新安装了它,还安装了旧的R版本3.4.2,我不知道这是否会改变什么)

  1. 设置一个新的工作目录
  2. 我直接打开RDS文件而不是下载它(所以只保存到临时文件),然后我能够在 R 中打开它,
  3. 我将它从临时文件移动到我的新工作目录并使用 Data_Essay_Account <- readRDS("C:/.../newdirectory/Data_Essay_Account.RDS") 命令并且它起作用了。

不知何故,正确下载它然后移动它不起作用,只有在我将它放入临时文件中之后才起作用。当我最初尝试时,硬删除包对我来说不起作用。

I also had several problems opening an .RDS file, either "error reading from connection" or "unknown input format". I tried with several R versions, with version 3.4.2. the following worked:
(I accidentally uninstalled rstudio and installed it again, also installed older R version 3.4.2, I don't know if that changes anything)

  1. set up a new working directory
  2. I opened the RDS file directly instead of downloading it (so only saved to temp files), then I was able to open it in R
  3. I moved it from temp files to my new working directory and used the Data_Essay_Account <- readRDS("C:/.../newdirectory/Data_Essay_Account.RDS") command and it worked.

Somehow properly downloading it and then moving it did not work, only after I had it in the temp files. Hard deleting packages did not work for me as I tried that initially.

み青杉依旧2024-11-24 05:38:08

与这里的前几个答案不同,我的问题不是通过安装 RStudio 或清理本地文件来解决的,而是通过重写 RDS 文件来解决的。

我可以确认这一点,因为该文件存储在云中,从各种 Windows 和 Mac 笔记本电脑读取该文件都失败,但来自同一 S3 存储桶的类似 RDS 文件可以正常工作。

当我重写 RDS 文件时,问题就消失了。

Unlike the top few answers here, my issue was resolved not by doing anything with my installation of RStudio or cleaning local files, but by re-writing the RDS file.

I could confirm this because the file was stored in the cloud, and reading it from a variety of windows and mac laptops all failed, but similar RDS files from the same S3 bucket worked without a problem.

When I re-wrote the RDS file, the problem went away.

尘曦2024-11-24 05:38:08

我在几个论坛上检查了这个问题,然后我尝试了这个,它对我有用:

1)首先删除 ggplot2 包

remove.packages("ggplot2")

2)然后再次安装该包

install.packages("ggplot2")

3)重新启动 rstudio 并再次安装你的包,希望这有帮助

似乎这个问题所需的包(在我的例子中是 ggplot2)已损坏,并且阻碍了任何新的依赖包的安装。

I checked for this issue on several forums and then I tried this and it worked for me:

1) Remove the ggplot2 package first

remove.packages("ggplot2")

2) Then Install the package again

install.packages("ggplot2")

3) restart rstudio and install your package again, hope this helps

Seems that the issue was with the required package (ggplot2 in my case) got corrupt and was hampering the installation of any new dependent package.

无悔心2024-11-24 05:38:08

我遇到了与OP相同的问题,但在我的情况下,问题.rds文件不是0字节,而是文件大小> 0并且充满NULL字符(至少这是我在我在 Notepad++ 中打开了它们)

通过在加载给我错误的库之前使用 debugonce(loadNamespace) (在我的示例中它是 VIM 包),我最终跟踪了我的问题已损坏.rds 文件位于 forcats 包的 META 文件夹中,该文件是在我安装 VIM 时作为第五代导入安装的,

    (In the rstudio environment pane)
 __NamesSpacesLoading__    chr[1:5] "forcats" "haven" "rio" "car" "VIM"

然后由 VIM 修复只需删除并重新安装 forcats
即可解决问题
调整 @csgillespie 接受的答案中的代码,我可以通过尝试读取所有 rds 文件来找到这一点,如下所示

paths = .libPaths()

l <- list.files(paths, 
           pattern = "*\\.rds$",
           ignore.case = T,
           recursive = T,
           full.names = TRUE)

checkRDS <- function(file) {
  tryCatch({
    readRDS(file)
    "OK"
  },
  error = function(cond) {
    return("Error")
  })
} 

l[sapply(l,checkRDS)=="Error"]

I had the same problem as the OP, but in my case the problem .rds files were not 0 bytes but had file size >0 and were full of NULL characters (at least that's what I saw when I opened them in Notepad++)

By using debugonce(loadNamespace) before loading the library that was giving me the error (in my example it was the VIM package) I eventually traced my problem to corrupted .rds files in the META folder of the forcats package, which was installed as a 5th generation import when I installed VIM

    (In the rstudio environment pane)
 __NamesSpacesLoading__    chr[1:5] "forcats" "haven" "rio" "car" "VIM"

I then fixed by VIM problem simply by removing and reinstalling forcats
Adapting the code in @csgillespie accepted answer, I could have found this by trying to read all the rds files as below

paths = .libPaths()

l <- list.files(paths, 
           pattern = "*\\.rds
quot;,
           ignore.case = T,
           recursive = T,
           full.names = TRUE)

checkRDS <- function(file) {
  tryCatch({
    readRDS(file)
    "OK"
  },
  error = function(cond) {
    return("Error")
  })
} 

l[sapply(l,checkRDS)=="Error"]
情感失落者2024-11-24 05:38:08

我能够通过使用 .libPaths() 并找到 4.1 文件夹来修复它(我的 R 版本是 4.1,所以可能有所不同)。从那里我关闭了 R studio 并删除了每个包的所有文件夹。

我重新安装了所有软件包,并通过错误消息找到了无法下载的软件包。当我找到未下载的软件包时,我使用以下命令安装了它们:install.packages("package", type = "binary")

虽然有点长,但解决了我所有的问题。

I was able to fix it by using .libPaths() and finding the 4.1 folder (my version of R was 4.1, so may differ). From there I closed R studio and deleted all the folders for each package.

I reinstalled all packages and found the packages which wouldn't download through the error messages. When I located the packages that didn't download I installed them by using: install.packages("package", type = "binary").

A bit long but solved all my issues.

国产ˉ祖宗2024-11-24 05:38:08

我的镜像配置错误。我的镜子是俄勒冈州立大学,因为那也是我最接近的大学。 这是镜像列表。在安装任何软件包之前调用此代码。更多信息请参见?选项。

mirror <- "https://ftp.osuosl.org/pub/cran/"
local({
  r <- getOption("repos")
  r["CRAN"] <- mirror
  options(repos = r)
})

My mirror was misconfigured. My mirror is Oregon State University because that is what I am closest too. Here is a list of mirrors. Call this code before installing any packages. More information in ?options.

mirror <- "https://ftp.osuosl.org/pub/cran/"
local({
  r <- getOption("repos")
  r["CRAN"] <- mirror
  options(repos = r)
})
终止放荡2024-11-24 05:38:08

解决方法

在 R 控制台中执行此命令:

options(pkgType = "source")

一切顺利。

The workaround

Execute this command in the R console:

options(pkgType = "source")

You're good to go.

意中人2024-11-24 05:38:08

我的问题是我是使用 save() 函数而不是 saveRDS() 函数保存 RDS 对象。

# Doesn't work
a = 1
save(a, file="test.rds") 
b = readRDS("test.rds")
print(b)
# Works
a = 1
saveRDS(a, file="test.rds") 
b = readRDS("test.rds")
print(b)

My issue was that I was saved the RDS object with the save() function and not the saveRDS() function.

# Doesn't work
a = 1
save(a, file="test.rds") 
b = readRDS("test.rds")
print(b)
# Works
a = 1
saveRDS(a, file="test.rds") 
b = readRDS("test.rds")
print(b)
冬天的雪花2024-11-24 05:38:08

你好~当rds文件太大(比如超过2Gbi)时,可能会发生这种情况。您可以尝试简单地使用加载函数:load("Your file path")

Hi~ THis may happends when the rds file is too big (like over 2Gbi). You can try simply use load function:load("Your file path")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文