2

Is there any package for calling a python function from R by passing the function arguments through R? Now i have directly called the python file using system in R.

a<-system('/home/anaconda3/bin/python  /home/Desktop/myfile.py' ,intern = TRUE)

But this myfile.py file is having a function with paramenter. How to specify the parameter in R?

I have tried system('/home/anaconda3/bin/python /home/Desktop/myfile.py argument',wait=FALSE,intern = TRUE) .But it returns 0.

2 Answers 2

1

for example I want to pass the number of core that my python script can use:

system(paste('/home/anaconda3/bin/python','home/Desktop/myfile.py',NCORE))

Then in Python Script before launch the function I can read my parameter in this way:

n_core = int(sys.argv[1])

sys.argv is a list in Python, which contains the command-line arguments passed to the script.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm attempting this same logic right now, and R is finding the executable but not finding the .py script. The .py script does exist. Is there any special difference between ' or "? Should there be spaces between the string objects passed through system()?
Oh, apparently you can't have spaces in the path name. It runs if I remove spaces in the underlying folder structure names. Is there a way to keep spaces? I can rely on myself to not put spaces in but I can't guarantee that others using this script will do the same.
1

please look at reticulate

library(reticulate)
os <- import("os")
os$listdir(".")

Comments

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.