Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R提示符中显示工作目录?_R_Command Prompt - Fatal编程技术网

如何在R提示符中显示工作目录?

如何在R提示符中显示工作目录?,r,command-prompt,R,Command Prompt,我想在我的R控制台的提示符中看到当前的工作目录。使用选项(prompt=paste(getwd(),“>>”)时,将显示会话开始时的工作目录。但当我在该会话期间更改工作目录时,它从未更新: /home/sieste >> setwd("newdir") /home/sieste >> cat("damn!\n") 我现在要做的是在.Rprofile中重新定义setwd函数 setwd <- function(...) { base::setwd(...)

我想在我的R控制台的提示符中看到当前的工作目录。使用
选项(prompt=paste(getwd(),“>>”)时,
将显示会话开始时的工作目录。但当我在该会话期间更改工作目录时,它从未更新:

/home/sieste >> setwd("newdir")
/home/sieste >> cat("damn!\n")
我现在要做的是在.Rprofile中重新定义
setwd
函数

setwd <- function(...) {
  base::setwd(...)
  options(prompt=paste(getwd(),">> "))
}

setwd因为
prompt
选项实际上只是一个字符串,没有任何内部计算的特殊指令(与shell prompt不同),如果您更改工作目录以获取内部的当前工作目录,则必须更改它

对我来说,你使用的解决方案似乎是最好的。虽然有点不成熟,但任何解决方案都是因为您想要实现一些R本身不支持的非常基本的东西


此外,您不必担心函数在引擎盖下执行
base::setwd
,这会使提示符与实际工作目录不同步。这在实践中是不会发生的。正如注释中指出的,可能没有基本函数(除了
源代码
)。只有与包构建和安装相关的功能。我注意到,即使在
源代码
中,通常在其他函数中,
setwd
也像
owd一样使用。您的解决方案似乎足够优雅,我怀疑是否存在更简单的解决方案。您基本上有自己的解决方案,但您的问题中“独立于我调用的函数”的部分不清楚,并且“不必重新定义基函数”这一部分很容易解决,只需调用函数,而不是像
setwd\u和\u update\u提示符
之类的
setwd
,或者任何你想要的东西。@Thomas:假设我执行了一个函数,在引擎盖下调用
base::setwd()
并更改工作目录。我的解决方案当时不会更新提示符。我希望提示符在每个命令后都会更新,就像Linux shell一样。包含了注释中的大部分相关信息,并在
源代码上进行了一点扩展。即使
源代码
对您来说也不重要,因为它会更改工作目录这只是暂时的。