Editing a .r File from Another .r File

I am trying to reproduce my current project and create a master document (eventually a .rmd file) that will be used to invoke and execute several other documents. Thus, I and other researchers should only open and run one file.

The current configuration includes three levels: the main file, 2 files for reading, 2 databases. The main file calls the read-in files using source (), and the read-in files parse the .csv databases and apply labels.

Reading files and databases are automatically generated using the data management software that I use (REDCAP) every time I download updated data.

However, there is a line of code in the read files that deletes all the objects in my environment. I would like to edit reading files directly from the main file, so I don’t have to open reading files every time I run my report. In particular, since all the files to read are the same, I would like to delete line # 2 in each.

I tried searching google and tried file.edit () but couldn't find anything. I'm not even sure that this is possible, but thought that I would ask. Let me know if I can improve this question or if you need any additional code to answer it. Thank!

Current relevant master code (edited for generality):

 source("read-in1")  
 source("read-in2")

The current current code of the file to read (the same in each file, except for the database name):

 #Clear existing data and graphics  
 rm(list=ls())  
 graphics.off()  
 #Load Hmisc library  
 library(Hmisc)  
 #Read Data  
 data=read.csv('database.csv')  
 #Setting Labels  

[reading code truncated]

:
: Windows 7 Professional x86
R: 3.1.3
R Studio version: 0.99.441

+4
3

. : (1) , (2) , rm (list = ls()), , , (3) .

files_to_change <- c("read-in1.R", "read-in2.R")
for (f in files_to_change) {
    old <- paste0(f, ".old")
    system(paste("cmd.exe /c ren", f, old))
    system(paste("cmd.exe /c findstr /v rm(list=ls())", old, ">", f))
    system(paste("cmd.exe /c rm", old))
}

#Clear existing data and graphics  
graphics.off()  
#Load Hmisc library  
library(Hmisc)  
#Read Data  
data=read.csv('database.csv')  
#Setting Labels  

*.R . script

@echo off
ren "%~f1" "%~nx1.old"
findstr /v "rm(list=ls())" "%~f1.old" > "%~f1"
rm "%~nx1.old"

, "example.bat", , system.

+1

readLines() - ( @Hong Ooi ):

eval(parse(readLines("read-in1.R")[-2]))

, :

f <- file("read-in1.R", open="r")
t <- readLines(f)
close(f)

for (l in t[-2]) { eval(parse(text=l)) }

for() , ( -2). , , , , @Hong Ooi's:

f <- file("read-in1.R", open="r")
t <- readLines(f)
close(f)

f <- file("out.R", open="w")
o <- writeLines(t[-2], f)
close(f)
source("out.R")
+4

, , API REDCap redcapAPI, REDCapR. REDCap R . redcapAPI (REDCapR . , ).

+2

All Articles