How to view/edit code of functions in Rstudio


I need to ask how to view/edit code of "pre-packaged" functions in Rstudio.

Kind Regards
Jan Žemlička

can you give an example of what you mean ?

In RStudio, you can see the code of a function by clicking F2 while cursor is on the function. In editor for RScript, there is a small wand with Go to function Definition


You cannot edit the function though.

R include a feature like this for temporarily (lasting the session) editing a function utils::edit(). See ?edit to see the help page. You can save the result in a new object for example.

I type in the function without brackets, although in many cases they will call primitive functions you can't edit easily and/or methods e.g.

> subset
#function (x, ...) 
#<bytecode: 0x10a2da720>
#<environment: namespace:base>
#function (x, subset, select, drop = FALSE, ...) 
#    ....etc...
#<bytecode: 0x10a2d9398>
#<environment: namespace:base>

If you want to edit the code you can copy-paste it into your own function (for above perhaps my_subset())

Works for functions in packages too, e.g.

> dplyr::add_count
#function (x, ..., wt = NULL, sort = FALSE) 
#    g <- group_vars(x)
#    grouped <- group_by(x, ..., add = TRUE)
#    out <- add_tally(grouped, wt = !!enquo(wt), sort = sort)
#   grouped_df(out, g)
#<bytecode: 0x10fc471f8>
#<environment: namespace:dplyr>
1 Like

Thank you very much!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like