I would like to access, update and run an R script from another R script. Specifically, I would like to create a loop that reads in the lines of another R script in order to update some variable names by adding increments of 1 to each of them. Once the variables have been updated, I would like to run the R script.
Currently my code for this is as follows:
for (i in 1:n) {
x <- readLines("Mscript0.R")
y <- gsub(paste0("Mtrain",i),paste0("Mtrain",i+1), x)
cat(y, file="Mscript0.R", sep="\n")
source("Mscript0.R")
}
Please note that the string "Mtrain" in the source script takes on various different forms such as:
Mtrain4 <- read.csv("Mtrain4.csv",header=T,sep=",")
s <- Mtrain4$Weight
Any Ideas?
Thanks