passing in a string to use as pattern for regex

Hi everyone!

I'm trying to read in multiple files from multiple folders based on a pattern by writing my own function for it. The function takes two arguments: the folder path and an "id" string variable that is on each file in the folder.

I think the issue is with the id argument and how to read the string that I would pass in as the id into the subsequent pattern and sub function.

Any insight would be extremely helpful! Thanks.

read_docs <- function (path, id) {
file_list <- c(list.files(path, pattern = "./id/.\.pdf",
all.files = FALSE, full.names = TRUE, recursive = FALSE,
ignore.case = TRUE, include.dirs = FALSE, no.. = FALSE)

for (i in file_list) {
name <- sub(".*'id' - (.?) -.", "\1", i)
assign(name, pdf_text(i))
}
}

Not 100% clear to me what you're after, but to use your function's id parameter in the pattern argument to list.files() you could use paste0() to create the string you need.

I.e.,
paste0("./", id, "/.\.pdf")

— Robert

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.