2

I have written a function in python that takes two arguments one of which is a file path name. I have wrapped the call to this python function within a loop in R. I want to be able to update a variable in this R for loop and pass the value of this variable to the python function as the file path name. How can I do this?

R code:

for (file in filenames) {
     file <- paste("/home/yyy/xx", file, sep="/")
     system('python pylib/filefunction.py <TODO: file> divide') # divide is the 2nd argument
}
1
  • what about converting divide to a string and concatenating it onto the end of the system call? (maybe with paste?) Commented Jul 8, 2012 at 7:04

1 Answer 1

5

I believe you can just use paste again:

for (file in filenames) {
  file <- paste('/home/yyy/xx', file, sep='/')
  system(paste('python', 'pylib/filefunction.py', file, 'divide'))
}

Or, better yet, sprintf:

for (file in filenames) {
  system(sprintf('python pylib/filefunction.py /home/yyy/xx/%s divide', file))
}
Sign up to request clarification or add additional context in comments.

1 Comment

If this is not the correct answer, then I don't understand the question. (+1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.