Knitr中python块的根目录? [英] Root directory for python chunk in knitr?

查看:92
本文介绍了Knitr中python块的根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不是想象中的那么简单.我已经建立了以下基本目录:

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

root --> Paper
     --> Code

我正在写的论文.我想从Paper目录中的knitr文档中调用Code目录中的Python脚本(类似 this ,但使用Python而不是R).所以会是这样的:

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脚本testit.py

Python script testit.py

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

编织文档test.Rnw保存在Code目录中:

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}

当我编译为PDF时,答案是正确的:

When I compile to PDF, the answer is right:

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

但是文件test-2.Rnw保存在Paper目录中:

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}

输出:

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

root.dir在这里没有像R块那样明显的作用,而且我不知道还有什么尝试改变Python块的目录的.这是一个问题,因为我希望python脚本对Code目录中的文件进行操作,但是当然找不到它们.我可以在python脚本中硬编码目录更改,但这似乎很脆弱.我什至很乐意将根目录直接传递给python解释器作为脚本的参数,但有可能吗(但我不这么认为,因为knitr使用python -c吗?).关于如何处理这个有什么想法吗?我的Google-fu在这里使我失败了...

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...

推荐答案

对不起,除了R以外,我对引擎的路径问题没有给予足够的重视.现在,此问题已在Github上的开发版本中得到解决.

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.

这篇关于Knitr中python块的根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆