Skip to content

Writing output of a looped process with pdfs

1 message · MacQueen, Don

#
Having done this:

setwd("/Users/sisolarrosa/Documents/PhD/R_work/AF/IIC/split_fnp/")
shps<- dir(getwd(), "*.shp")
shps <- gsub('.{4}$', '', shps)

You can create the list directly, instead of manually, like this (not
tested, and see ?list):

  fnps <- vector('list', length(shps))
  names(fnps) <- shps

  for (shp in shps) fnps[[shp]] <- readOGR(".",layer=shp)

It is not clear from your posting exactly which command in your calculate
distances loop is failing. Have you stepped through them one at a time
manually?

Your calculation of the name outfile is probably failing because fnp is
not a character string. fnp is a spdf, so it makes no sense to use it in
   paste0("distances_", fnp, ".txt")

If you create fnps as I suggest above, then you can modify your loop along
these lines:

for (nm in names(fnps)) {
  fnp <- fnps[[nm]]
...
...  paste0("distances_", nm, ".txt")
...
}
 
Note that then names of the list fnps were calculated from a directory
listing created using dir(), so they are based on the file names. If any
of the file names have space characters in them, you will want to replace
those spaces with dots or underscores. I also don't know if the names
include the .shp suffix; if so you might want to get rid of that in the
outfile names.

In other words, I've outlined an approach, but there are likely some pesky
details in handling the list element names and output file names based on
the input file names.

-Don