I want to make a function that reads all csv of a local directory and save them as a list of dataframes in my R environment. I tried the follwing:
getdflist <- function(directory = getwd()) {
setwd(directory)
monitors <- list.files(directory)
dataframes <- vector("list", length(monitors))
for (i in seq_along(monitors)) {
dataframes[[i]] <- read.csv(monitors[[i]])
print(i)
}
dataframes
}
but it only prints i and doesn't save the list in my environment
Could anyone help me to see the error on my code?
Note: When I try to run it as a code (not a function):
> monitors <-list.files(getwd())
> dataframes <- vector("list", length(monitors))
> for (i in 1:length(monitors)){
+ dataframes[[i]] <- read.csv(monitors[[i]], sep = ",")
+ }
>
it works and saves it as a dataframes list with 65.5MB weight, the problem is when I pass it as a function. May be it could be because the lexical scoping?
Solution for this question is the most valued answer for this: How to make object created within function usable outside