Rscript:确定执行脚本的路径 [英] Rscript: Determine path of the executing script

查看:411
本文介绍了Rscript:确定执行脚本的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 foo.R 的脚本,其中包含另一个脚本 other.R 目录:

I have a script called foo.R that includes another script other.R, which is in the same directory:

#!/usr/bin/env Rscript
print("Hello")
source("other.R")

但是我想要 R 找到 other.R 无论当前工作目录是什么。

But I want R to find that other.R no matter what the current working directory.

换句话说, foo.R 需要知道自己的路径。我该怎么做?

In other words, foo.R needs to know its own path. How can I do that?

推荐答案

这里有一个简单的解决方案。此命令:

Here there is a simple solution for the problem. This command:

script.dir <- dirname(sys.frame(1)$ofile)

返回当前脚本文件的路径。在脚本保存后,它起作用。

returns the path of the current script file. It works after the script was saved.

这篇关于Rscript:确定执行脚本的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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