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