I noticed a painpoint while working in RStudio: I didn’t have the ’tabout’ functionality I had in Obsidian and VScode.
“There must be a way to do this! 🤔” I thought.
Indeed, there was! So, here’s how I did it (and how you can, too).
Credits to Matthias Nistler for this wonderful post on creating custom shortcuts in R.
Create R Package#
- Go to “New Project”
- Choose “New Directory” then “R Package”
- Give your package a cool name. I named mine
codersnest
- Pick a project location.
This creates a hello.R
file and hello
function.
Delete the hello.R
file in your project folder (mine is ~\Documents\codersnest
) and hello.Rd
in the man
subfolder (i.e. ~\Documents\codersnest\man
)
Create Addin#
Install Dependencies#
In the console, run:
install.packages(c("usethis", "devtools", "rstudioapi"))
Define Addin Function#
Cool Trick#
Matthias showed a nifty trick to create new files.
In a new file (Ctrl+Shift+N
), type the code below, then press Ctrl+Enter
:
usethis::use_r("tabout")
This creates and focuses a new tabout.R
file. Pretty cool right?
Enough Playing#
Copy and paste the code below into tabout.R
tabout <- function() {
# Get the active document context
context <- rstudioapi::getActiveDocumentContext()
# Get the current selection
selection <- context$selection[[1]]
# Get the current cursor position
cursor_pos <- selection$range$end
line <- cursor_pos[1]
col <- cursor_pos[2]
# Get the text of the current line
line_text <- context$contents[line]
# Check if the cursor is just before a closing bracket
if (substr(line_text, col, col) %in% c(")", "]", "}")) {
# Move the cursor to the right of the closing bracket
rstudioapi::setCursorPosition(c(line, col + 1))
} else {
# Perform the usual Tab action if not before a closing bracket
rstudioapi::insertText(" ") # Insert two spaces
}
}
Save as an Addin#
Go to “File” > “New File” > “Text File” > paste the following:
Name: Tabout
Description: Enables Tabout functionality in R
Binding: tabout
Interactive: false
Matthias explained what each key is:
- Name is a short description of what the addin does. This will be displayed when you want to set the shortcut later.
- Description is a longer description of its functionality.
- Binding sets the name of the function that should be called by the shortcut.
- Interactive defines whether this addin is interactive (e.g., runs a Shiny application) or not.
This text file MUST be saved as addins.dcf
in the inst/rstudio
directory of your project folder (e.g. ~\Documents\codersnest\inst\rstudio\addins.dcf
).
Finalize Package#
In the console, run the following commands sequentially:
## Defines the rstudioapi dependency for your package
usethis::use_package("rstudioapi")
## Check for errors
devtools::check()
## Update/create your package
devtools::build()
# devtools::build() output
> [1] "C:/Users/Abdul-Hameed/Documents/codersnest_0.1.0.tar.gz"
At the top right corner, click the project name then “Close Project”:
Install Package#
Install the package with the command:
# Same path as the one above
install.packages("C:/Users/Abdul-Hameed/Documents/codersnest_0.1.0.tar.gz", repos = NULL)
Bind tabout()
to Tab
#
Go to “Tools” > “Modify Keyboard Shortcuts” > search “tabout” > set shortcut toTab
> “Apply” > 4+4!!!
CONGRATULAAAAATIONS!!! 🎉🥳😇