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

[This article was first published on R-posts.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.

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)
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.
To leave a comment for the author, please follow the link and comment on their blog: R-posts.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)