Kaleb
Kaleb

Reputation: 1022

How can an R variable be passed to a system call for a python function?

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
}

Upvotes: 2

Views: 1218

Answers (1)

Craig Citro
Craig Citro

Reputation: 6625

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))
}

Upvotes: 5

Related Questions