0

I am trying to load several gpkg files into R but got some problems. Initially this code works:

library("dplyr")
library("rgdal")
library("sp")
library("st")
library("sf")

setwd("C:\Users\filename.gpkg")
temp = list.files(pattern=".gpkg")
myfiles = lapply(temp, st_read)

Also, this works for each file (but I have more than 15 files):

st_read("filename.gpkg", layer= 'layerx')

But I need to specify the layer, so I code this way:

temp = list.files(pattern=".gpkg", full.names= TRUE)
tem2 = lapply(temp, st_read(dsn= '.',layer= 'layerx'))

But does not work, produce this error:

Error: Cannot open "C:\Users\"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.

Any ideas on how to solve this? BR

0

1 Answer 1

2

In lapply, the second argument needs to be a function definition (not a function call).

Please try replacing the last expression with:

tem2 = lapply(temp, function(x) st_read(x, layer= 'layerx'))
2
  • 1
    Great Michael, that worked!!
    – Alex
    Dec 12, 2021 at 14:15
  • 2
    It would be polite to upvote and accept the answer then.
    – user30184
    Dec 12, 2021 at 18:04

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.