sc73 sc73 - 3 months ago 11
R Question

Shortcut for running a single line of code in R

Does anyone know how I could create a keyboard shortcut or something similar to run a single line of diagnostic code in R Studio? i.e. if I wanted to do something simple, like checking the dimensions of a data frame, but I wanted to do it a lot throughout the day and didn't want to be continually typing dim(data), how could I get dim(data) into a keyboard shortcut or some other quick easy way to call that single line of code?

Answer

R itself can’d do that. Your editor may be able to, though (I know that Vim + Vim-R can do something like this).

What you can do in R is bind a function to an active binding. That way, whenever you invoke the binding, it executes your piece of code. To illustrate:

makeActiveBinding('x', function () dim(data), globalenv())

Now whenever you enter x in the R console, it executes dim(data).

Comments