0

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

nrcjea001
  • 1,027
  • 1
  • 9
  • 21
  • 2
    I'd love to help! What is your question? – statsNoob Nov 13 '15 at 19:25
  • 1
    this seems like a bad programming practice. Instead of updating variable names by brute read and write, why not make them arguments to whatever routine you're running repeatedly? – C8H10N4O2 Nov 13 '15 at 19:36

0 Answers0