获取 R 脚本的路径 [英] Getting path of an R script

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

问题描述

有没有办法以编程方式在脚本本身中找到 R 脚本的路径?

Is there a way to programmatically find the path of an R script inside the script itself?

我之所以这么问是因为我有几个脚本使用 RGtk2 并从 .glade 文件加载 GUI.

I am asking this because I have several scripts that use RGtk2 and load a GUI from a .glade file.

在这些脚本中,我必须在开头放置一个 setwd("path/to/the/script") 指令,否则 .glade 文件(在同一目录中)将找不到.

In these scripts I am obliged to put a setwd("path/to/the/script") instruction at the beginning, otherwise the .glade file (which is in the same directory) will not be found.

这很好,但如果我将脚本移动到不同的目录或另一台计算机,我必须更改路径.我知道,这没什么大不了的,但如果有这样的东西就好了:

This is fine, but if I move the script in a different directory or to another computer I have to change the path. I know, it's not a big deal, but it would be nice to have something like:

setwd(getScriptPath())

那么,是否存在类似的功能?

So, does a similar function exist?

推荐答案

使用 source("yourfile.R", chdir = T)

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

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