获取当前脚本的路径 [英] Get the path of current script
问题描述
我想以编程方式将工作目录设置为当前脚本的路径,但首先我需要获取当前脚本的路径.
所以我希望能够做到:
current_path = ...检索当前脚本的路径 ...setwd(current_path)
就像 RStudio 菜单一样:
到目前为止我尝试过:
initial.options <- commandArgs(trailingOnly = FALSE)file.arg.name <- "--file="script.name <- sub(file.arg.name, "", initial.options[grep(file.arg.name, initial.options)])script.basename <- 目录名(script.name)
<块引用>
script.name 返回NULL
source("script.R", chdir = TRUE)
<块引用>
退货:文件错误(文件名,r",编码 = 编码):无法打开连接另外:警告消息:在文件(文件名,r",encoding = encoding) : 无法打开文件 '/script.R': 没有这样的文件或目录
dirname(parent.frame(2)$ofile)
<块引用>
返回:dirname(parent.frame(2)$ofile) 中的错误:应为字符向量参数...因为 parent.frame 为空
frame_files <- lapply(sys.frames(), function(x) x$ofile)frame_files <- 过滤器(否定(is.null),frame_files)路径 <- 目录名(frame_files[[length(frame_files)]])
<块引用>
返回:空,因为 frame_files 是一个 0 的列表
thisFile <- function() {cmdArgs <- commandArgs(trailingOnly = FALSE)针 <- "--file="匹配 <- grep(needle, cmdArgs)如果(长度(匹配)> 0){# 脚本return(normalizePath(sub(needle, "", cmdArgs[match])))} 别的 {# 'source'd 通过 R 控制台返回(normalizePath(sys.frames()[[1]]$ofile))}}
<块引用>
返回:path.expand(path) 中的错误:'path' 参数无效
我也看到了来自这里的所有答案,此处,此处 和 此处.没有快乐.
使用 RStudio 1.1.383
如果不需要外部库来实现这一点就好了.
在 RStudio 中,您可以使用
获取当前显示在源窗格中的文件的路径rstudioapi::getSourceEditorContext()$path
如果你只想要目录,使用
dirname(rstudioapi::getSourceEditorContext()$path)
如果您想要source(filename)
运行的文件的名称,那就有点难了.您需要在堆栈中的某处查找变量 srcfile
.向后多远取决于你如何写东西,但它大约后退 4 步:例如,
fi <- tempfile()writeLines("f()", fi)f <- function() 打印(sys.frame(-4)$srcfile)来源(fi)菲
应该在最后两行打印相同的内容.
I would like to set the working directory to the path of current script programmatically but first I need to get the path of current script.
So I would like to be able to do:
current_path = ...retrieve the path of current script ...
setwd(current_path)
Just like the RStudio menu does:
So far I tried:
initial.options <- commandArgs(trailingOnly = FALSE)
file.arg.name <- "--file="
script.name <- sub(file.arg.name, "", initial.options[grep(file.arg.name, initial.options)])
script.basename <- dirname(script.name)
script.name returns NULL
source("script.R", chdir = TRUE)
Returns: Error in file(filename, "r", encoding = encoding) : cannot open the connection In addition: Warning message: In file(filename, "r", encoding = encoding) : cannot open file '/script.R': No such file or directory
dirname(parent.frame(2)$ofile)
Returns: Error in dirname(parent.frame(2)$ofile) : a character vector argument expected ...because parent.frame is null
frame_files <- lapply(sys.frames(), function(x) x$ofile)
frame_files <- Filter(Negate(is.null), frame_files)
PATH <- dirname(frame_files[[length(frame_files)]])
Returns: Null because frame_files is a list of 0
thisFile <- function() {
cmdArgs <- commandArgs(trailingOnly = FALSE)
needle <- "--file="
match <- grep(needle, cmdArgs)
if (length(match) > 0) {
# Rscript
return(normalizePath(sub(needle, "", cmdArgs[match])))
} else {
# 'source'd via R console
return(normalizePath(sys.frames()[[1]]$ofile))
}
}
Returns: Error in path.expand(path) : invalid 'path' argument
Also I saw all answers from here, here, here and here. No joy.
Working with RStudio 1.1.383
EDIT: It would be great if there was no need for an external library to achieve this.
In RStudio, you can get the path to the file currently shown in the source pane using
rstudioapi::getSourceEditorContext()$path
If you only want the directory, use
dirname(rstudioapi::getSourceEditorContext()$path)
If you want the name of the file that's been run by source(filename)
, that's a little harder. You need to look for the variable srcfile
somewhere back in the stack. How far back depends on how you write things, but it's around 4 steps back: for example,
fi <- tempfile()
writeLines("f()", fi)
f <- function() print(sys.frame(-4)$srcfile)
source(fi)
fi
should print the same thing on the last two lines.
这篇关于获取当前脚本的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!