Root directory for python chunk in knitr?

让人想犯罪 __ 提交于 2019-12-10 16:58:18

问题


I hope this isn't as simple as it feels. I've got a basic directory set up of:

root --> Paper
     --> Code

for a 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 R). So it would be something like this:

Python script testit.py

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

knitr document test.Rnw saved in the Code directory:

\documentclass{article}
\begin{document}

<<setup,echo=FALSE>>=
read_chunk("testit.py")
@

<<testit,engine='python'>>=
@

\end{document}

When I compile to PDF, the answer is right:

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

But document test-2.Rnw 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 like it would for an R chunk, and I don't know what else to try to change the directory for the Python chunk. This is a problem, because I would like the python script to operate on files in the Code directory, but of course it can't find them. I could hard code a directory change in the python script, but that seems fragile. I'd even be happy passing the root directory directly to the python interpreter as an argument to the script, were that possible (but I don't think so, because knitr uses python -c ?). Any thoughts on how to handle this? My google-fu has failed me here...


回答1:


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



来源:https://stackoverflow.com/questions/15512545/root-directory-for-python-chunk-in-knitr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!