如何在R提示符下显示工作目录? [重复]

时间:2020-12-26 23:11:53

This question already has an answer here:

这个问题在这里已有答案:

I would like to see the current working directory in the prompt of my R console. When using options(prompt=paste(getwd(),">> ")) the working directory at the start of the session is shown. But it never gets updated when I change the working directory during that session:

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

/home/sieste >> setwd("newdir")
/home/sieste >> cat("damn!\n")

What I do at the moment is to redefine the setwd function in my .Rprofile

我现在所做的是重新定义我的.Rprofile中的setwd函数

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

Now the prompt gets updated correctly whenever I call setwd. My question is: Is there a more elegant way of updating the prompt dynamically, independent of which function I call and without having to redefine base functions?

现在,每当我调用setwd时,提示都会正确更新。我的问题是:是否有更优雅的方式动态更新提示,独立于我调用的函数,而无需重新定义基本函数?

1 个解决方案

#1


3  

Because prompt option is really just a string, without any special directives evaluated inside (unlike shell prompt), you have to change it if you change working directory to get current working directory inside.

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

The solution you use seems the best to me. A bit hacky, but any solution will be as you want to implement something quite fundamental that is not supported by R itself.

您使用的解决方案对我来说似乎是最好的。有点hacky,但任何解决方案将是你想要实现R本身不支持的非常基本的东西。

Moreover, you don’t have to fear functions executing base::setwd under the hood, which would get your prompt out of sync with real working directory. That does not happen in practice. As Thomas pointed out in the comments, probably no base functions (other than source) call setwd. The only functions that do are related to package building and installation. And I noticed that even in source and usually in the other functions, setwd is used like owd <- setwd(dir); on.exit(setwd(owd)), so that the working directory is set back to original when the function finishes.

此外,您不必担心在底层执行base :: setwd的函数会导致您的提示与实际工作目录不同步。这在实践中不会发生。正如Thomas在评论中指出的那样,可能没有基本函数(除了源代码)调用setwd。唯一的功能与包构建和安装有关。我注意到即使在源代码中,通常在其他函数中,setwd也像owd < - setwd(dir)一样使用; on.exit(setwd(owd)),以便在函数完成时将工作目录设置回原始状态。

#1


3  

Because prompt option is really just a string, without any special directives evaluated inside (unlike shell prompt), you have to change it if you change working directory to get current working directory inside.

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

The solution you use seems the best to me. A bit hacky, but any solution will be as you want to implement something quite fundamental that is not supported by R itself.

您使用的解决方案对我来说似乎是最好的。有点hacky,但任何解决方案将是你想要实现R本身不支持的非常基本的东西。

Moreover, you don’t have to fear functions executing base::setwd under the hood, which would get your prompt out of sync with real working directory. That does not happen in practice. As Thomas pointed out in the comments, probably no base functions (other than source) call setwd. The only functions that do are related to package building and installation. And I noticed that even in source and usually in the other functions, setwd is used like owd <- setwd(dir); on.exit(setwd(owd)), so that the working directory is set back to original when the function finishes.

此外,您不必担心在底层执行base :: setwd的函数会导致您的提示与实际工作目录不同步。这在实践中不会发生。正如Thomas在评论中指出的那样,可能没有基本函数(除了源代码)调用setwd。唯一的功能与包构建和安装有关。我注意到即使在源代码中,通常在其他函数中,setwd也像owd < - setwd(dir)一样使用; on.exit(setwd(owd)),以便在函数完成时将工作目录设置回原始状态。