0

I would like to use R to go into a text document I have and iteratively substitute the base name of the files in the first two lines (input and output) with those from a list I have stored in R. The text file looks like this:

parameter  input "C:\Users\inputdirectory\20090522_104644_r2_0_DVJ_50.k"
parameter  output "C:\Users\outputdirectory\20090522_104644_r2_0_DVJ_50.k"
parameter  meshsize 60.0
parameter  zlength 500.0
parameter  zelem 10
runscript "meshoutline.scl"  &input &output &meshsize &zlength &zelem

The idea is to replace the input and output files (they should be identical names since they are stored in different directories) and then send the script to run in CMD. Then repeat the loop with the next file name in the list, which looks like this in R:

flist <- c("20080715_160601_es_2_KOE_50", "20080806_162049_es_0_JSX_50", "20080810_004229_es_0_WIB_50")

I've never used R for systems stuff before. Is this possible?

jogo
  • 12,469
  • 11
  • 37
  • 42
hmnoidk
  • 545
  • 6
  • 20

1 Answers1

0

This is the way you can do that:

s0 <-
c('parameter  input "C:\\Users\\inputdirectory\\"',
'parameter  output "C:\\Users\\outputdirectory\\"',
'parameter  meshsize 60.0',
'parameter  zlength 500.0',
'parameter  zelem 10',
'runscript "meshoutline.scl"  &input &output &meshsize &zlength &zelem')

flist <- c("20080715_160601_es_2_KOE_50", "20080806_162049_es_0_JSX_50", "20080810_004229_es_0_WIB_50")

for (file in flist) {
  s <- s0
  s[1:2] <- paste0(s[1:2], file)
  sapply(s, system)
}
jogo
  • 12,469
  • 11
  • 37
  • 42
  • I think what this code is trying to do is run each line of s0 through CMD. S0 needs to be saved as its own text file and run through CMD as that, rather than by line. – hmnoidk Nov 22 '18 at 19:13