1

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

quant
  • 4,062
  • 5
  • 29
  • 70
  • If you are using commenting for conditional code execution so often you should probably think about rewriting your code to be more flexible. – Spacedman May 01 '17 at 19:14
  • @Spacedman No it is not that. For instance, in a script I have a function that calculates some "stuff" and it takes really long. I save the "stuff" in a csv so that i have them saved in order not to re-run the function again, and then i read the csv. But the read line for instance i have it commented out. Things like that – quant May 02 '17 at 07:58
  • You should turn your "scripts" into proper standalone functions. Then you only need to call the functions you need. Anyway, not the place for discussion of best practice methods here. – Spacedman May 02 '17 at 15:39

2 Answers2

2

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.

YCR
  • 3,794
  • 3
  • 25
  • 29
1

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.

Community
  • 1
  • 1
Christoph
  • 6,841
  • 4
  • 37
  • 89
  • I was aware about the first one (shortcuts), but unfortunately it is not what I need. Regarding the second one (macros), i see that snippets use R commands. But there is no R command for commenting in/out and for running a piece of code, right ? Please correct me if I am wrong – quant May 02 '17 at 08:01
  • But `ctrl_shift+c` is not an R command, is an RStudio shortcut, right ? I mean how could I use this in a snippet ? – quant May 02 '17 at 08:21
  • But comments are only useful in an editor? Why would you like to comment in the R-command line? – Christoph May 02 '17 at 10:09
  • Let me try to put it in a different way. I would like a shortcut, that allows me to run the following sequence of shortcuts in the editor in the line that my cursor is on: 1. shift+ctrl+c, 2. ctrl+r, 3.shift+ctrl+c. Is this possible with a snippet ? – quant May 02 '17 at 10:29
  • You have an .R file with a line `# Command`. Then you want to run the sequence above such that you can execute the line but you don't have to modify the line? (It just stays the same) – Christoph May 02 '17 at 11:05
  • In the case that i described above, the line in the end will not be affected, since it just commented out/in, run, commented in/out, so yes ! – quant May 02 '17 at 11:09
  • See my edit. I think the question is solved although it is not your desired result ;-) – Christoph May 02 '17 at 13:25