0

I have list of data frames and i want to export one bar chart per data frame... I am trying use lapply but it does not work... Does anyone know how to do this?

my_data <- lapply(X = seq(from = 1, to = length(in_files_path), by = 1), FUN = function(x){
  data_tables <- read.table(file = in_files_path[[x]], header = TRUE)
})

lapply(X = seq(from = 1, to = length(in_files_path), by = 1), FUN = function(x){
  setwd(dir = ou_graph_path)
  png(filename = in_files_name[[x]], 
      units = "in", 
      width = 15, 
      height = 10, 
      res = 300)
  ggplot(data = my_data[[x]], aes(x = my_data[[x]]$A, y = my_data[[x]]$B)) +
    geom_bar()
  dev.off()
}) 

1 Answers1

1

I would advice using the following approach

I would advice using the following approach

# Get list of files
# Start loop -
# read files
# make plot
# store plots in list
# - end loop
#
# Start loop -
# perform plot operation
# save plots
# - end loop

setwd(your_location_of_the_files)
list_files = list.files(pattern = ".csv")

for(i_file in list_files){
    dummy = fread(i_file,header = TRUE)
    png(filename = paste(your_location_for_the_plots,in_files_name[[x]],sep="/"), 
    units = "in", 
    width = 15, 
    height = 10, 
    res = 300)
    # You can just say A here, not dummy$A
    plot(ggplot(data = dummy, aes(x = A, y = B)) + geom_bar())
    dev.off()
}
zwep
  • 1,207
  • 12
  • 26
  • 1
    You can also specify the path in `png()`; better not to mess with the working directory. I would save the plot at the same time it is created. If you create 1000 plots it can cause memory issues if you put them into memory first. – Jasper Jun 29 '17 at 09:03
  • Ah yes, agree on that! Forgot about that option in png. Will edit the answer – zwep Jun 29 '17 at 09:06