I'm writing an R function for a package which accepts an *rds file as an input argument. The user is meant to provide an input *rds file which will be read by the function, and further worked with downstream.
I'm currently confused how to best check that the *rds file provided is (1) actually an *rds file and (2) a valid *rds file.
Note on *rds and readRDS()
For instance, let's say the user provides a file with the *rds extension but it's not valid:
$ touch foobar.rds
$ R
> foo = readRDS("foobar.rds")
Error in readRDS("foobar.rds") : error reading from connection
Yes, I could write a tryCatch()
as follows to catch these errors, e.g.
readInputRdsFile = function(input_rds){
input = tryCatch(readRDS(input_rds),
error = function(c) stop("The input *rds is invalid. Please fix your input file.")
)
}
## within R
> readInputRdsFile("foobar.rds")
Error in value[[3L]](cond) :
The input *rds is invalid. Please fix your input file.
However, this is a very generic check. There could be many things wrong with the *rds file besides "invalid" (in this case, it's not an *rds file at all, but a text file).
Are there more standard checks to include which would more precisely detect issues with the *rds file? Any insight appreciated, thank you!
question from:
https://stackoverflow.com/questions/66049477/how-to-check-that-an-input-rds-file-is-valid 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…