Reputation: 4339
When you install Git on windows, it adds a context menu option when you right-click on a folder to "Git Bash Here". The way it does this is by adding a registry key like this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\git_shell\command]
@="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%1\""
Notice the cd
argument at the end that passes the directory name to the program.
I would like to do something similar for R (and other programs). Unfortunately R doesn't accept a cd
argument. This will launch R:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\R\command]
@="\"C:\\Program Files\\R\\R-3.4.3\\bin\\x64\\Rgui.exe\" \"--cd=%1\""
but it gives an error message saying the cd
argument is not recognized, and Rgui will start with whatever the default working directory is, defeating the entire point.
What I really want it to do is the equivalent of this command:
start "R" /D %1 "C:\Program Files\R\R-3.4.3\bin\x64\Rgui.exe"
where %1
is the folder that was right-clicked on. Is this possible?
Upvotes: 0
Views: 401
Reputation: 5650
You can write R code that runs on startup and checks the commandline arguments.
You can put the following code at the end of C:\Program Files\R\R-3.4.3\etc\Rprofile.site
(or any other file that gets executed at startup):
local({
processArg <- function(arg) {
parts <- strsplit(arg, "=")[[1]]
if (length(parts) == 2) {
if (parts[1] == "R_startup_wd") {
setwd(parts[2])
}
}
}
invisible(sapply(commandArgs(FALSE), processArg))
})
It checks if R was called with an argument R_startup_wd=your_working_dir
and changes the working directory if yes.
You can then call R like
"C:\Program Files\R\R-3.4.3\bin\x64\Rgui.exe" "R_startup_wd=your_working_dir"
Note that the argument name is given without "--", i.e. we have R_startup_wd
and not --R_startup_wd
. Otherwise RGui will complain about "unknown arguments"
You can of course still use R without the argument given.
Upvotes: 3