How do I load an rds file into R
Rather than typing a long file path, a really good idea in R is to let the system do the typing for you. That is, do something like this:
filename <- file.choose()
Canteen_clean <- readRDS(filename)
The first line will open the usual file open dialog box; you can select the file you want, and the name will be stored in the filename
variable. The second line will use that name to open it.
readRDS(file) in R
These are suggestions I have come across:
- Delete your
.Rhistory
and.RData
files in the directory in which you are running R. - Run
update.packages()
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.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
Creating a function that allows me to readRDS files?
Are you looking for something like this?
saveRDS(diamonds, '~/diamonds.rds')
#> Error in saveRDS(diamonds, "~/diamonds.rds"): object 'diamonds' not found
test <- function(entry) {
readRDS(paste0("~/", entry))
}
test('diamonds.rds')
#> # A tibble: 53,940 x 10
#> carat cut color clarity depth table price x y z
#> <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
#> 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
#> 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
#> 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
#> 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
#> 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
#> 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
#> 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
#> 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
#> 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
#> # … with 53,930 more rows
#no need to pass string as an argument
test2 <- function(entry) {
.string <- substitute(entry) |> as.character()
readRDS(stringr::str_glue("~/{.string}"))
}
test2(diamonds.rds)
#> # A tibble: 53,940 x 10
#> carat cut color clarity depth table price x y z
#> <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
#> 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
#> 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
#> 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
#> 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
#> 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
#> 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
#> 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
#> 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
#> 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
#> # … with 53,930 more rows
Created on 2021-06-28 by the reprex package (v2.0.0)
Load an RDS file from the web (i.e. a url) directly into R?
You can use:
b <- readRDS(url("https://collidr-api.s3-ap-southeast-2.amazonaws.com/pfd.RDS","rb"))
str(b)
# 'data.frame': 649346 obs. of 2 variables:
# $ package_names : chr "A3" "A3" "A3" "A3" ...
# $ function_names: chr "a" "a3" "A3-package" "a3.base" ...
Related Topics
How to Add Percentage or Count Labels Above Percentage Bar Plot
Controlling Ggplot2 Legend Display Order
How to Plot a Function Curve in R
Filtering Out Duplicated/Non-Unique Rows in Data.Table
How to Position Two Legends Independently in Ggplot
Which Data.Table Syntax For Left Join (One Column) to Prefer
R Ifelse to Replace Values in a Column
How to Calculate the Co-Occurrence in the Table
What Does .Sd Stand For in Data.Table in R
Editing Legend (Text) Labels in Ggplot
Dplyr::Select Function Clashes With Mass::Select
Understanding the Order() Function