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.
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.
1. Requirements Windows PC
(WrappR is not ready for Linux or Mac yet)
The following software need to be installed on the PC:
– gadenbuie/shrtcts package* (installed in R)2. WrappR Setup and operation (assuming R, Rstudio, and the gadenbuie/shrtcts are installed already):
a. Download the zip files from https://archive.org/download/wrappr/WrappR.zip.
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.
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.
*Aden-Buie G (2022). shrtcts: Make Anything an RStudio Shortcut. https://pkg.garrickadenbuie.com/shrtcts, https://github.com/gadenbuie/shrtcts.
WrappR for Rstudio: Use Keyboard Shortcuts to Wrap Highlighted Text With Custom Code was first posted on April 28, 2023 at 1:20 pm.