Search code examples
rimportdirectoryraster

R: Error in x[[1]] : subscript out of bounds (importing files)


I am working with the R programming language. I have a folder full of ".R" files that I want to upload into R.

The folder has the following address : C:/Users/OneDrive/Documents/dk"

I tried to follow the directions from the following tutorial: Reading in multiple .rds files and creating one object:

    library(raster)

 getwd()
[1] "C:/Users/OneDrive/Documents"

path = "C:/Users/OneDrive/Documents/dk"

files <- list.files(path = path, pattern = "\\.R$", full.names = TRUE)
r <- lapply(files, readRDS)
s <- stack(r)

But this returns the following error:

Error in x[[1]] : subscript out of bounds

Does anyone know what I am doing wrong? I have included a screenshot which shows the general setup:

enter image description here

My goal is to load all these R files into R Studio at once, and then run them all at the same time - thus, creating all these functions in the global environment.

Can someone please show me what I am doing wrong?

Thanks


Solution

  • As suggested, the second answer from the question I linked was able to help!

    files <- list.files(path = path, pattern = "\\.rds$", full.names = TRUE)
    stack <- do.call("rbind", lapply(files, readRDS))