I'm using a for loop to read in multiple shapefiles within a directory and manipulate them individually. Presently I cannot do this because the for loop results in the error: "Error in readOGR(shp_paste) : length(dsn) == 1L is not TRUE". Not a single shapefile is read in; this error occurs immediately. I do not know what this means, and can only find a similar post with this error for writeOGR (link here). When I try to load the shapefiles individually into R Studio, I'm successful in doing so (though I haven't tried individually for all of them, there are too many). What could be causing this error? It's difficult to go through the files in the directory individually to identify potential problems because there are over 10,000. Below is my code:

    input_path<- "/Volumes/Directory/data_folder/"
    files<- list.files(input_path, pattern = "[.]shp$")

    for(f in files){
    ifile<- list.files(input_path, f)
    shp_paste<- paste(input_path, ifile, sep = "")
    shp1<- readOGR(shape_paste)

Running this code produces the error above.

  • In my test of your code, it is pasting the filename to the end of the path without a separator, making a nonexistent filename. You could instead use file.path. I also highly recommend adding print statements in your function as a test so that you can see what output is being produced at each state.
    – SMiller
    Commented May 22, 2019 at 16:23
  • Could you paste what you're getting? What I get from shp_paste is "/Volumes/Directory/data_folder/file1.shp", for example. If you took this and put it directly into readOGR it would read the shapefile successfully. I think the problem is someplace else
    – Wincow
    Commented May 22, 2019 at 16:28
  • Your code assigns to shp_paste and then reads from shape_paste. Please check your examples fail correctly before posting them - by which I mean it doesn't produce the error you say it does!
    – Spacedman
    Commented May 22, 2019 at 17:09

2 Answers 2


Seems like you are passing a list to the read function instead of a single file

When you call

shp_paste<- paste(input_path, ifile, sep = "")

the ifile object can be a list of files, so shp_paste has a length greater than 1.

You might change this by changing the code to

shp_paste<- paste(input_path, f, sep = "")

since you are iterating over files. Or you could just list the full path in the first place:

files<- list.files(input_path, pattern = "[.]shp$", full.names=T)

So this code should work:

    input_path <- "/Volumes/Directory/data_folder/"
    files <- list.files(input_path, pattern = "[.]shp$", full.names=T)

    for (f in files){
    shp1 <- readOGR(f)

Use full.names=TRUE to get the full path to the shapefiles, and then use lapply to loop over the shapefiles and read them all into a list:

> files<- list.files(input_path, pattern = "[.]shp$",full.names=TRUE)
> allShapes = lapply(files, readOGR)

> plot(allShapes[[1]]) # to plot the first one.

or loop over them:

> for(shp in files){
    sp = readOGR(shp)
    message("REad ",nrow(sp))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.