Data frame files from folders

Hi, I want to print out every file contents of either .csv/.tsv of folders, I made a code but it only does it for the first file, the rest generates a 0x0 table and would like to know what's messing it up.

##Directory Tree
dataPath <- "F:/Server/Ref"
tblFile <-
list.files(path = paste(dataPath, "Pool", sep = "/"),
#recursive = TRUE,
pattern = "*.[ct]sv",
full.names = TRUE) %>%
data.frame()
##File Content
for (i in 1:nrow(tblFile[1])){
print(assign(paste("", tblFile[1, i]), tblFile[1, i] %>%
map_df(read.table, header = FALSE, fill = TRUE) %>%
#map_df(~fread(.), header = FALSE, fill = TRUE) %>%
data.frame()))
}

The it only does it for the first file illustrates the R gotcha with for loops: unless captured by an object in the global environment, each iteration clobbers its predecessor.

To avoid this, the outline is

holder <- list(length = n) # n = number of return values you expect
for (i in seq_along(source_object) {
    holder[i] = some_function()

For your application, given a list of delimiter separated files located, for illustration, in the csv folder under the Rproj folder could be more directly done along the following lines

library(data.table)
add_prefix <- function(x) paste0("csv/",x)
the_files <- dir(here("csv"))
the_files <- lapply(the_files,add_prefix) |> unlist()
batch <- lapply(the_files,fread)
for(i in seq_along(batch)) print(batch[[i]])
1 Like

If this can help others on that topic, here is a working script:

fullPath <- "C:/FileOrFolderPath"
##File Name
if (dir.exists(fullPath)) {
tblFile <- list.files(path = fullPath,
recursive = TRUE,
pattern = "*.[ct]sv",
full.names = TRUE)
} else if (file.exists(fullPath)) {
tblFile <- fullPath
}
##File Content
for (i in seq_along(tblFile)) {
assign(paste0("", tblFile[i]), tblFile[i] %>%
map_df(read.table,
header = FALSE,
sep = ifelse(right(tblFile[i], 3) == "csv", ",", "\t"),
quote = "",
dec = ".",
fill = TRUE,
comment.char = ""), envir = .GlobalEnv)
}

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.