Root directory for python chunk in knitr?

I hope this is not as easy as it sounds. I have a base directory:

root --> Paper --> Code 

for the paper I'm writing. I want to call a Python script in the Code directory from my knitr document in the Paper directory (something like this , but with Python instead of P). So it would be something like this:

Python script testit.py

 ## @knitr testit import os print os.getcwd() 

The document test knitr.Rnw is saved in the code directory:

 \documentclass{article} \begin{document} <<setup,echo=FALSE>>= read_chunk("testit.py") @ <<testit,engine='python'>>= @ \end{document} 

When compiling to PDF, the answer is correct:

 ## /Users/blah/foo/bar/Code 

But the test-2.Rnw document is saved in the paper directory:

 \documentclass{article} \begin{document} <<setup,echo=FALSE>>= opts_knit$set(root.dir="../Code/") read_chunk("../Code/testit.py") @ <<testit,engine='python'>>= @ \end{document} 

outputs:

 ## /Users/blah/foo/bar/Paper 

root.dir has no obvious effect here, as it would be for R chunk, and I don't know what else you can try to change the directory for a piece of Python. This is a problem because I want the python script to work with files in the Code directory, but of course they cannot be found. I could hardcode the directory change in a python script, but that seems fragile. I would even be happy to pass the root directory directly to the python interpreter as an argument to the script, this was possible (but I don't think so, because knitr uses python -c?). Any thoughts on how to handle this? My google-fu failed me here ...

+4
source share
1 answer

Sorry, I did not pay enough attention to the path problem for engines other than R This problem has now been fixed in the development version on Github.

+6
source

All Articles