How to show working directory in line R?

I would like to see the current working directory at the prompt of my console R. When using options(prompt=paste(getwd(),">> ")) , the working directory is displayed at the beginning of the session. But it never updates when I change the working directory during this session:

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

What I'm doing at the moment is to override the setwd function in my .Rprofile

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

Now the tooltip is updated correctly when I call setwd . My question is: is there a more elegant way to dynamically update a tooltip, regardless of the function I call, and without having to override the base functions?

+7
r command-prompt
source share
1 answer

Because the prompt option is just a string, without any special directives evaluated internally (as opposed to a shell prompt), you must change it if you change the working directory to get the current working directory inside.

The solution you use seems to me the best. A bit hacked, but any solution will be the way you want to implement something completely fundamental that is not supported by R.

In addition, you do not need to be afraid of the functions performed by base::setwd under the hood, which will make your online synchronization work with the real working directory. This does not happen in practice. As noted in Thomas's comments, there may be no basic functions (other than source ) calling setwd . The only functions related to building and installing packages. And I noticed that even in source and usually in other functions, setwd used as owd <- setwd(dir); on.exit(setwd(owd)) owd <- setwd(dir); on.exit(setwd(owd)) , so the working directory goes back to the original when the function ends.

+3
source share

All Articles