How to define a function so that the R studio can predict its variable?

Viewed 205

For example, we type mat then the R studio predicts as follows:

matrix(data, nrow = rows, ncol = cols)

How to define my function so that the R studio can predict its variables ?

If I define my funtion foo(x,y) as an additon x+y and export by #'@export in my package then R studio can not predict its variable names x,y but only its function name foo().

What I wanted was something similar to that stated here: How to let R predict user input That is before I finish typing the column name, R has started predicting my input.


Thank you for the answer of @piece !! I can do it

1 Answers

Assuming you have a function foo:

foo <- function(x, y){
  x + y
}

In RStudio, go to Tools > Global Options > Code > Edit Snippets and add the following one:

snippet foo
    foo(x = ${1:x}, y = ${2:y})

Then if you type foo and hit tab you should have your snippet available.

enter image description here

Related