quant
quant

Reputation: 4482

Create macros/shortcuts in R(studio)

Is there a way to write your own macro in R(studio). Many times in my code i need to comment out a line/piece of code, run it, and then comment it in again. I know that a shortcut to comment in a line/piece of code is ctrl+shift+c, so I would like to create a shortcut e.g. ctrl+alt+c to comment in/out+run+comment in/out

Upvotes: 1

Views: 3535

Answers (2)

YCR
YCR

Reputation: 4002

You can create an Addins doing exactly what you want and assign a shortcut to it.

For exemple, that Addin is used to create upgraded shortcut for pipe.

To do what you want, a solution may be possible but require a few constrain.

Three, at least:

  • First, you need to select the code you want to comment
  • Secondly, in the same time you use the shortcut, the file should be saved.
  • Thirdly in your file, you can't have two time the same selected piece of code.

The commented code will be the input of the Addins.

The code is as simple as read your file, replace the code by commented code, then run the modified code.

It is different in what you want to achieve in the sense of in that case, it is more create a temporary copy of the code, comment the undesirable code, run the temporary modified code.

Upvotes: 2

Christoph
Christoph

Reputation: 7063

For shortcuts see here. Macros might be found here.

E.g. commenting in/out = Ctrl +Shift + C (Both)

enter image description here

If you want a to have a new shortcut, you have to ask RStudio. For an example, where it was already solved, see here. From the list of available shortcuts it is clear that "your" shortcut does not exist.

Upvotes: 1

Related Questions