0

Suppose I have a file structure defined as follows:

There are three folders: A, B and C. Each of the folders contains a file called file_demo.csv. Now I would like to read the file from each of the folders, do some operation on them and export them to three new files not in those folders. Subsequently I use lapply() to do.

Here's some code for a demo:

# the folder list
folder_list <- c('A', 'B', 'C') 

# creating demo data frames
set.seed(1)
file_demo_a <- data.frame(X = rnorm(5),
                        Y = rpois(5, lambda = 2))
write_csv(file_demo_a, 'A/file_demo.csv')

set.seed(2)
file_demo_b <- data.frame(X = rnorm(5),
                          Y = rpois(5, lambda = 2))
write_csv(file_demo_b, 'B/file_demo.csv')

set.seed(3)
file_demo_c <- data.frame(X = rnorm(5),
                          Y = rpois(5, lambda = 2))
write_csv(file_demo_c, 'C/file_demo.csv')

# defining a function
df_mod_func <- function(folder_name){
  path_name <- paste(folder_name, 'file_demo.csv', sep = "/")
  new_demo <- read_csv(path_name)
  new_demo <- new_demo + 1  # do a new operation
  csv_file_name <- paste(folder_name, 'new_file_demo.csv', sep = "_")
  new_demo %>% write_csv(csv_file_name)
  # return(NULL)
}

lapply(folder_list, df_mod_func)

Now the problem I am facing is that when I call lapply(), each of the final data frames are printed to the console. This is a problem because these data files that I will load are huge and I do not want R to crash. I also do not want to store it a an object because of the huge size. I have also tried to return NULL in the function but that seems like a hacky way plus I do not want to fill up my console with useless output.

Is there a way to not get lapply (or use any other function) to collect the output in this case and just silently execute?

Jash Shah
  • 2,064
  • 4
  • 23
  • 41

1 Answers1

3

If it's just about not printing the result, you can always use invisible(), as in:

invisible( lapply( folder_list, df_mod_func ) )
d0d0
  • 160
  • 8