I would like to write several R source codes in a loop. However I am not getting a functional source file. That´s what I tried:
for(k in 1:10){
cod <- 'x <- 1+k
nam <- paste0(x, ".txt")
write.table(x, nam)'
cod <- gsub("x", k, cod)
file <- paste0("source", k, ".R")
dput(cod, file=file)}
Strictly speaking, I would like to get exactly the code in the cod object in 10 sources codes (differing by the k, which go from 1 to 10). Actually this code produce all the 10 files, but they contain strange characters and are no functional at all.
dputwithwriteLines(cod, file)dputwrites an ASCII text representation of an R object, it's not mean for saving output. UsewriteLines,sink,cat5 <- 1+kbecause you substitutexwith the value ofk. What is going on?source("code.R"); dothing(1)anddothing(2)and so on. Or better still parameterise the script and uselittler.