'Rscript: Write dynamic text to a file and use dynamic file name

I am trying to put dynamic text into a file that is named dynamically.

filenames <- c("a","b","c","d","e","f","g","h")

for(i in seq_along(filenames)) {
    txt <- paste("This is file", filenames[i])
    #print(txt)
    write.table(paste(txt, filenames[i]), paste("File", filenames[i], ".txt"))
}

This code outputs almost what I want, except I get a leading "x" and "1" in all, as well as the dynamic portion of the name in the text of the files.

enter image description here

How do I correct this?



Sources

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

Source: Stack Overflow

Solution Source