我如何在 vscode 中从 R 中获得 magrittr 管道的别名

Ale*_*lex 2 r vscode-settings

我想要一个别名来输入%>%vscode(R 中的管道命令)。在 Rstudio 中,这被映射到 ctrl + shift + M,但如果由于任何原因在 vscode 中不可用,我很乐意映射到其他东西,我只是不确定如何添加新别名。

arn*_*nle 11

也可以通过以下方式添加到rmd中

{
  "key": "Ctrl+Shift+m",
  "command": "type",
  "args": { "text": " %>% " },
  "when": "editorTextFocus && editorLangId == rmd"
}
Run Code Online (Sandbox Code Playgroud)

并通过以下方式连接到 R 终端

    {
      "key": "Ctrl+Shift+m",
      "command": "workbench.action.terminal.sendSequence",
      "args": { "text": " %>% " },
      "when": "terminalFocus"
    },
Run Code Online (Sandbox Code Playgroud)

这就是我的 settings.json 添加 %>% 和 <- 到我的 Rmarkdowns、Rscripts 和 R 终端(包括弧度)的方式:

[
    // OTHER KEYBINDINGS,


 
    // keybindings for R scripts. 
    {
      "key": "Ctrl+Shift+m",
      "command": "type",
      "args": { "text": " %>% " },
      "when": "editorTextFocus && editorLangId == r"
    },
    {
      "key": "Alt+-",
      "command": "type",
      "args": { "text": " <- " },
      "when": "editorTextFocus && editorLangId == r"
    },
    // keybindings for Rmarkdown
    {
      "key": "Ctrl+Shift+m",
      "command": "type",
      "args": { "text": " %>% " },
      "when": "editorTextFocus && editorLangId == rmd"
    },
    {
      "key": "Alt+-",
      "command": "type",
      "args": { "text": " <- " },
      "when": "editorTextFocus && editorLangId == rmd"
    },
    // keybindings for R terminal (radian included)
    {
      "key": "Ctrl+Shift+m",
      "command": "workbench.action.terminal.sendSequence",
      "args": { "text": " %>% " },
      "when": "terminalFocus"
    },
    {
      "key": "Alt+-",
      "command": "workbench.action.terminal.sendSequence",
      "args": { "text": " <- " },
      "when": "terminalFocus"
    },
    // OTHER KEYBINDINGS

]



Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,可以使用正则表达式来匹配多种语言,因此您不需要重复 R 和 Rmd 的命令。使用 `"when": "editorTextFocus &amp;&amp; editorLangId =~ /r|rmd/"` 代替。 (5认同)
  • `"when": "editorTextFocus &amp;&amp; editorLangId =~ /r|rmd|quarto/"` 会将其扩展为包括四开本。 (4认同)

Mal*_*udo 7

您只需要将此添加到您的keybindings.json文件中(请参阅此处了解如何打开它):

{
  "key": "Ctrl+Shift+m",
  "command": "type",
  "args": { "text": " %>% " },
  "when": "editorTextFocus"
}
Run Code Online (Sandbox Code Playgroud)

这样你就不需要宏


keybindings.json 修改后的文件:

// Place your key bindings in this file to override the defaults
[
    {
        "key": "Ctrl+Shift+m",
        "command": "type",
        "args": { "text": " %>% " },
        "when": "editorTextFocus"
    }
]
Run Code Online (Sandbox Code Playgroud)