R-posts.com

WrappR for Rstudio: Use Keyboard Shortcuts to Wrap Highlighted Text With Custom Code

Interested in publishing a one-time post on R-bloggers.com? Press here to learn how.
Introduction:
While coding in RStudio I wanted to use the RStudio keyboard shortcuts to wrap functions and custom lines of code around datasets, code, or objects in the editor pane.  I could not find what I wanted after reviewing various packages and solutions.  Combining the RStudio API, the shrtcts package, and an HTA interface config tool, I made my own solution.

Example:
Type mtcars$mpg in the editor pane, highlight that line, press Ctrl+T, and next you will have table(mtcars$mpg) with less typing. Using WrappR, the “table(” and “)” are wrapped around mtcars$mpg after pressing the assigned keyboard shortcut.

Getting Started:

1. Requirements Windows PC
(WrappR is not ready for Linux or Mac yet)
The following software need to be installed on the PC:
– R
– Rstudio
– gadenbuie/shrtcts package* (installed in R)

install.packages(“devtools”)
library(“devtools”)
remotes::install_github(“gadenbuie/shrtcts”)

2. WrappR Setup and operation (assuming R, Rstudio, and the gadenbuie/shrtcts are installed already):
b. Unzip and place the files in a folder location to launch the WrappR tool from.
c. Launch the WrappR.hta file and you should see one entry for wrappinc.
d. Included in the zip file is a “.shrtcts.yaml” file. If you want to use a pre-defined set of WrappR entries, then place this file in the ‘C:\Users\%username%\.config’ folder. If it doesn’t exist, then please create the .config in your root windows profile folder.

3. Container Layout:



ROW ONE – Text name. This text is searchable from the Ctrl+F find function.

ROW TWO – Editable name field for the User-Defined-Code (UDC), along with management buttons. Use the Duplicate button to make a new UDC.
(Note: UDC names can’t contain whitespaces and conflicts occur when UDCs have identical names)

ROW THREE – User-Defined-Code (UDC). Your highlighted text in the Rstudio editor pane will be interposed between text from the left and right fields defined here.

ROW FOUR – Assigned keyboard shortcut.  Ctrl, Alt, Shift, and an alpha character can be used in assignments.  Use different combinations of those function keys with an alpha character to create more assignments. 

Examples:
Ctrl+D can be used to produce data.frame()
Ctrl+Shift+D can be used to produce as.data.frame()

Save & Search:



Pressing the Save button will save all your entries to an essential file found in this path. C:\Users\username\.config\.shrtcts.yaml

Type in any text to search among your entries. To revert, clear the search field. You may also need to press the Enter key on your keyboard to return all the entries.

Lastly, run the following code and then close and re-open RStudio to utilize your new shortcut.

if (interactive() && requireNamespace(“shrtcts”, quietly = TRUE)) {
shrtcts::add_rstudio_shortcuts(set_keyboard_shortcuts = TRUE)
}


*Aden-Buie G (2022). shrtcts: Make Anything an RStudio Shortcut. https://pkg.garrickadenbuie.com/shrtcts, https://github.com/gadenbuie/shrtcts.
Exit mobile version