You can, but I believe you'll have to restart the IDE for any changes to keybindings done in this way to take effect. There's no user-accessible way (beyond the Modify Keyboard Shortcuts dialog) to refresh the set of loaded user keybindings at the moment.
@kevinushey, @jonathan i wrote a package to overcome this issue and uploaded it to CRAN, but got an email that CRAN cant allow a package that writes to disk other than tempdir.
Dear Jonathan Sidi,
your package creates ~/.R/rstudio/keybindings:
$ grep -r 'rstudio.*keybindings' .
./rsam/R/rm_shorcut.R: path="~/.R/rstudio/keybindings/addins.json",
./rsam/R/set_shortcut.R: path="~/.R/rstudio/keybindings/addins.json",
./rsam/R/fetch_keys.R: json_now <- jsonlite::fromJSON("~/.R/rstudio/keybindings/addins.json")
./rsam/R/zzz.R: key_dir <- '~/.R/rstudio/keybindings'
./rsam/R/zzz.R: dir.create('~/.R/rstudio/keybindings',showWarnings = FALSE,recursive = TRUE)
./rsam/R/zzz.R: key_dir <- '~/.R/rstudio/keybindings'
./rsam/R/zzz.R: dir.create('~/.R/rstudio/keybindings',showWarnings = FALSE,recursive = TRUE)
Please do not create directories/documents outside of tempdir(). This is not allowed by CRAN policies.
Please fix this immediately and resubmit a new version with increased version number via the CRAN web form.
Best,
CRAN
CRAN typically allows users to do this in interactive processes. From the CRAN Repository Policy:
Packages should not write in the user’s home filespace (including clipboards), nor anywhere else on the file system apart from the R session’s temporary directory (or during installation in the location pointed to by TMPDIR: and such usage should be cleaned up). Installing into the system’s R installation (e.g., scripts to its bin directory) is not allowed.
Limited exceptions may be allowed in interactive sessions if the package obtains confirmation from the user.
Can you wrap the associated code in an interactive() check + prompt the user beforehand?