Exuberant Ctags with R
Asked Answered
R

7

20

Is there any documented use of ctags with R? Would this be useful? Would it be difficult to implement?

Specifically, I've just started using Vim. It would be cool to be able to write an R function in one file, use the function in another file (e.g., an Rnw file, test file, or some other script), and be able to use Ctrl+] to navigate to the function source.

Update: I've since stumbled on the rtags function. It is suggested below that it works with vim.

Raisin answered 25/1, 2011 at 14:49 Comment(0)
S
10

This is a modification of Henrico's answer, and may be implemented by copying and pasting the following code into one's ~/.ctags files. Henrico's code did not work for indented functions, but the following code does.

--langdef=R
--langmap=r:.R.r
--regex-R=/^[ \t]*"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-[ \t]function/\1/f,Functions/
--regex-R=/^"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-[ \t][^f][^u][^n][^c][^t][^i][^o][^n]/\1/g,GlobalVars/ 
--regex-R=/[ \t]"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-[ \t][^f][^u][^n][^c][^t][^i][^o][^n]/\1/v,FunctionVariables/

This allows variables to be recognized with ctags as well as functions. If you're using the taglist vim addon, then, it allows you to distinguish between global variables and other variables. Also, if you're using taglist, then you will need to paste the following in your vimrc.

let tlist_r_settings = 'R;f:Functions;g:GlobalVariables;v:FunctionVariables'
Sire answered 26/1, 2013 at 23:43 Comment(1)
I think this answer is wrong! Check https://mcmap.net/q/663553/-ctags-and-r-regexCharismatic
I
9

This is the content of my .ctags file in my home directory. I found it somewhere on the internet. Using this you can generate a tags-file for vim.

 --langdef=Splus
 --langmap=Splus:.s.S.R.r.q
 --regex-Splus=/^[ \t]+"?([.A-Za-z][.A-Za-z0-9_]*)"?[\t]*<-[\t]*function/\1/
 --regex-Splus=/^"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-/\1/
Iphlgenia answered 25/1, 2011 at 16:41 Comment(2)
@shabbychef Because the language definition for functions and such is equal, there is no need to use a separate Ctags definition. Ofcourse, you can just change --langdef=Splus to --langdef=R.Phlegethon
Your edits aren't really appropriate, @Sire - they change so much of Henrico's answer that it's not his answer any more. If you have a better solution, please post it as an answer of your own.Lacagnia
R
3

Probably you can read how to add support for a new language to ctags.

Redon answered 25/1, 2011 at 14:51 Comment(1)
Thanks. I had a quick look at that. I've never used ctags, so it might be a bit of a learning curve. But I plan to get into it more.Raisin
C
3

rtags() is the best to way to generate tags for R codes from what I see so far, since it will take all different ways of assignments in consideration, such as:

ok = c("<-", "=", "<<-", "assign",
       "setGeneric", "setGroupGeneric", "setMethod",
       "setClass", "setClassUnion")

An example of using rtags():

path <- "~/path/to/project"
rtags(path=path,
      pattern = "[.]*\\.[RrSs]$",
      keep.re = ".", # the value ('/R/') in the help page will only run through the codes in R/ folder.
      verbose = TRUE,
      ofile = file.path(path, 'TAGS'),
      append = FALSE,
      recursive = TRUE)
Claar answered 19/3, 2016 at 23:11 Comment(1)
This is useful, how rtags output can be integrated with vim?Cello
T
1

As mentioned by other rtags() + etags2ctags() can generate a tagsfile for vim (see :h tags-and-searches). You can create a custom command for vim so that it run rtags() in R using Rscript. To do so put this in your .vimrc:

:command! Rtags :!Rscript -e 'rtags(path="./", recursive=TRUE, ofile="RTAGS")' -e 'etags2ctags("RTAGS", "rtags")' -e 'unlink("RTAGS")'
set tags+=rtags

Now when you do :Rtags vim will run the external process Rscript (it has to be in the PATH of course) and evaluate rtags(path="./", recursive=TRUE, ofile="RTAGS");etags2ctags("RTAGS", "rtags");unlink("RTAGS"). rtags() will generate an RTAGS file in Emacs tags format then etags2ctags() will transform that into an rtags file which vim can read. unlink() deletes the RTAGS file since it's not needed for vim.

The set tags+=rtags makes vim to search for an rtags file in addition to the usual tags file (See :h tags-and-searches)

Teledu answered 8/12, 2016 at 13:37 Comment(0)
W
1

Universal-ctags has an R parser.

$ cat input.r
add <- function (x, y) {
    3 -> z
    return(x + y + z)
}
add (3, 4)
$ ./ctags --quiet --options=NONE -o - --kinds-R='*' --fields=+S input.r
add input.r /^add <- function (x, y) {$/;"  f   signature:(x, y)
x   input.r /^add <- function (x, y) {$/;"  z   function:add
y   input.r /^add <- function (x, y) {$/;"  z   function:add
z   input.r /^    3 -> z$/;"    v   function:add

In addition, the ctags implementation supports R6Class and S4Class.

Weatherspoon answered 30/10, 2020 at 2:29 Comment(0)
I
1

Actually, I think rtags() works in vim.

I use the nvim-r plugin (available at https://www.vim.org/scripts/script.php?script_id=2628), and have the following in my ~/.vimrc file:

:command! Rtags :!Rscript -e 'rtags(path="./", recursive=TRUE, ofile="TAGS")'

With this setup, using :Rtags in vim rebuilds the tags, and then e.g. g] when the mouse is over a word finds the definitions for that word.

Illusionary answered 2/6, 2021 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.