Setup
Inspired from this question
I have some tools in a package called consoleR
.
Inside this package, a setwd
funciton is masking base::setwd
in order to display the current directory in the prompt like this:
## file consoleR/R/setwd.R
#' @export
setwd <- function(...) {
base::setwd(...)
current_dir <- basename(getwd())
options(prompt = paste(current_dir, " > "))
}
I load that package every session in my .Rprofile
:
## file ~/.Rprofile
if(interactive()) {
library(consoleR, warn.conflicts = FALSE)
setwd(getwd()) ## initializing the prompt
}
Issue
When I start R from a nix-terminal everything works fine, but when I start RStudio I got an error I cannot explain:
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
Error in base::setwd(...) : character argument expected
project_dir >
Effort
Then I use traceback()
and I get:
project_dir > traceback()
5: base::setwd(...) at setwd.R#10
4: setwd(owd)
3: .rs.onAvailablePackagesStale(reposString)
2: .rs.availablePackages()
1: .rs.rpc.discover_package_dependencies("353B2D06", ".R")
rstudio-available-packages-3178bc4c5ea9a2 >
Note that the prompt is showing another directory than the current one BEFORE and AFTER the call to traceback
.
The current dir is in fact my $HOME
dir.
What could be going on?