'Generate a graph for each file in a directory using R

I have been trying to generate an ECDF graph for a group of files that contain a column of numbers from which the file should be generated.

The input files look like this:

122
34.5
566
...

I am able to generate the ECDF graph for one file at a time with this script that I run in Rstudio:

input <- read.table('/home/agalvez/data/domains/test_ecdf.txt', sep="\t", header=FALSE)
names(input)=c("Values")

#Build the function
myecdf <- ecdf(input$Values)
#Plot the function
plot(myecdf, main = "CDF", xlab = "Bit score", ylab = "Probability")

I have been trying to apply this script to the whole directory of files (too big to do it one by one) and I did not have success. I have been reading and I think a for loop could be a good solution, but unfortunately I do not know how to implement it. Could someone give me some tips on this?

UPDATE-----

My last attempt, following your suggestions was:

library(tidyverse)
plots <-
  list.files("/home/agalvez/data/domains/bits/", full.names = TRUE, recursive = TRUE, pattern = "") %>%
  map(~ {
    input <- read.table(.x, sep = "\t", header = FALSE)
    names(input) <- c("Values")
    myecdf <- ecdf(input$Values)
    
    p <- recordPlot()
    plot(myecdf, main = "CDF", xlab = "Bit score", ylab = "Probability")
    p
  })

# first plot
plots[[1]]

It produced the following errors:

'Error in recordPlot() : no current device to record from'
'Error in plots[[1]] : subscript out of bounds'


Solution 1:[1]

library(tidyverse)

plots <-
  list.files(".", full.names = TRUE, recursive = TRUE, pattern = "txt$") %>%
  map(~ {
    input <- read.table(.x, sep = "\t", header = FALSE)
    names(input) <- c("Values")
    myecdf <- ecdf(input$Values)
    
    p <- recordPlot()
    plot(myecdf, main = "CDF", xlab = "Bit score", ylab = "Probability")
    p
  })

# first plot
plots[[1]]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danlooo