4

Is there a keyboard shortcut in Rstudio to insert inline R code in RMarkDown documents?

ctr + alt + i is inserting a new code chunk. Similiar to that, it would be nice to have a keyboard shortcut to insert r (inline R code).
I checked Tools/Modify Keyboard Shortcuts in Rstudio but I can't find any helpful command there.

Also, browsing cheatsheets and documentations didn't help me either.

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
maxpe
  • 344
  • 2
  • 12

4 Answers4

4

One solution could be defining a code snippet.

  1. Choose Global Options from Tools Menu.
  2. Jump to the Code section.
  3. Under the Editing Tab look for Snippets and click Edit Snippets
  4. Choose Markdown. It will display existing snippets. At the end define your own snippet as shown in the image below:

Code Snippet
5. Click Save and you are done.

Typing inr followed by Shift+tab should insert the inline r code snippet.

Note: I could not control the cursor position for some reason, It would be ideal to position cursor before the last back tick.

Another solution could be writing a small Addin. See this RStudio Addins for more info

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
Imran Ali
  • 2,223
  • 2
  • 28
  • 41
  • 1
    Your snippet solution seems to work in R scripts but not in RMarkdown documents in RStudio (Version 1.0.136). So, I decided to create a small Rstudio Addin and assigned a shortcut ('ctrl + alt + u') to it. Works exactly like I wanted. Thank you very much for your suggestion! – maxpe Feb 13 '17 at 11:04
  • It would be wonderful if you could post the link to the Rstudio Addin you created here. – Imran Ali Feb 13 '17 at 11:07
  • @maxpe The zip Folder seems to be empty. Mind updating the Link? This missing feature has been driving me crazy! – Ratnanil Mar 31 '18 at 09:41
  • sry for the broken link, i have access to the original file in a few days and i will upload it again – maxpe Apr 01 '18 at 19:48
  • i am not able to find the file anymore but below @Ratnanil links to a much more advanced package that also provides the same function as my simple addin – maxpe Apr 16 '18 at 10:40
4

This is probably no longer helpful to the OP, but since I was searching for the same solution and didn't quite find it in the remedy package posted above, figured I'd share the snippet that finally worked for me:

`r `r \`${1:text}`

Key was to include the syntax to be printed within inline r code. Needed to additionally escape a backtick before the text argument, which I don't fully understand, but that is what worked.

user1864652
  • 53
  • 1
  • 4
2

There actually is a packaged addin on github specifically for this use case.

install.packages("devtools")
devtools::install_github("ThinkR-open/remedy")

# if you want to have the package update the hotkey settings
remedy::set_hotkeys 

https://github.com/ThinkR-open/remedy

Ratnanil
  • 1,641
  • 17
  • 43
1

Related to my answer to a similar question you can combine a working R Markdown Snippet (as suggested by @user1864652) with the shrtcts package to add a keybinding to the snippet:

  1. Add a name to the Snippet such as inl (for inline code):

    snippet inl
        `r `r \`${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 name of the new Snippet from Step 1:

    #' Inline R Code
    #'
    #' @description
    #'   If Editor has selection, transform current selection to inline R code.
    #'   If Editor has no selection, write new inline R code.
    #' @interactive
    #' @shortcut Cmd+Shift+I
    function() {
      if (rstudioapi::selectionGet()$value == "") {
        rstudioapi::insertText("inl")
        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("`r {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+shift+i without selection to place your cursor right before the second backtick of the inline code and press Tab to continue writing after the second backtick. Or you can select text and then press cmd+shift+i to transform the selected text to inline code.

The solution above can be easily adapted for bold, italic or monospace (code font) text in RMarkdown documents or to write within Dollar signs to add Inline Latex Math. You simply have to replace the snippet name in the line

rstudioapi::insertText("inl") 

and modify the desired output in the line

stringr::str_glue("`r {selected_text}`")

to e.g. stringr::str_glue("**{selected_text}**") for bold text.

Joel Beck
  • 318
  • 2
  • 6