如何将R变量传递给python函数的系统调用?

Kal*_*leb 2 python r

我在python中编写了一个函数,它接受两个参数,其中一个是文件路径名.我在R中的循环中包装了对这个python函数的调用.我希望能够更新此R for循环中的变量,并将此变量的值作为文件路径名传递给python函数.我怎样才能做到这一点?

R代码:

for (file in filenames) {
     file <- paste("/home/yyy/xx", file, sep="/")
     system('python pylib/filefunction.py <TODO: file> divide') # divide is the 2nd argument
}
Run Code Online (Sandbox Code Playgroud)

Cra*_*tro 5

我相信你可以paste再次使用:

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

或者,更好的是sprintf:

for (file in filenames) {
  system(sprintf('python pylib/filefunction.py /home/yyy/xx/%s divide', file))
}
Run Code Online (Sandbox Code Playgroud)