我在重新保存对象时展示了saveRDS的奇怪行为.这是一个可重复的例子:
df <- data.frame(a = c(1,2), b = c(3,4))
saveRDS(df, "test.rds")
readRdsFile <- readRDS("test.rds")
saveRDS(df1, "test.rds") #trying to resave the object however there is an issue with the object df1. Mistaken object name or it has not been compiled somewhere in the code before so it does not exist.
readRdsFile2 <- readRDS("test.rds") #the original file is corrupted.
Error in readRDS(“test.rds”) : error reading from connection
这有意义吗?它是否有意,是否有任何我可以做的,以避免文件损坏?
最佳答案 我不知道问题的来源,但如果你没有找到更好的东西,这里有一个修复.在获取saveRDS之前,它将返回str的错误,因此您不会遇到损坏问题
saveRDS2 <- function(object,file){str(object);saveRDS(object,file)}
df <- data.frame(a = c(1,2), b = c(3,4))
saveRDS2(df, "test.rds")
readRdsFile <- readRDS("test.rds")
saveRDS2(df1, "test.rds") # error as df1 doesn't exists
readRdsFile2 <- readRDS("test.rds") #the original file is not corrupted and can be reloaded