获取R脚本的path

有没有一种方法来编程在脚本本身内find一个R脚本的path?

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

在这些脚本中,我必须在开始处放置一个setwd("path/to/the/script")指令,否则将不会find.glade文件(位于同一目录中)。

这很好,但如果我将脚本移动到另一个目录或另一台计算机,我必须更改path。 我知道,这不是什么大不了的事情,但有这样的事情会很好:

setwd(getScriptPath())

那么,有没有类似的function?

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

这适用于我:

 getSrcDirectory(function(x) {x}) 

这在脚本内部定义了一个匿名函数(什么都不做),然后确定该函数的源目录,该目录就是脚本所在的目录。

仅适用于RStudio:

 setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) 

这在运行 ning或文件时有效。

利用Rscript的隐含的“–file”参数

当使用“Rscript”( Rscript doc )调用脚本时,脚本的完整path作为系统参数给出。 下面的函数利用这个来提取脚本目录:

 getScriptPath <- function(){ cmd.args <- commandArgs() m <- regexpr("(?<=^--file=).+", cmd.args, perl=TRUE) script.dir <- dirname(regmatches(cmd.args, m)) if(length(script.dir) == 0) stop("can't determine script dir: please call the script with Rscript") if(length(script.dir) > 1) stop("can't determine script dir: more than one '--file' argument detected") return(script.dir) } 

如果将代码包装在一个包中,则可以随时查询包目录的一部分。
这里是一个RGtk2包的例子:

 > system.file("ui", "demo.ui", package="RGtk2") [1] "C:/opt/R/library/RGtk2/ui/demo.ui" > 

你可以在目录inst/glade/中执行相同的目录,这将成为已安装软件包中的目录glade/ system.file()将为你安装时计算path,而不pipe操作系统如何。

这个答案对我很好:

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

注意:脚本必须来源为了返回正确的path

我发现它在: https : //support.rstudio.com/hc/communities/public/questions/200895567-can-user-obtain-the-path-of-current-Project-s-directory-

但是我还是不明白什么是sys.frame(1)$ ofile。 在R Documentation中我没有find任何有关这方面的信息。 有人可以解释吗?

 #' current script dir #' @param #' @return #' @examples #' works with source() or in RStudio Run selection #' @export z.csd <- function() { # http://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script # must work with source() if (!is.null(res <- .thisfile_source())) res else if (!is.null(res <- .thisfile_rscript())) dirname(res) # http://stackoverflow.com/a/35842176/2292993 # RStudio only, can work without source() else dirname(rstudioapi::getActiveDocumentContext()$path) } # Helper functions .thisfile_source <- function() { for (i in -(1:sys.nframe())) { if (identical(sys.function(i), base::source)) return (normalizePath(sys.frame(i)$ofile)) } NULL } .thisfile_rscript <- function() { cmdArgs <- commandArgs(trailingOnly = FALSE) cmdArgsTrailing <- commandArgs(trailingOnly = TRUE) cmdArgs <- cmdArgs[seq.int(from=1, length.out=length(cmdArgs) - length(cmdArgsTrailing))] res <- gsub("^(?:--file=(.*)|.*)$", "\\1", cmdArgs) # If multiple --file arguments are given, R uses the last one res <- tail(res[res != ""], 1) if (length(res) > 0) return (res) NULL } 

如何使用系统和shell命令? 在Windows中,我认为当你在RStudio中打开脚本时,它将当前shell目录设置为脚本的目录。 您可能需要添加cd C:\ eg或任何您想要search的驱动器(例如shell('dir C:\\ * file_name / s',intern = TRUE) – \\来转义转义字符)。 除非您进一步指定子目录(对于Linux,我从/开始search),否则只能用于唯一命名的文件。 在任何情况下,如果你知道如何在shell中find某个东西,这就提供了一个在R中find它的布局并返回目录。 应该工作,无论你是采购还是运行脚本,但我还没有充分探索潜在的错误。

 #Get operating system OS<-Sys.info() win<-length(grep("Windows",OS)) lin<-length(grep("Linux",OS)) #Find path of data directory #Linux Bash Commands if(lin==1){ file_path<-system("find / -name 'file_name'", intern = TRUE) data_directory<-gsub('/file_name',"",file_path) } #Windows Command Prompt Commands if(win==1){ file_path<-shell('dir file_name /s', intern = TRUE) file_path<-file_path[4] file_path<-gsub(" Directory of ","",file_path) filepath<-gsub("\\\\","/",file_path) data_directory<-file_path } #Change working directory to location of data and sources setwd(data_directory)