Search code examples
rrstudioadd-in

Is there an Rstudio shortcut key for ``?


I use it a lot on Rmarkdow for referring to code, so I created an Addin, but wanted to know if there's a shortcut. If it isn't the case, how could I do to configure the addin so when calling it, the position of the caret or cursor stands between both symbols, exactly as it happens when using "" or () in RStudio.

insertInAddin <- function() { rstudioapi::insertText("``") } is the code I used for the Add-in

I'm looking help understanding how to setup

rstudioapi::setCursorPosition() and document_position() inside the location argument of insertText.


Solution

  • You can use the shrtcts package for this task. It lets you assign Keyboard Shortcuts to arbitrary R Code.

    1. Create a new R Markdown Snippet which can also be used on its own by typing e.g. in (for inline code font) and press Shift+Tab:

      snippet in
          `${1}`$0
      
    2. Use the command shrtcts::edit_shortcuts() in the RStudio Console to open the file where you define your custom shortcuts.

    3. Paste the following code inside that file (set your preferred keybinding in the @shortcut line). Note that the inserted text in the second line of the function must match the new Snippet from Step 1:

      #' Code Font
      #'
      #' @description
      #'   If Editor has selection, transform current selection to code font.
      #'   If Editor has no selection, write between backticks.
      #' @interactive
      #' @shortcut Cmd+E
      function() {
        if (rstudioapi::selectionGet()$value == "") {
          rstudioapi::insertText("in")
          rstudioapi::executeCommand("insertSnippet") |>
            capture.output() |>
            invisible()
        } else {
            # Gets The Active Document
            ctx <- rstudioapi::getActiveDocumentContext()
      
            # Checks that a document is active
            if (!is.null(ctx)) {
      
            # Extracts selection as a string
            selected_text <- ctx$selection[[1]]$text
      
            # modify string
            selected_text <- stringr::str_glue("`{selected_text}`")
      
            # replaces selection with string
            rstudioapi::modifyRange(ctx$selection[[1]]$range, selected_text)
          }
        }
      }
      

      This solution uses the native pipe |> and thus requires R 4.1. You can of course just define separate variables in each line or use the magrittr pipe if you use earlier versions of R. Further the stringr::str_glue() command can be easily replaced with a base R solution to avoid dependencies.

    4. Use the command shrtcts::add_rstudio_shortcuts(set_keyboard_shortcuts = TRUE) in the RStudio Console to add the new shortcut with its assigned keybinding. Then restart RStudio.

    Now you can use e.g. cmd+e without selection to place your cursor within backticks and press Tab to continue writing after the second backtick. Or you can select text and then press cmd+e to surround the selected text by backticks.

    The solution above can be easily generalized for bold and italic text in RMarkdown documents or to write within Dollar signs to add Inline Latex Math.